編集画面にnoindex設定を追加 ( カスタム投稿に対応 )

概要説明

記事ごとにnoindexの指定を行うようにできるカスタマイズ。カスタム投稿のpost_typeを指定することでカスタム投稿でもnoindex制御が行なえます。検索エンジンに表示させたくないページはこちらの機能でページごとに個別に設定が行なえます。

functions.php
/*----------------------------------------------------
 head内にカスタム用のコードを追加する
----------------------------------------------------*/
if ( !function_exists( 'custom_meta_robots' ) ){
    function custom_meta_robots( array $robots ) {
        global $post;
        if ( isset($post->ID) ) {
            if ( get_post_meta( $post->ID, 'noindex' , true ) ) {
                $robots['noindex'] = true;
                $robots['follow']  = true;
                $robots['max-image-preview'] = false;
            }
        }
        return $robots;
    }
    add_filter( 'wp_robots', 'custom_meta_robots' );
}
/*----------------------------------------------------
 編集画面(サイド)にチェックボックスエリアを表示
----------------------------------------------------*/
if ( !function_exists( 'add_noindex_metabox' ) ){
    function add_noindex_metabox() {
        add_meta_box( 'custom_noindex', 'インデックス設定', 'create_noindex', array('post', 'page'), 'side' );
    }
    add_action( 'admin_menu', 'add_noindex_metabox' );

    function create_noindex() {
        $key = 'noindex';
        global $post;
        $get_value = get_post_meta( $post->ID, $key, true );
        wp_nonce_field( 'action_' . $key, 'nonce_' . $key );
        $value = 'noindex';
        $c = '';
        if( $value === $get_value ) $c = ' checked';
        echo '<label><input type="checkbox" name="' . $key . '" value="' . $value .'"' . $c . '>' . $key . '</label>';
    }
}

/*----------------------------------------------------
 カスタムフィールドの保存
----------------------------------------------------*/
if ( !function_exists( 'save_custom_noindex' ) ){
    function save_custom_noindex( $post_id ) {
        $key = 'noindex';
        if ( isset( $_POST['nonce_' . $key] )) {
            if( check_admin_referer( 'action_' . $key, 'nonce_' . $key ) ) {
                if( isset( $_POST[$key] )) {
                    update_post_meta( $post_id, $key, $_POST[$key] );
                } else {
                    delete_post_meta( $post_id, $key, get_post_meta( $post_id, $key, true ) );
                }
            }
        }
    }
    add_action( 'save_post', 'save_custom_noindex' );
}

 

編集画面にnoindex設定を追加 ( カスタム投稿に対応 )

HTMLソースにてnoindex確認

<html lang="ja">
<head >
<meta charset="UTF-8">
<meta name="description" content="test...">
<meta name="viewport" content="width=device-width">
<title>...</title>
<meta name='robots' content='noindex, follow' />
<link rel='dns-prefetch' href='//stats.wp.com' />

 

チェックを入れて保存すると
<meta name=’robotscontent=’noindex, follow‘ />
がソースに表示されるようになります

カスタム投稿の編集画面に追加する場合

// 固定ページ、投稿ページの場合
array('post', 'page')

//  固定ページ、投稿ページ、カスタム投稿ページを設定する場合
array('post', 'page', '例)news', 'カスタム投稿のpost_typeを指定')

noindex、nofollowについて

noindex

検索結果に表示させないように制御する設定

nofollow

ページに存在するリンクを検索エンジンがたどることを禁止する設定

max-image-preview

Discover など Google のプロダクトで表示されるサムネイル画像の大きさを指定

WordPress Developer Resources

関連機能

  1. 日本語を含むファイル名のファイルをアップロードした時に自動的にリネームさせる

  2. Smart Custom Fieldsの定義をfunctions.phpで行う

  3. WordPress カテゴリーごとにテキスト色、背景色の設定が行えるように項目を追加する

サイドバー

よく使うカスタマイズ

最近の記事

アニメの名言集

服だけ溶かす薬。男ってのはね
こういうの渡しておけば喜ぶんだよ

葬送のフリーレン
by フリーレン

Profile

PAGE TOP
Amazon プライム対象