ずっと、プラグイン「Redirection」を使ったりやhtaccessを編集したりして、301リダイレクトの設定をしていました。
今回は、できればいいなと思っていた投稿ページでの301リダイレクトを設置する方法を見つけたので、嬉しくなって投稿しています^^
投稿ページで301リダイレクト設定をする方法
前提として多くのページを一気に301リダイレクト設定する場合は、Redirectionかhtaccessのほうが便利です。
今回の方法は、あーこの投稿記事だけ、別のサイトに移したい場合(あとは別のカテゴリー、あとはアドレスを変えたいとかも)に、便利な方法です。
Cocoonでは初期設定で投稿ページに301リダイレクトの設定欄がある
今ご覧のこのサイトは、CocoonというWordpressテーマを使用して作っているのですが、このCocoonでは、初期設定から投稿ページに301リダイレクトがあります。

Cocoonでは投稿ページの右サイドバーから設定できる
Cocoonに限らず、最近のテーマは投稿ページ内に、SEO対策で記事タイトルを見出し部分と別に設定できたり、記事ごとにCSSを設定できたり、メタディスクリプションを設定できたり、canonical設定できたりと色々便利ですよね。

とずっと思っていたのです。
リダイレクトはしょっちゅう使うわけではないので、Cocoonを使い始めてからも当分のあいだ、気付いていなかったのですが、初めて見つけた時は嬉しくなりました^^
使用中テーマの投稿ページで301リダイレクトを設定できるようにする方法
今回、別のテーマで運営しているサイトで投稿ページ内で301リダイレクトさせられないかと思って、調べてみたところ、なんとCocconの設計者であるわいひらさんの寝ログというサイト内に記述してあるではないですか!?灯台下暗し^^;
早速、設置方法をご紹介しますが、めちゃめちゃ簡単です。
子テーマのfunctions.phpに、次のコードをコピペするだけで実装できます。
(わいひらさんのコードをそのまま記述しています。品用:https://nelog.jp/wordpress-redirect-custom)
///////////////////////////////////////
// カスタムボックスの追加
///////////////////////////////////////
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;
これで、オッケー!
投稿ページを見てみると、こんな感じでCocoon同様に右のサイドバーに301リダイレクト設定できる欄ができました!
注意点が1つだけあって、この方法では投稿ページと固定ページにのみ301リダイレクト欄がでます。カテゴリーページなどでは出来ないのでご注意ください。
コメント