沖縄は11月なのにまだまだ暑く海にも入れる気温です。なので今度シュノーケルにいってきやす!
僕は普段Wordpressを使用する時は毎回テーマをいちから自作しています。
そんな時head内に出てくるデフォルトのコードがあると邪魔なので毎回削除しています。今回はその不要なコードを削除する方法(フック)の紹介です。
まずは何もしていない状態でのコードを見てみましょう。
※お使いのWordpressのバージョンによって異なります。
<script type="text/javascript"> window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/2\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/2\/svg\/","svgExt":".svg","source":{"concatemoji":"http:\/\/example.com\/wp\/wp-includes\/js\/wp-emoji-release.min.js"}}; !function(a,b,c){function d(a){var c,d,e,f,g,h=b.createElement("canvas"),i=h.getContext&&h.getContext("2d"),j=String.fromCharCode;if(!i||!i.fillText)return!1;switch(i.textBaseline="top",i.font="600 32px Arial",a){case"flag":return i.fillText(j(55356,56806,55356,56826),0,0),!(h.toDataURL().length<3e3)&&(i.clearRect(0,0,h.width,h.height),i.fillText(j(55356,57331,65039,8205,55356,57096),0,0),c=h.toDataURL(),i.clearRect(0,0,h.width,h.height),i.fillText(j(55356,57331,55356,57096),0,0),d=h.toDataURL(),c!==d);case"diversity":return i.fillText(j(55356,57221),0,0),e=i.getImageData(16,16,1,1).data,f=e[0]+","+e[1]+","+e[2]+","+e[3],i.fillText(j(55356,57221,55356,57343),0,0),e=i.getImageData(16,16,1,1).data,g=e[0]+","+e[1]+","+e[2]+","+e[3],f!==g;case"simple":return i.fillText(j(55357,56835),0,0),0!==i.getImageData(16,16,1,1).data[0];case"unicode8":return i.fillText(j(55356,57135),0,0),0!==i.getImageData(16,16,1,1).data[0];case"unicode9":return i.fillText(j(55358,56631),0,0),0!==i.getImageData(16,16,1,1).data[0]}return!1}function e(a){var c=b.createElement("script");c.src=a,c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f,g,h,i;for(i=Array("simple","flag","unicode8","diversity","unicode9"),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 rel='https://api.w.org/' href='http://example.com/wp-json/' /> <link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://example.com/wp/xmlrpc.php?rsd" /> <link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://example.com/wp/wp-includes/wlwmanifest.xml" /> <meta name="generator" content="WordPress 4.6.1" />
うーん。いろいろ出てきますね。仕様や運営方針よっては必要なコードもあるでしょうが、個人的にほとんどの場合、不要だと思います。
出力されるコードの説明も兼ねて出力しない方法をご案内していきます。
絵文字(Emoji and Smily)用のJSとCSSを削除する
WordPress 4.2から記事本文内に絵文字が使えるようになりました。
アメブロなどでよく見かけるアレですね。
そのために追加された絵文字用のJSとCSSが下記です。まずはこれを出力しない方法です。
<script type="text/javascript"> window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/2\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/2\/svg\/","svgExt":".svg","source":{"concatemoji":"http:\/\/example.com\/wp\/wp-includes\/js\/wp-emoji-release.min.js"}}; !function(a,b,c){function d(a){var c,d,e,f,g,h=b.createElement("canvas"),i=h.getContext&&h.getContext("2d"),j=String.fromCharCode;if(!i||!i.fillText)return!1;switch(i.textBaseline="top",i.font="600 32px Arial",a){case"flag":return i.fillText(j(55356,56806,55356,56826),0,0),!(h.toDataURL().length<3e3)&&(i.clearRect(0,0,h.width,h.height),i.fillText(j(55356,57331,65039,8205,55356,57096),0,0),c=h.toDataURL(),i.clearRect(0,0,h.width,h.height),i.fillText(j(55356,57331,55356,57096),0,0),d=h.toDataURL(),c!==d);case"diversity":return i.fillText(j(55356,57221),0,0),e=i.getImageData(16,16,1,1).data,f=e[0]+","+e[1]+","+e[2]+","+e[3],i.fillText(j(55356,57221,55356,57343),0,0),e=i.getImageData(16,16,1,1).data,g=e[0]+","+e[1]+","+e[2]+","+e[3],f!==g;case"simple":return i.fillText(j(55357,56835),0,0),0!==i.getImageData(16,16,1,1).data[0];case"unicode8":return i.fillText(j(55356,57135),0,0),0!==i.getImageData(16,16,1,1).data[0];case"unicode9":return i.fillText(j(55358,56631),0,0),0!==i.getImageData(16,16,1,1).data[0]}return!1}function e(a){var c=b.createElement("script");c.src=a,c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f,g,h,i;for(i=Array("simple","flag","unicode8","diversity","unicode9"),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>
■functions.php
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');
上記をfunctions.phpに追記することで削除できます。
Embed(https://api.w.org/)を削除する
<link rel='https://api.w.org/' href='http://example.com/wp-json/' />
さて次は上記のコードですが、こちらは「Embed」というシステムのためのものです。
僕の周囲ではこのシステムを使うようなサイトをいまだ見たことありません。なので下記のコードをfunctions.phpに追記して削除します。
■functions.php
remove_action('wp_head','rest_output_link_wp_head'); remove_action('wp_head','wp_oembed_add_discovery_links'); remove_action('wp_head','wp_oembed_add_host_js');
上記をfunctions.phpに追記することで削除できます。
EditURIを削除する
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://example.com/wp/xmlrpc.php?rsd" />
これはMovable TypeやBloggerなどのXML-RPC 投稿プロトコルで利用される情報が、このアドレスで提供されています。記事をWordpressの管理画面からのみ更新する場合は不要なので削除しましょう。
■functions.php
remove_action('wp_head','rsd_link');
上記をfunctions.phpに追記することで削除できます。
wlwmanifestを削除する
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://example.com/wp/wp-includes/wlwmanifest.xml" />
このコードはWindows Live Writerというマイクロソフトが開発したブログ作成用のソフトウェア用のコードです。Wordpressでは管理画面にログインせずともこのソフトから記事を投稿することができるんですね。
Windows Live Writer(ウィンドウズ ライブ ライター)とは、マイクロソフトが開発したソフトウェアで、ブログの投稿と編集を行うアプリケーションである。 Windows Live のサービスの1つである。 Windows Essentialsに含まれていて、インストールできる。
引用元:Wikipedia
使ってみたいなーとは思っているんですが、僕はMac派なので使えないんですよね。僕みたいにWindows Live Writerを使っていない人には無用ですよね。下記のコードをfunctions.phpに追記して消しちゃいましょう。
■functions.php
remove_action('wp_head','wlwmanifest_link');
上記をfunctions.phpに追記することで削除できます。
Generator(Wordpressのバージョン)を削除する
<meta name="generator" content="WordPress 4.6.1" />
デフォルトの状態だと使用しているWordpressのバージョンが出力されてしまいます。定期的にWPをバージョンアップしていればそんなに問題はないのですが、バージョンアップした際にプラグインや自作したテーマのプログラムとの兼ね合いでバグが起きる可能性を懸念してバージョンアップぜず古いバージョンのまま使っている人もちょくちょくいます(あとはサーバーの仕様上の問題とかもあったりしますが割愛)
しかし、それは危険です。過去のバージョンで認識されている脆弱性をついて攻撃される可能性があります(これを解消するためにもバージョンアップは随時しましょう)Wordpressのバージョンが外部にわかってしまうと攻撃方法を絞り込むことができますので、せてめバージョンは表示しないようにしましょう。
■functions.php
remove_action('wp_head','wp_generator');
上記をfunctions.phpに追記することで削除できます。
まとめ
最後に、上記のソースコードを削除するフックをまとめておきます。よろしければご参考くださいな。
/* 絵文字用の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'); /* Edit URI 削除 */ remove_action('wp_head','rsd_link'); /* wlwmanifest 削除 */ remove_action('wp_head','wlwmanifest_link'); // Generator 削除 remove_action('wp_head','wp_generator');