WordPressがheadタグに吐き出すタグは不要なものも多いので、不要なタグは削除してソースコードをきれいにする。
コンテンツ
ソースコード全体
// RSSフィードを無効化
remove_action('do_feed_rdf', 'do_feed_rdf');
remove_action('do_feed_rss', 'do_feed_rss');
remove_action('do_feed_rss2', 'do_feed_rss2');
remove_action('do_feed_atom', 'do_feed_atom');
//RSSフィードのリンクを除去
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'feed_links_extra', 3);
// generatorを非表示にする
remove_action('wp_head', 'wp_generator');
// EditURIを非表示にする
remove_action('wp_head', 'rsd_link');
// wlwmanifestを非表示にする
remove_action('wp_head', 'wlwmanifest_link');
// 短縮URLを非表示にする
remove_action('wp_head', 'wp_shortlink_wp_head');
// 絵文字用JS・CSSを非表示にする
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_styles', 'print_emoji_styles');
// コメントフィードを非表示にする
remove_action('wp_head', 'feed_links_extra', 3);
// WordPressのバージョンが付与されたver=〜 を非表示にする
function vc_remove_wp_ver_css_js($src) {
if (strpos($src, 'ver=' . get_bloginfo('version')))
$src = remove_query_arg('ver', $src);
return $src;
}
add_filter('style_loader_src', 'vc_remove_wp_ver_css_js', 9999);
add_filter('script_loader_src', 'vc_remove_wp_ver_css_js', 9999);
// dns-prefetchを非表示にする
add_filter('wp_resource_hints', 'remove_dns_prefetch', 10, 2);
function remove_dns_prefetch($hints, $relation_type) {
if ('dns-prefetch' === $relation_type) {
return array_diff(wp_dependencies_unique_hosts(), $hints);
}
return $hints;
}
// oEmbedを非表示にする
remove_action('wp_head', 'wp_oembed_add_discovery_links');
//rel="next" rel="prev" を非表示にする
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
ソースコードから削除できるhtmlタグ詳細
上記ソースコードをfunctions.phpに記述すると、以下のhtmlタグを除去することができる。
RSSフィードリンクを除去
<link rel="alternate" type="application/rss+xml" href="https://example.com/feed">
<link rel="alternate" type="application/rss+xml" title="サンプル » コメントフィード" href="https://example.com/comments/feed/">
generatorを非表示にする
meta generator は、文書の作成に使用したソフトウェア名を示すタグ。
<meta name="generator" content="Wordpress 5.2.2" />
EditURIを非表示にする
外部の投稿ツール(XML-RPCでの投稿)からWordPressに記事を投稿する際に必要なタグ。
<meta name="generator" content="Wordpress 5.2.2" />
wlwmanifestを非表示にする
マイクロソフト製のブログ更新アプリの「Windows Live Writer」からの投稿を受け付けるためのタグ。
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://vccw.dev/wp-includes/wlwmanifest.xml" />
短縮URLを非表示にする
WordPressはページ毎に短縮URLが出力される。
<link rel="shortlink" href="https://site-domain.com/?p=1" />
絵文字用JS・CSSを非表示にする
絵文字を使用しなければ不要。
<script type="text/javascript">
window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/2.3\/72x72\/","ext":".png","svgUrl":false,"svgExt":".svg","source":{"concatemoji":"http:\/\/vccw.dev\/wp-includes\/js\/wp-emoji-release.min.js?ver=4.8.2"}};
!function(a,b,c){function d(a){var b,c,d,e,f=String.fromCharCode;if(!k||!k.fillText)return!1;switch(k.clearRect(0,0,j.width,j.height),k.textBaseline="top",k.font="600 32px Arial",a){case"flag":return k.fillText(f(55356,56826,55356,56819),0,0),b=j.toDataURL(),k.clearRect(0,0,j.width,j.height),k.fillText(f(55356,56826,8203,55356,56819),0,0),c=j.toDataURL(),b!==c&&(k.clearRect(0,0,j.width,j.height),k.fillText(f(55356,57332,56128,56423,56128,56418,56128,56421,56128,56430,56128,56423,56128,56447),0,0),b=j.toDataURL(),k.clearRect(0,0,j.width,j.height),k.fillText(f(55356,57332,8203,56128,56423,8203,56128,56418,8203,56128,56421,8203,56128,56430,8203,56128,56423,8203,56128,56447),0,0),c=j.toDataURL(),b!==c);case"emoji4":return k.fillText(f(55358,56794,8205,9794,65039),0,0),d=j.toDataURL(),k.clearRect(0,0,j.width,j.height),k.fillText(f(55358,56794,8203,9794,65039),0,0),e=j.toDataURL(),d!==e}return!1}function e(a){var c=b.createElement("script");c.src=a,c.defer=c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f,g,h,i,j=b.createElement("canvas"),k=j.getContext&&j.getContext("2d");for(i=Array("flag","emoji4"),c.supports={everything:!0,everythingExceptFlag:!0},h=0;h<i.length;h++)c.supports[i[h]]=d(i[h]),c.supports.everything=c.supports.everything&&c.supports[i[h]],"flag"!==i[h]&&(c.supports.everythingExceptFlag=c.supports.everythingExceptFlag&&c.supports[i[h]]);c.supports.everythingExceptFlag=c.supports.everythingExceptFlag&&!c.supports.flag,c.DOMReady=!1,c.readyCallback=function(){c.DOMReady=!0},c.supports.everything||(g=function(){c.readyCallback()},b.addEventListener?(b.addEventListener("DOMContentLoaded",g,!1),a.addEventListener("load",g,!1)):(a.attachEvent("onload",g),b.attachEvent("onreadystatechange",function(){"complete"===b.readyState&&c.readyCallback()})),f=c.source||{},f.concatemoji?e(f.concatemoji):f.wpemoji&&f.twemoji&&(e(f.twemoji),e(f.wpemoji)))}(window,document,window._wpemojiSettings);
</script>
<style type="text/css">
img.wp-smiley,
img.emoji {
display: inline !important;
border: none !important;
box-shadow: none !important;
height: 1em !important;
width: 1em !important;
margin: 0 .07em !important;
vertical-align: -0.1em !important;
background: none !important;
padding: 0 !important;
}
</style>
コメントフィードを非表示にする
<link title="コメントフィード" href="https://www.site-domain.com/" rel="alternate" type="application/rss+xml" />
WordPressのバージョンが付与されたver=〜 を非表示にする
WordPressはJavaScriptやCSSに、WordPressやプラグインもしくはスクリプトそのもののバージョン番号を付与する。ブラウザがこのバージョン番号が変わったことで、古いキャッシュを使わないようにするためのもの。
<link rel='stylesheet' href='https://site-dimain.com/wp-content/plugins/plugin-name/example.css?ver=1.0.0' media='all'>
<script src='https://site-dimain.com/wp-content/themes/theme-name/js/example.js?ver=1.0.0' defer charset='UTF-8'></script>
dns-prefetchを非表示にする
「DNS PrefetchingDNS(プリフェッチ)」を利用するための記述。
DNSプリフェッチは、事前に外部ドメインDNSの名前解決しておくようにするもので、読み込み時間が若干早くなる。WordPressでは、絵文字の保存ドメインをプリフェッチしている。
<link rel="dns-prefetch" href="//s.w.org" />
oEmbedを非表示にする
oEmbedはYouTubeやTwitterなどの外部コンテンツを埋め込むための機能。
<link rel="alternate" type="application/json+oembed" href="https://site-domain.com/wp-json/oembed/1.0/embed?url=http%3A%2F%2Fwww.poroanet.com%2Fblog%2Fwordpress%2F58.html" />
<link rel="alternate" type="text/xml+oembed" href="https://site-domain.com/wp-json/oembed/1.0/embed?url=http%3A%2F%2Fwww.poroanet.com%2Fblog%2Fwordpress%2F58.html&format=xml" />
rel=”next” rel=”prev” を非表示にする
ページネーションした一連のページの繋がり関係をGoogleに伝えるための仕組みですが、Googleは2019年にこのサポートの終了を発表。
<link rel="prev" href="https://site-dimain.com/prev-page">
<link rel="next" href="https://site-dimain.com/next-page">
その他
プラグインから読み込まれるタグもあり、個別に対応が可能な場合がある。
参考:https://cocorograph.co/knowledge/how-to-delete-wordpress-unnecessary-tags/