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

広告WordPress・PHP を学べる本

Amazonのアソシエイトとして、WordPress functions.phpは適格販売により収入を得ています。

概要説明

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

functions.php
/*----------------------------------------------------
 head内にカスタム用のコードを追加する
----------------------------------------------------*/
if ( !function_exists( 'custom_meta_robots' ) ){
    function custom_meta_robots( array $robots ) {
        // 記事・固定ページを 1 つだけ表示しているときだけ(一覧のページには付けない)
        if ( is_singular() && get_post_meta( get_queried_object_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( 'add_meta_boxes', 'add_noindex_metabox' );

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

/*----------------------------------------------------
 カスタムフィールドの保存
----------------------------------------------------*/
if ( !function_exists( 'save_custom_noindex' ) ){
    function save_custom_noindex( $post_id ) {
        $key = 'noindex';
        // 自動保存・nonce がない・編集できない人のときは何もしない
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return;
        }
        if ( ! isset( $_POST['nonce_' . $key] ) || ! wp_verify_nonce( $_POST['nonce_' . $key], 'action_' . $key ) ) {
            return;
        }
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
        if ( isset( $_POST[$key] ) ) {
            update_post_meta( $post_id, $key, 'noindex' ); // 値は noindex だけ
        } else {
            delete_post_meta( $post_id, $key );
        }
    }
    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='robots' content='noindex, follow' />
がソースに表示されるようになります

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

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

noindex、nofollowについて

noindex

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

nofollow

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

max-image-preview

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

WordPress Developer Resources

関連機能

  1. WordPress のリビジョンの数を減らす・止める(wp-config.php と投稿タイプごとの設定・自動保存の間隔)

  2. WordPress アスキーアートや隠しメッセージをhtmlソースに追加する

  3. WordPressのバージョン情報を自動的に最新バージョンに偽装する

サイドバー

よく使うカスタマイズ

最近の記事

Profile

PAGE TOP