[WordPress] カスタムフィールドの値を元に特定のページでリダイレクトする方法

<?php/** * 外部リンクページのリダイレクト処理 */function oc_template_redirect() { if ( is_page() ) { $status = 302; $location = ""; $post_id = get_queried_object_id(); // 外部サイトへリダイレクトするためのカスタムフィールドを取得 $location = get_post_meta($post_id, "oc_external_link_url", true); // 空でなければリダイレクト(状況によってはURLの文字列チェック等がいるかも) if(!empty($location) ) { wp_redirect( $location, $status ); exit; } }}add_action( 'template_redirect', 'oc_template_redirect' );