GRAPHNOTES

ACFを利用してシンプルにSEO管理を行う

日付:
2025年09月12日
カテゴリー:
Wordpress

All in One SEO Packは便利なSEOプラグインですが、サイトの規模によっては冗長的になりやすいです。

ACF Pro(Advenced Custom Fileld Pro)を使用しているならこちらで一本化するのも手です。
OGPやdescription、noindexをカスタムフィールドで制御します。

functions.phpで制御

PHP
//SEOをACFで管理する(フィールド登録)
if ( function_exists( 'acf_add_local_field_group' ) ) {
    acf_add_local_field_group( array(
        'key'                   => 'group_seo_meta_settings',
        'title'                 => 'SEO・OGP設定',
        'fields'                => array(
            array(
                'key'               => 'field_seo_title',
                'label'             => 'SEO タイトル',
                'name'              => 'seo_title',
                'type'              => 'text',
                'instructions'      => '未入力の場合は通常の投稿タイトルが使用されます。',
                'placeholder'       => '',
            ),
            array(
                'key'               => 'field_seo_description',
                'label'             => 'SEO ディスクリプション',
                'name'              => 'seo_description',
                'type'              => 'textarea',
                'instructions'      => '検索結果やSNSシェア時に表示される説明文を入力してください。',
                'rows'              => 3,
            ),
            array(
                'key'               => 'field_ogp_image',
                'label'             => 'OGP 画像',
                'name'              => 'ogp_image',
                'type'              => 'image',
                'instructions'      => '未入力の場合はアイキャッチ画像が使用されます。',
                'return_format'     => 'array',
                'preview_size'      => 'medium',
                'library'           => 'all',
            ),
            array(
                'key'               => 'field_noindex_flag',
                'label'             => '検索エンジン設定',
                'name'              => 'noindex_flag',
                'type'              => 'true_false',
                'message'           => 'このページを検索エンジンにインデックスさせない (noindex, follow)',
                'default_value'     => 0,
                'ui'                => 1,
            ),
        ),
        'location'              => array(
            array(
                // メディア(attachment)以外を指定
                array(
                    'param'    => 'post_type',
                    'operator' => '!=',
                    'value'    => 'attachment',
                ),
            ),
        ),
        'menu_order'            => 0,
        'position'              => 'normal', // 投稿エディタの下に配置
        'style'                 => 'default',
        'label_placement'       => 'top',
        'instruction_placement' => 'label',
        'active'                => true,
    ) );
}

//SEOをACFで管理する(出力)
// <title> に反映
add_filter( 'pre_get_document_title', function ( $title ) {
    if ( is_singular() && function_exists( 'get_field' ) ) {
        $seo_title = get_field( 'seo_title', get_queried_object_id() );
        if ( $seo_title ) {
            return wp_strip_all_tags( $seo_title );
        }
    }
    return $title;
} );

// noindex(WP 5.7+)
add_filter( 'wp_robots', function ( $robots ) {
    if ( is_singular() && function_exists( 'get_field' ) && get_field( 'noindex_flag', get_queried_object_id() ) ) {
        $robots['noindex'] = true;
        $robots['follow']  = true;
        unset( $robots['max-image-preview'] ); // 必要に応じて
    }
    return $robots;
} );

// サイトマップから除外
add_filter( 'wp_sitemaps_posts_query_args', function ( $args ) {
    $args['meta_query'] = array(
        'relation' => 'OR',
        array( 'key' => 'noindex_flag', 'compare' => 'NOT EXISTS' ),
        array( 'key' => 'noindex_flag', 'value' => '1', 'compare' => '!=' ),
    );
    return $args;
} );

function my_custom_seo_meta_tags() {
    if ( ! function_exists( 'get_field' ) ) return;

    // デフォルト値(アーカイブ等)
    $title       = wp_get_document_title();
    $description = get_bloginfo( 'description' );
    $url         = home_url( add_query_arg( array(), $GLOBALS['wp']->request ?? '' ) );
    $og_image    = get_theme_file_uri( 'images/default-ogp.png' ); // 固定画像に変更
    $type        = 'website';

    if ( is_singular() ) {
        $post_id = get_queried_object_id();

        $title = get_field( 'seo_title', $post_id ) ?: get_the_title( $post_id );

        $description = get_field( 'seo_description', $post_id );
        if ( ! $description ) {
            $post        = get_post( $post_id );
            $description = has_excerpt( $post_id )
                ? $post->post_excerpt
                : wp_trim_words( strip_shortcodes( $post->post_content ), 80, '…' );
        }

        $img = get_field( 'ogp_image', $post_id );
        if ( is_array( $img ) && ! empty( $img['url'] ) ) {
            $og_image = $img['url'];
        } elseif ( has_post_thumbnail( $post_id ) ) {
            $og_image = get_the_post_thumbnail_url( $post_id, 'full' );
        }

        $url  = get_permalink( $post_id );
        $type = is_front_page() ? 'website' : 'article';
    }

    $description = trim( wp_strip_all_tags( $description ) );

    if ( $description ) {
        echo '<meta name="description" content="' . esc_attr( $description ) . '" />' . "\n";
        echo '<meta property="og:description" content="' . esc_attr( $description ) . '" />' . "\n";
    }
    echo '<meta property="og:title" content="' . esc_attr( wp_strip_all_tags( $title ) ) . '" />' . "\n";
    echo '<meta property="og:type" content="' . esc_attr( $type ) . '" />' . "\n";
    echo '<meta property="og:url" content="' . esc_url( $url ) . '" />' . "\n";
    echo '<meta property="og:site_name" content="' . esc_attr( get_bloginfo( 'name' ) ) . '" />' . "\n";
    if ( $og_image ) {
        echo '<meta property="og:image" content="' . esc_url( $og_image ) . '" />' . "\n";
    }
    echo '<meta name="twitter:card" content="' . ( $og_image ? 'summary_large_image' : 'summary' ) . '" />' . "\n";
}
add_action( 'wp_head', 'my_custom_seo_meta_tags', 1 );

add_action( 'after_setup_theme', function () {
    add_theme_support( 'title-tag' );
} );

<head>に必ず<?php wp_head();?>を記載し、<title>は書かないようにしておきます。