WordPressでサイトを運営していると、ページスピードやGTmetrixなどで記事ページの計測をしたいときがあります。
Cocoonなどテーマによっては、ページを開いたときに画面の下にリンクが追加されるので便利です。
この記事では管理ページの記事一覧に、タイトルごとに以下のリンクを追加する方法を紹介します。
- ページスピード
- 検索パフォーマンス
- リッチリザルトテスト
- ahrefs
Googleサーチコンソールの検索パフォーマンスやページスピードはよく使うので、管理ページから開けると便利です。
説明を読まずにコピペしてリンクを追加する
面倒なコードの説明を読まずに、以下のコードをfunction.phpに追加すればリンクを追加できます。
if (is_admin()) { // 管理画面のみメソッド追加する // 追加リンク function mch_add_tag_row_actions($actions, $post) { if ($post->post_type !== 'post' && $post->post_type !== 'page') { return $actions; } $base = 'https://' . $_SERVER['HTTP_HOST']; $post_name = $post->post_name; $encoded_url = "{$base}/{$post_name}/"; $encoded_url = urlencode(str_replace('&','&', $encoded_url)); $encoded_base = urlencode(str_replace('&','&', $base)); $encoded_post_name = urlencode(str_replace('&','&', $post_name)); $actions['pagespeed'] = "<a href='https://developers.google.com/speed/pagespeed/insights/?filter_third_party_resources=true&hl=ja&url={$encoded_url}' target='_blank' rel='noopener noreferrer'>ページスピード</a>"; $actions['gtmetrix'] = "<a href='https://gtmetrix.com/?url={$encoded_url}' target='_blank' rel='noopener noreferrer'>GTmetrix</a>"; $actions['rich-results'] = "<a href='https://search.google.com/test/rich-results?utm_campaign=sdtt&utm_medium=message&url={$encoded_url}&user_agent=1' target='_blank' rel='noopener noreferrer'>リッチリザルトテスト</a>"; $actions['gsc-performance'] = "<a href='https://search.google.com/search-console/performance/search-analytics?resource_id={$encoded_base}&page=*{$encoded_post_name}' target='_blank' rel='noopener noreferrer'>検索パフォーマンス</a>"; // $actions['ahrefs'] = "<a href='https://ahrefs.com/site-explorer/overview/v2/exact/live?target={$encoded_url}' target='_blank' rel='noopener noreferrer'>ahrefs</a>"; return $actions; } add_filter( 'page_row_actions', 'mch_add_tag_row_actions', 10, 2 ); add_filter( 'post_row_actions', 'mch_add_tag_row_actions', 10, 2 ); }
リンクを追加するソースコードの説明
マウスオーバーしたとき表示される、投稿一覧ページの「ゴミ箱へ移動」「表示」のとなりにリンクを追加するには add_filter() を使います。
filterは「page_row_actions」と「post_row_actions」の2つで、投稿と固定ページで異なります。
今回は同じリンクを追加するので、同じ関数で処理しています。
add_filter( 'page_row_actions', 'mch_add_tag_row_actions', 10, 2 ); add_filter( 'post_row_actions', 'mch_add_tag_row_actions', 10, 2 );
第一引数の$actionsはkey-valueの配列になっていて、値にはリンクタグが格納されています。
格納する順番を変えれば、リンクの順番も連動して変更されます。
コメント