ページ本文へ移動
炭火ブログ

MENU

ねこ ねこ

SEOプラグイン不要!WordPress構造化データ自作手順

WordPress用のSEOプラグインはどのような機能があるのかを、プラグインを導入して調べました。その中で構造化データというものが、ほぼすべてのSEOプラグインから出力されていてたので、これをプラグインを使わずに出力したいと思います。

構造化データを作成するにあたって、次のGoogleのガイドラインに沿って作成する必要があります。

Google 検索がサポートする構造化データ マークアップ

ページ数の少ない当ブログのようなサイトの場合、間違った書式で構造化データを出力するなら、何も出力しないほうが良いです。間違った構造化データは検索サイトによる評価を下げ、全体のアクセス数が減る可能性があるのでコードの利用には注意してください。

Amazonプライム感謝祭
目次
  1. コードを利用するための用意
  2. コードの使用方法
  3. コードの説明

コードを利用するための用意

コードが作動するように必要な画像の用意や情報の入力をします。

出力されるデータの中にユーザーの名前が含まれていますので「ブログ上の表示名」を変更し、公開できる名称に変更してください。同じく「サイト」プロフィールページのurlを記入して下さい。自己紹介ページが無い場合は下のコードから「'url'=>$author->user_url,」を削除して下さい。

コードの使用方法

次のコードを</body>の直前に追加して下さい。

<?php
if(is_single() || is_page('自己紹介ページ・サイト紹介ページのページスラッグ')):
   jsonld_data();
endif;
?>

次のコードを「functions.php」に追加して下さい。

<?php
function  jsonld_data()
{
    $jsonld = array();
    if (is_single()) {
        $categories = get_the_category();
        $category = $categories[0];
        $post_id = get_the_ID();

        // 投稿の抜粋を取得
        $description = get_the_excerpt();

        // 投稿者情報を取得
        global $post;
        $author = get_userdata($post->post_author);
        $thumbnail_url = '';
        if (has_post_thumbnail()) {
            $thumbnail_url = get_the_post_thumbnail_url($post_id, 'full');
        } else {
            $thumbnail_url = get_theme_file_uri('i/cha.webp');
        }
        //記事・パンくずリスト
        $jsonld = array(
            '@context' => 'https://schema.org',
            '@graph' => array(
                array(
                    '@type' => 'Article',
                    'headline' => get_the_title(),
                    'image' => array(
                        '@type' => 'ImageObject',
                        'url' => $thumbnail_url,
                    ),
                    'datePublished' => get_the_time('c'),
                    'dateModified' => get_the_modified_time('c'),
                    'author' => array(
                        '@type' => 'Person',
                        'name' => $author->display_name,
                        'url' => $author->user_url,
                    ),
                    'publisher' => array(
                        '@type' => 'Organization',
                        'name' => get_bloginfo('name'),
                        'url' => home_url('/'),
                    ),
                    'description' => $description,
                ),
                array(
                    '@type' => 'BreadcrumbList',
                    'name' => get_the_title(),
                    'itemListElement' => array(
                        array(
                            '@type' => 'ListItem',
                            'position' => 1,
                            'name' => esc_attr($category->name),
                            'item' => home_url('/category/') . esc_attr($category->slug) . '/',
                        ),
                        array(
                            '@type' => 'ListItem',
                            'position' => 2,
                            'name' => get_the_title(),
                        ),
                    ),
                ),
            ),
        );

        // サムネイル画像
        if (has_post_thumbnail()) {
            $jsonld['@graph'][] = array(
                '@type' => 'ImageObject',
                'contentUrl' => $thumbnail_url,
                'license' => home_url('ライセンスついて書かれているページのスラッグ'),
                'acquireLicensePage' => home_url('ライセンスを取得するための方法が書かれたページ・問い合わせページのスラッグ'),
                'creditText' => $author->user_url,
                'creator' => array(
                    '@type' => 'Person',
                    'name' => $author->display_name,
                ),
                'copyrightNotice' => $author->display_name,
            );
        }

        // 記事内画像
        $content_images = get_attached_media('image', $post_id);
        if ($content_images) {
            foreach ($content_images as $image) {
                $img_url = wp_get_attachment_image_src($image->ID, 'full')[0];
                $jsonld['@graph'][] = array(
                    '@type' => 'ImageObject',
                    'contentUrl' => $img_url,
                    'license' => home_url('ライセンスついて書かれているページのスラッグ'),
                    'acquireLicensePage' => home_url('ライセンスを取得するための方法が書かれたページ・問い合わせページのスラッグ'),
                    'creditText' => $author->user_url,
                    'creator' => array(
                        '@type' => 'Person',
                        'name' => $author->display_name,
                    ),
                    'copyrightNotice' => $author->display_name,
                );
            }
        }

        //プロフィール    
    } elseif (is_page('自己紹介ページ・サイト紹介ページのページスラッグ')) {
        global $post;
        $author = get_userdata($post->post_author);
        $jsonld = array(
            '@context' => 'https://schema.org',
            '@type' => 'ProfilePage',
            'dateCreated' => get_the_time('c'),
            'dateModified' => get_the_modified_time('c'),
            'mainEntity' => array(
                '@type' => 'Person',
                'name' => $author->display_name,
            ),
            'description' => get_bloginfo('name') . 'の管理者',
            'image' => array(
                '@type' => 'ImageObject',
                'url' = '管理者アバター画像のurl',
            ),
        );
    }
    echo '<script type="application/ld+json">';
    echo json_encode($jsonld, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    echo '</script>';
}

コードの説明

構造化データが出力されるのは投稿ページとプロフィールページです。投稿ページに「記事」「パンくずリスト」「画像メタデータ」、プロフィールページに「プロフィール」の構造化データが出力されます。

メタディスクリプションは「抜粋」を表示させています。投稿ページ内の「抜粋を入力 (任意)」に入力された文章がそのまま表示されます。未入力の場合は本文の最初から抜粋した文章を表示します。画像のサイズはアップロードしたサイズ「full」を表示させます。画像のサイズを変更したい場合は

$thumbnail_url = get_the_post_thumbnail_url($post_id, 'full');
$img_url = wp_get_attachment_image_src($image->ID, 'full')[0];

この行の'full'を変更してください。また、パーマリンクは最後に「/」が在ることを前提になっているので、ご自分の環境に合わせて正しく出力されるように調整してください。カテゴリーの階層は1階層のみに対応、複数カテゴリーには対応していません。

最後までお読みいただき、誠にありがとうございます。