自分が所有しているワードプレスサイト内にあるリンク(外部/内部)を一括で取得(表示)する方法を紹介しています。
外部リンクを一括取得したかった理由
私がこれをやろうと思った理由は、楽天アフィリエイトやAmazonアソシエイトのアフィリエイトリンクが現在も有効かどうかを確かめる為です。
一記事ずつ開いて外部リンクをチェックしてとなると膨大な時間が掛かってしまうので、一覧でずらずらっと表示させたかったのです。
楽天アフィリエイトやAmazonアソシエイトの商品が有効で無くなっても「リンク切れ」ではないんですよね。
ページ自体は生きていて楽天アフィリエイトの場合は楽天のページで「ページが表示できません」表示に、Amazonの場合はAmazonのページで「申し訳ございません。入力されたウェブアドレスは~」と表示されるのでリンクチェッカー的なものを使っても反応しません。
ですので手動でチェック、もしくはスクレイピング的なものでチェックをするしかありません。
その為に自サイト内にある全ての外部リンクを一括で取得する必要がありました。
こういう使い方以外にも活用する方法があるかもしれませんので、必要な方は参考にして下さい。
リンクを一括取得する手順
それでは具体的な方法を説明していきます。
使用しているテーマのfunctions.phpに追記する
まず初めにご使用になっているテーマのfunctions.phpを開いて以下を追記します。(子テーマを使用している場合は子テーマへ)
URLをテキストとして表示する場合
// リンクを取得(テキスト表示)
function display_all_posts($atts) {
$atts = shortcode_atts(array(
'count' => -1,
'offset' => 0
), $atts);
// 下書き、非公開も取得する
$args = array(
'posts_per_page' => intval($atts['count']),
'offset' => intval($atts['offset']),
'post_status' => array('publish', 'draft', 'private') // 下書きと非公開も含む
);
$query = new WP_Query($args);
$output = '<div class="all-posts">';
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
$raw_content = get_the_content();
// oEmbedを無効にした状態で HTML整形(段落など)だけ反映
$content = wpautop($raw_content); // <p> や <br> などの整形のみ
// リンクタグを削除
$content_without_links = preg_replace('/<a[^>]*>(.*?)<\/a>/', '', $content); // <a>タグを完全に削除
// href のURLだけ抽出
preg_match_all('/<a[^>]+href=["\'](https?:\/\/[^"\']+)["\']/', $content, $matches);
$output .= '<div class="content-block">';
// リンクを削除した本文(整形あり、oEmbedなし)
$output .= '<div class="original-content">' . $content_without_links . '</div>';
// URLだけリスト表示
if (!empty($matches[1])) {
$output .= '<div class="url-list">';
foreach ($matches[1] as $url) {
$output .= '<div class="url-item">' . esc_url($url) . '</div>';
}
$output .= '</div>';
}
$output .= '</div>'; // .content-block
endwhile;
wp_reset_postdata();
else :
$output .= '<p>記事がありません。</p>';
endif;
$output .= '</div>';
return $output;
}
add_shortcode('all_posts', 'display_all_posts');
リンクタグを付与して表示する場合
// リンクを取得(リンクタグ付与)
function display_all_posts($atts) {
$atts = shortcode_atts(array(
'count' => -1,
'offset' => 0
), $atts);
// 下書き、非公開も取得する
$args = array(
'posts_per_page' => intval($atts['count']),
'offset' => intval($atts['offset']),
'post_status' => array('publish', 'draft', 'private') // 下書きと非公開も含む
);
$query = new WP_Query($args);
$output = '<div class="all-posts">';
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
$raw_content = get_the_content();
// 1. アンカー付きURL(<a href="https://...">リンク</a>)を処理してURLそのものに変換
$content = preg_replace_callback('/<a[^>]+href=["\'](https?:\/\/[^\s"\'<>]+)["\'][^>]*>(.*?)<\/a>/i', function($matches) {
// リンクのURL部分だけを抜き出して、URLそのものをリンクテキストとして表示
return esc_url($matches[1]); // URLそのものをリンクテキストに表示
}, $raw_content);
// 2. テキストURL(https://...)を見つけてリンク化
$content = preg_replace_callback('/https?:\/\/[^\s"\'<>]+/', function($matches) {
// テキストURLにリンクタグを追加
return '<a href="' . esc_url($matches[0]) . '" target="_blank">' . esc_url($matches[0]) . '</a>';
}, $content);
// 最終的にリンクを含んだコンテンツを表示
$output .= '<div class="content-block">';
$output .= '<div class="original-content">' . $content . '</div>';
$output .= '</div>';
endwhile;
wp_reset_postdata();
else :
$output .= '<p>記事がありません。</p>';
endif;
$output .= '</div>';
return $output;
}
add_shortcode('all_posts', 'display_all_posts');
投稿または固定ページにショートコードを貼り付ける
functions.phpへの追記が終わったら、投稿ページがか固定ページにショートコードを貼ります。
何パターンか用意したのでお好みのものを選んで貼り付けてください。([]の部分だけ)
全件取得 → [all_posts count="-1"]
最新100記事を取得 → [all_posts count="100"]
1記事目から100記事目を取得 → [all_posts count="100" offset="0"]
100記事目から200記事目を取得 → [all_posts count="100" offset="100"]
50記事目から50記事取得 → [all_posts count="50" offset="50"]
貼り付けたらプレビューを行なってみて下さい。
下記のようにリンクがズラズラと並んでいると思います。
URLをテキストとして表示した場合
リンクタグを付与して表示した場合
表示されたリンクを一括で開く方法
一個ずつ開くのが面倒な場合は拡張機能を使用すれば一括で開く事が可能です。
Chrome系ブラウザであればこちらがオススメです。
Linkclump
下書きや非公開を検索対象外にする
上で紹介したコードは公開済みの投稿だけでなく下書きや非公開も検索の対象になっています。
もし公開済みだけを検索対象にしたい場合はfunctions.phpの以下の部分の記述を削除してください。
// 下書き、非公開も取得する
$args = array(
'posts_per_page' => intval($atts['count']),
'offset' => intval($atts['offset']),
'post_status' => array('publish', 'draft', 'private') // 下書きと非公開も含む
);