
WordPressサイトを運営していて、たまにこんなことがあります。
この投稿の内容はもう古いので、別の効果的なページにリダイレクトで跳ばしたい。
この機能を、コピペ一発で実装できるコードを書きました。
以下のように投稿・固定ページ管理画面から「リダイレクトURL」を設定するだけで目的のページへ跳ばせる仕様です。
このように「管理者側から手軽にリダイレクト変更できる仕様」の利点は、
やっぱやめた戻そう。
となった時にでも、「リダイレクトURL」入力欄を空欄に戻せば、手軽に元に戻せることです。
以下では、このカスタマイズ方法を紹介します。
目次
投稿・固定ページリダイレクトカスタマイズ
投稿・固定ページに入ってきた流入を、対象URLにリダイレクトするカスタマイズはこちら。子テーマのfunctions.phpにコピペで追記することで実装できます。
///////////////////////////////////////
// カスタムボックスの追加
///////////////////////////////////////
add_action('admin_menu', 'add_redirect_custom_box');
if ( !function_exists( 'add_redirect_custom_box' ) ):
function add_redirect_custom_box(){
//リダイレクト
add_meta_box( 'singular_redirect_settings', 'リダイレクト', 'redirect_custom_box_view', 'post', 'side' );
add_meta_box( 'singular_redirect_settings', 'リダイレクト', 'redirect_custom_box_view', 'page', 'side' );
}
endif;
///////////////////////////////////////
// リダイレクト
///////////////////////////////////////
if ( !function_exists( 'redirect_custom_box_view' ) ):
function redirect_custom_box_view(){
$redirect_url = get_post_meta(get_the_ID(),'redirect_url', true);
echo '<label for="redirect_url">リダイレクトURL</label>';
echo '<input type="text" name="redirect_url" size="20" value="'.esc_attr(stripslashes_deep(strip_tags($redirect_url))).'" placeholder="https://" style="width: 100%;">';
echo '<p class="howto">このページに訪れるユーザーを設定したURLに301リダイレクトします。</p>';
}
endif;
add_action('save_post', 'redirect_custom_box_save_data');
if ( !function_exists( 'redirect_custom_box_save_data' ) ):
function redirect_custom_box_save_data(){
$id = get_the_ID();
//リダイレクトURL
if ( isset( $_POST['redirect_url'] ) ){
$redirect_url = $_POST['redirect_url'];
$redirect_url_key = 'redirect_url';
add_post_meta($id, $redirect_url_key, $redirect_url, true);
update_post_meta($id, $redirect_url_key, $redirect_url);
}
}
endif;
//リダイレクトURLの取得
if ( !function_exists( 'get_singular_redirect_url' ) ):
function get_singular_redirect_url(){
return trim(get_post_meta(get_the_ID(), 'redirect_url', true));
}
endif;
//リダイレクト処理
if ( !function_exists( 'redirect_to_url' ) ):
function redirect_to_url($url){
header( "HTTP/1.1 301 Moved Permanently" );
header( "location: " . $url );
exit;
}
endif;
//URLの正規表現
define('URL_REG_STR', '(https?|ftp)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)');
define('URL_REG', '/'.URL_REG_STR.'/');
//リダイレクト
add_action( 'wp','wp_singular_page_redirect', 0 );
if ( !function_exists( 'wp_singular_page_redirect' ) ):
function wp_singular_page_redirect() {
//リダイレクト
if (is_singular() && $redirect_url = get_singular_redirect_url()) {
//URL形式にマッチする場合
if (preg_match(URL_REG, $redirect_url)) {
redirect_to_url($redirect_url);
}
}
}
endif;
このカスタマイズは、「投稿・固定ページに入ってきた訪問者」に対してだけ、リダイレクトする仕様です。
カテゴリーページや、アーカイブページへの利用はできませんのでご了承ください。
動作確認
上記のPHPコードを子テーマのfunctions.phpに貼り付けて、「投稿管理画面」を開くと、サイドに「リダイレクト」ボックスが表示されます。
あとは「リダイレクトURL」テキストボックスに対して、リダイレクト先のURLを入力すればOKです。
あとは、対象ページに流入があった場合に「リダイレクトURL」に入力されたURLに対して、301リダイレクトされます。
まとめ
とりあえず「リダイレクトが設定できるカスタマイズがしたい」という場合であれば、コピペ一発でできるので、比較的簡単にできるかと思います。
なので「もう情報が古くなってしまってあまり意味を成さないページ」があるのであれば、「関連性があって効果的なページ」にアクセスを流入させるというのもありかと思います。
うまくいかない場合は、コピペした箇所を元に戻す(削除する)だけです。



素晴らしいコードをありがとうございました!
まさにこれを探していました。