先日、イベントカレンダー(XO Event Calendar)のタイトルをクリックしたら、指定したページへジャンプする方法をご案内しました。

プラグインの投稿画面に、カスタムフィールドというものを使ったリダイレクトの機能を追加したのですが、今回は、投稿ページ・固定ページにそのリダイレクト用のボックスを追加する手順をご案内いたします。
今回もちょっと普段触らないような箇所を触るので、先にバックアップを取っておいてくださいね。
ワードプレスの設定箇所を開く
ワードプレスにログインします。
そして、左側のメニューの[外観]ー[テーマエディター]をクリックします。

右側の方で編集するテーマが、使用しているテーマの子テーマであることを確認して(ここでは、SANGO Child)、その下のテーマファイルの中から、『テーマのための関数(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;
前項で開いた『テーマのための関数(functions.php)』の
/************************
*functions.phpへの追記は以下に
*************************/
と
/************************
*functions.phpへの追記はこの上に
*************************/
の間にコピーした記述をペーストします。
そして、画面を下の方にスクロールして、『ファイルを更新』ボタンを押します。
これで、投稿ページ・固定ページにリダイレクトのボックスができました。
投稿ページ・固定ページの入力画面を確認してみよう!
左側のメニューから、[投稿]-[新規追加](または[固定ページ]-[新規追加])をクリックします。

すると、このようにサイドバーに『リダイレクト』欄が表示されるようになっていることをご確認ください。
この『リダイレクトURL』にジャンプさせたいアドレスを入力すると、タイトルをクリックしたら、そのアドレスにジャンプするようになります。
作業お疲れ様でした。お役に立てますと幸いです!