WordPressのヘッダーに出力される不要なタグを削除する



Wordpressのヘッダーに出力される不要なタグを削除する

WordPressは初期状態だと wp_head(); で沢山タグが出力される。何かしら便利なタグなのかもしれないが、必要なもの以外は出力されないようにしている。

この記事を書いている現在の最新版WordPressバージョン6.1.1では以下で一通り削除できた。

functions.phpに追記するコード

//絵文字スクリプト削除
remove_action('wp_head', 'print_emoji_detection_script', 7);
//絵文字スタイル削除
remove_action('wp_print_styles', 'print_emoji_styles');
//記事ページprev,next削除
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10);
//記事ページcanonical削除
remove_action('wp_head', 'rel_canonical');
//REST APIの何か削除
remove_action('wp_head', 'rest_output_link_wp_head');
//EditURIの何か削除
remove_action('wp_head', 'rsd_link');
//Windows Live Writerの何か削除
remove_action('wp_head', 'wlwmanifest_link');
//WordPressのバージョン表示削除
remove_action('wp_head', 'wp_generator');
//サイトアイコン関係削除
remove_action('wp_head', 'wp_site_icon', 99);
//記事リンクをiframeにする何か削除
remove_action('wp_head', 'wp_oembed_add_discovery_links');
//記事リンクをiframeにする何か削除(フッター)
remove_action('wp_head', 'wp_oembed_add_host_js');
//shortlink削除
remove_action('wp_head', 'wp_shortlink_wp_head');
//EditURIの何か削除(レスポンスヘッダー)
remove_action('template_redirect', 'rest_output_link_header', 11);
//shortlink削除(レスポンスヘッダー)
remove_action('template_redirect', 'wp_shortlink_header', 11);
//検索エンジン向けの画像設定の何か削除
remove_action('wp_robots','wp_robots_max_image_preview_large');
//dns-prefetch削除
function custom_remove_dns_prefetch($hints, $relation_type) {
  if('dns-prefetch' === $relation_type) {
    return array_diff(wp_dependencies_unique_hosts(), $hints);
  }
  return $hints;
}
add_filter('wp_resource_hints', 'custom_remove_dns_prefetch', 10, 2);
//CSS削除
function custom_remove_css() {
  //Gutenberg関係の何か
  wp_dequeue_style('wp-block-library');
  //ブロックテーマで使用される何か
  wp_dequeue_style('global-styles');
  //クラシックテーマ向けの何か
  wp_dequeue_style('classic-theme-styles');
}
add_action('wp_enqueue_scripts', 'custom_remove_css');

削除されるタグ

絵文字スクリプト削除

<script type="text/javascript">
	window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/","svgExt":".svg","source":{"concatemoji":"https:\/\/http://example.com/\/wp-includes\/js\/wp-emoji-release.min.js?ver=5.3.2"}};
	!function(e,a,t){var r,n,o,i,p=a.createElement("canvas"),s=p.getContext&&p.getContext("2d");function c(e,t){var a=String.fromCharCode;s.clearRect(0,0,p.width,p.height),s.fillText(a.apply(this,e),0,0);var r=p.toDataURL();return s.clearRect(0,0,p.width,p.height),s.fillText(a.apply(this,t),0,0),r===p.toDataURL()}function l(e){if(!s||!s.fillText)return!1;switch(s.textBaseline="top",s.font="600 32px Arial",e){case"flag":return!c([127987,65039,8205,9895,65039],[127987,65039,8203,9895,65039])&&(!c([55356,56826,55356,56819],[55356,56826,8203,55356,56819])&&!c([55356,57332,56128,56423,56128,56418,56128,56421,56128,56430,56128,56423,56128,56447],[55356,57332,8203,56128,56423,8203,56128,56418,8203,56128,56421,8203,56128,56430,8203,56128,56423,8203,56128,56447]));case"emoji":return!c([55357,56424,55356,57342,8205,55358,56605,8205,55357,56424,55356,57340],[55357,56424,55356,57342,8203,55358,56605,8203,55357,56424,55356,57340])}return!1}function d(e){var t=a.createElement("script");t.src=e,t.defer=t.type="text/javascript",a.getElementsByTagName("head")[0].appendChild(t)}for(i=Array("flag","emoji"),t.supports={everything:!0,everythingExceptFlag:!0},o=0;o<i.length;o++)t.supports[i[o]]=l(i[o]),t.supports.everything=t.supports.everything&&t.supports[i[o]],"flag"!==i[o]&&(t.supports.everythingExceptFlag=t.supports.everythingExceptFlag&&t.supports[i[o]]);t.supports.everythingExceptFlag=t.supports.everythingExceptFlag&&!t.supports.flag,t.DOMReady=!1,t.readyCallback=function(){t.DOMReady=!0},t.supports.everything||(n=function(){t.readyCallback()},a.addEventListener?(a.addEventListener("DOMContentLoaded",n,!1),e.addEventListener("load",n,!1)):(e.attachEvent("onload",n),a.attachEvent("onreadystatechange",function(){"complete"===a.readyState&&t.readyCallback()})),(r=t.source||{}).concatemoji?d(r.concatemoji):r.wpemoji&&r.twemoji&&(d(r.twemoji),d(r.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>

記事ページprev,next削除

<link rel='prev' title='前の記事タイトル' href='https://example.com/1/' />
<link rel='next' title='次の記事タイトル' href='https://example.com/3/' />

記事ページcanonical削除

<link rel="canonical" href="https://example.com/1/" />

REST APIの何か削除

<link rel='https://api.w.org/' href='https://example.com/wp-json/' />

EditURIの何か削除

<link rel="EditURI" type="application/rsd+xml" title="RSD" href="https://example.com/xmlrpc.php?rsd" />

Windows Live Writerの何か削除

<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="https://example.com/wp-includes/wlwmanifest.xml" />

WordPressのバージョン表示削除

<meta name="generator" content="WordPress 5.3.2" />

サイトアイコン関係削除

<link rel="icon" href="https://example.com/wp-content/uploads/icon-32x32.png" sizes="32x32" />
<link rel="icon" href="https://example.com/wp-content/uploads/icon-192x192.png" sizes="192x192" />
<link rel="apple-touch-icon-precomposed" href="https://example.com/wp-content/uploads/icon-180x180.png" />
<meta name="msapplication-TileImage" content="https://example.com/wp-content/uploads/icon-270x270.png" />

記事リンクをiframeにする何か削除

<link rel="alternate" type="application/json+oembed" href="https://example.com/wp-json/oembed/1.0/embed?url=https%3A%2F%2Fhttp://example.com/%2F1%2F" />
<link rel="alternate" type="text/xml+oembed" href="https://example.com/wp-json/oembed/1.0/embed?url=https%3A%2F%2Fhttp://example.com/%2F1%2F&format=xml" />

記事リンクをiframeにする何か削除(フッター)

<script type='text/javascript' src='https://example.com/wp-includes/js/wp-embed.min.js?ver=5.3.2'></script>

shortlink削除

<link rel='shortlink' href='https://example.com/?p=1' />

EditURIの何か削除(レスポンスヘッダー)

Link: <https://example.com/wp-json/>; rel="https://api.w.org/"

shortlink削除(レスポンスヘッダー)

Link: <https://example.com/?p=1021>; rel=shortlink

検索エンジン向けの画像設定の何か削除

<meta name='robots' content='max-image-preview:large' />

dns-prefetch削除

<link rel='dns-prefetch' href='//s.w.org' />

Gutenberg関係のCSS削除

<link rel='stylesheet' id='wp-block-library-css'  href='https://example.com/wp-includes/css/dist/block-library/style.min.css?ver=5.3.2' type='text/css' media='all' />

クラシックテーマ向けの何かのCSS削除

<link rel='stylesheet' id='classic-theme-styles-css' href='https://example.com/wp-includes/css/classic-themes.min.css?ver=1' type='text/css' media='all' />

ブロックテーマで使用される何かのCSS削除

<style id='global-styles-inline-css' type='text/css'>
body{--wp--preset--color--black: #000000;~省略~.wp-block-pullquote{font-size: 1.5em;line-height: 1.6;}
</style>



関連記事