MENU

問い合わせ

    お問い合わせ


    【WordPressで作るキュレーションメディア 第9回】OGP・SNS カードと「広告・PR表記」― ステマ規制への対応

    (2026年10月29日 追記:記事どおりに再現できるよう、必要なコードと手順を追加しました)

    キュレーションメディアの記事は、SNS で共有されて読まれることが多くなります。共有されたときにタイトルや画像がきちんと表示されるかどうかは、OGP(=SNS に「このページのタイトル・説明・画像はこれ」と伝える meta タグ)で決まります。また、アフィリエイトリンクなどの広告を含む記事では、2023年10月1日から、広告であることが分かる表示がこれまで以上に重要になりました。いわゆる「ステマ規制」です。

    「SNS で共有したときの見た目を整えたい。あと、アフィリエイトの記事に PR 表記を入れたいけど、書き忘れが怖い…」

    結論から言うと、OGP は記事の情報から自動で作り、PR 表記は「広告を含む」のチェックと広告リンクの検出の両方をきっかけに、プラグインで自動挿入するのが確実です。表記をテーマや書き手の手作業に任せると、テーマの変更や書き忘れで消えてしまいます。ただし、表記の文言や位置が法令上十分かどうかは、コードでは判断できません。社内で決め、必要に応じて専門家に確認してください。

    前回の第8回では、目次と構造化データを作りました。第9回の今回は、OGP・X カードの出力と、広告・PR表記の仕組みをプラグインに追加します。

    目次

    今回のゴールと、ステマ規制の概要

    完成すると、次のようになります。

    • 記事ページとトップページの <head> に、OGP(og:title・og:description・og:image など)と X カードの meta タグが出る
    • 編集画面の「広告・PR表記」パネルで「この記事は広告を含む」をオンにすると、記事タイトルの直後に PR 表記が、記事一覧・ランキング・関連記事のタイトルの前に「PR」ラベルが出る
    • 設定画面に登録した広告リンクのドメインへのリンクには rel="sponsored" が付き、その記事はチェックを忘れていても PR 表記が出る

    ステマ規制で何が求められているか

    消費者庁のページでは、広告であるにもかかわらず広告であることを隠すことが「ステルスマーケティング」であり、令和5年10月1日から景品表示法違反になると説明されています。根拠は「一般消費者が事業者の表示であることを判別することが困難である表示」という告示(令和5年内閣府告示第19号)です。ポイントを整理すると次のとおりです。

    • 規制されるのは、広告であって、一般消費者が広告であることを分からないもの
    • 広告には、企業がインフルエンサーなどの第三者に依頼・指示するものも含まれる。アフィリエイトプログラムを使った表示も、運用基準の中で例として挙げられている
    • 規制の対象は、商品・サービスを供給する事業者(広告主)

    📰 出典:消費者庁「令和5年10月1日からステルスマーケティングは景品表示法違反となります。」

    規制の対象は広告主ですが、メディア側が表記をしなければ、広告主はメディアに広告を出せません。また、読者から見れば「広告なのに隠していた」メディアとして信頼を失います。キュレーションメディアにとって、PR 表記は運営の前提と考えておくのが安全です。

    運用基準から読み取れる「実装で気をつけること」

    告示の運用基準には、広告であることが明瞭になっていない例と、明瞭になっている例が挙げられています。そのうち、画面の作り方に関わるものを抜き出すと次のとおりです。

    運用基準で挙げられている例今回の実装での対応
    「広告」「宣伝」「プロモーション」「PR」といった文言による表示は、明瞭になっている例記事では「【広告】」で始まる文章、一覧では「PR」ラベルを表示
    視認しにくい末尾の位置にだけ表示する例は、明瞭になっていない記事タイトルの直後(本文より前)に表示
    周囲の文字より小さい、薄い色で認識しにくい例は、明瞭になっていない本文と同じ文字サイズ・太字、背景色と枠線付き。色は第1回でコントラスト比を確認した「注意・PR表記」の色
    他の情報に紛れ込ませる例は、明瞭になっていない独立した枠として表示し、ほかの注記とまとめない

    📰 出典:消費者庁「「一般消費者が事業者の表示であることを判別することが困難である表示」の運用基準」

    運用基準には「これらの文言を使っていても、表示内容全体から明瞭と認められない場合もある」という注意も書かれています。文言を入れれば必ず適法、というものではありません。個別の記事の判断は、社内のルールと専門家への確認で行ってください。

    今回追加・変更したファイル

    ファイル内容
    プラグイン includes/ogp.phpOGP・X カードの meta タグ出力(新規)
    プラグイン includes/pr-notice.php投稿メタ「広告を含む」、広告リンクのドメイン設定、PR 表記と rel=”sponsored”(新規)
    プラグイン editor/pr-panel.js・webpack.config.js編集画面の「広告・PR表記」パネル(新規)とビルド対象への追加
    プラグイン assets/pr-notice.cssPR 表記の見た目(新規)
    プラグイン blocks/ranking/render.php・blocks/related/render.php一覧に「PR」ラベルを追加
    プラグイン curation-tools.php読み込み、バージョン 0.9.0

    企画の段階ではテーマに PR 表記のパターン(patterns/pr-notice.php)を置く予定でしたが、表記はテーマを替えても消えてはいけないため、表示も CSS もプラグイン側に置きました。今回、テーマの変更はありません。

    OGP と X カードを記事の情報から作る

    wp-content/plugins/curation-tools/includes/ogp.php(curation_tools_print_ogp() の抜粋。出力を止めるフィルターの判定と、トップページ用の配列の中身を省略)

    if ( is_singular( 'post' ) ) {
    	$post = get_queried_object();
    	if ( ! $post instanceof WP_Post ) {
    		return;
    	}
    	$tags = array(
    		'og:type'        => 'article',
    		'og:title'       => curation_tools_plain_text( get_the_title( $post ) ),
    		'og:description' => curation_tools_get_description( $post ),
    		'og:url'         => get_permalink( $post ),
    	);
    } elseif ( is_front_page() ) {
    	// トップページは og:type を website にし、サイト名とキャッチフレーズを使う
    }
    
    // (中略: og:site_name・og:locale・og:image・公開日と更新日を追加)
    
    // 横長の画像があれば大きい画像のカード、なければ小さいカードにする。
    $large = $image && $image['width'] >= 600 && $image['width'] > $image['height'];
    echo '<meta name="twitter:card" content="' . esc_attr( $large ? 'summary_large_image' : 'summary' ) . '" />' . "\n";
    
    foreach ( $tags as $property => $content ) {
    	if ( '' === $content ) {
    		continue;
    	}
    	$value = in_array( $property, array( 'og:url', 'og:image' ), true ) ? esc_url( $content ) : esc_attr( $content );
    	printf( '<meta property="%1$s" content="%2$s" />' . "\n", esc_attr( $property ), $value );
    }

    OGP の基本となるプロパティは og:title・og:type・og:image・og:url の4つです。これに説明文(og:description)とサイト名(og:site_name)を加えています。

    📰 出典:The Open Graph protocol

    X(旧 Twitter)のカードは、twitter:card でカードの種類を指定し、タイトルや画像は og: のタグがあればそれが使われます。そこで X 専用のタグは twitter:card だけにして、二重管理を避けました。

    📰 出典:X Developer Platform「Cards markup」

    説明文と画像のフォールバック(代わりに使うもの)

    • 説明文:手動の抜粋があればそれを使い、なければ本文の先頭から120文字で作ります。get_the_excerpt() は本文の表示用の処理を通るため使わず、excerpt_remove_blocks() で文章系のブロックだけを残してから、タグを除いて切り詰めています。
    • パスワード保護の記事:本文の内容が SNS に漏れないよう、説明文を空にします。
    • 画像:アイキャッチがあればその large サイズ、なければ「サイトアイコン」(ブラウザのタブなどに表示される、サイトの正方形の画像)を使います。どちらもなければ画像のタグは出しません。

    includes/ogp.php(curation_tools_get_description() の抜粋)

    if ( post_password_required( $post ) ) {
    	return '';
    }
    $text = has_excerpt( $post ) ? $post->post_excerpt : excerpt_remove_blocks( $post->post_content );
    $text = curation_tools_plain_text( strip_shortcodes( $text ) );
    $text = trim( preg_replace( '/\s+/u', ' ', $text ) );
    if ( mb_strlen( $text ) > $length ) {
    	$text = mb_substr( $text, 0, $length - 1 ) . '…';
    }
    return $text;

    SEO 系のプラグインを入れると、同じ OGP が二重に出力されることがあります。その場合は、フィルター curation_tools_output_ogp で false を返すと、このプラグインの出力を止められるようにしました。

    includes/ogp.php の全文です。上の2つの抜粋で省略した、出力を止めるフィルターの判定とトップページ用の値、画像を選ぶ処理を含みます。

    wp-content/plugins/curation-tools/includes/ogp.php の全文(130行・新規)
    <?php
    /**
     * OGP / X(旧 Twitter)カード: SNS で共有されたときのタイトル・説明・画像を <head> に出力する。
     *
     * 対象は記事ページとトップページ。SEO プラグインなど、ほかの仕組みで OGP を出す場合は
     * フィルター curation_tools_output_ogp で false を返して止める(二重出力を避ける)。
     * 画像や説明文を外部から取得する通信は行わない。
     *
     * @package Curation_Tools
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    /**
     * 記事の説明文を作る。手動の抜粋があればそれを、なければ本文の先頭から作る。
     *
     * get_the_excerpt() は本文を表示用に組み立てる処理(the_content)を通るため使わず、
     * excerpt_remove_blocks() で文章系のブロックだけを残してから、タグを除いて切り詰める。
     * パスワード保護の記事は、本文の内容が漏れないよう説明文を空にする。
     *
     * @param WP_Post $post   投稿。
     * @param int     $length 最大文字数。
     * @return string
     */
    function curation_tools_get_description( WP_Post $post, $length = 120 ) {
    	if ( post_password_required( $post ) ) {
    		return '';
    	}
    	$text = has_excerpt( $post ) ? $post->post_excerpt : excerpt_remove_blocks( $post->post_content );
    	$text = curation_tools_plain_text( strip_shortcodes( $text ) );
    	$text = trim( preg_replace( '/\s+/u', ' ', $text ) );
    	if ( mb_strlen( $text ) > $length ) {
    		$text = mb_substr( $text, 0, $length - 1 ) . '…';
    	}
    	return $text;
    }
    
    /**
     * 共有用の画像(URL・幅・高さ)を返す。アイキャッチ → サイトアイコンの順に探す。
     *
     * @param WP_Post|null $post 投稿(トップページでは null)。
     * @return array{url: string, width: int, height: int}|null
     */
    function curation_tools_get_share_image( $post ) {
    	if ( $post && has_post_thumbnail( $post ) ) {
    		$src = wp_get_attachment_image_src( get_post_thumbnail_id( $post ), 'large' );
    		if ( $src ) {
    			return array(
    				'url'    => $src[0],
    				'width'  => (int) $src[1],
    				'height' => (int) $src[2],
    			);
    		}
    	}
    	$icon = get_site_icon_url( 512 );
    	if ( $icon ) {
    		return array(
    			'url'    => $icon,
    			'width'  => 512,
    			'height' => 512,
    		);
    	}
    	return null;
    }
    
    /**
     * OGP と X カードの meta タグを出力する。
     */
    function curation_tools_print_ogp() {
    	/**
    	 * OGP を出力するかどうか。SEO プラグイン等で出力する場合は false にする。
    	 *
    	 * @param bool $enabled 出力するなら true。
    	 */
    	if ( ! apply_filters( 'curation_tools_output_ogp', true ) ) {
    		return;
    	}
    
    	$post = null;
    	if ( is_singular( 'post' ) ) {
    		$post = get_queried_object();
    		if ( ! $post instanceof WP_Post ) {
    			return;
    		}
    		$tags = array(
    			'og:type'        => 'article',
    			'og:title'       => curation_tools_plain_text( get_the_title( $post ) ),
    			'og:description' => curation_tools_get_description( $post ),
    			'og:url'         => get_permalink( $post ),
    		);
    	} elseif ( is_front_page() ) {
    		$tags = array(
    			'og:type'        => 'website',
    			'og:title'       => get_bloginfo( 'name' ),
    			'og:description' => get_bloginfo( 'description' ),
    			'og:url'         => home_url( '/' ),
    		);
    	} else {
    		return;
    	}
    
    	$tags['og:site_name'] = get_bloginfo( 'name' );
    	$tags['og:locale']    = 'ja_JP';
    
    	$image = curation_tools_get_share_image( $post );
    	if ( $image ) {
    		$tags['og:image']        = $image['url'];
    		$tags['og:image:width']  = (string) $image['width'];
    		$tags['og:image:height'] = (string) $image['height'];
    	}
    	if ( $post ) {
    		$tags['article:published_time'] = get_the_date( DATE_W3C, $post );
    		$tags['article:modified_time']  = get_the_modified_date( DATE_W3C, $post );
    	}
    
    	// 横長の画像があれば大きい画像のカード、なければ小さいカードにする。
    	$large = $image && $image['width'] >= 600 && $image['width'] > $image['height'];
    	echo '<meta name="twitter:card" content="' . esc_attr( $large ? 'summary_large_image' : 'summary' ) . '" />' . "\n";
    
    	foreach ( $tags as $property => $content ) {
    		if ( '' === $content ) {
    			continue;
    		}
    		$value = in_array( $property, array( 'og:url', 'og:image' ), true ) ? esc_url( $content ) : esc_attr( $content );
    		printf( '<meta property="%1$s" content="%2$s" />' . "\n", esc_attr( $property ), $value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- 直前で esc_url / esc_attr 済み。
    	}
    }
    add_action( 'wp_head', 'curation_tools_print_ogp', 5 );

    「広告を含む」の設定と、広告リンクの自動検出

    PR 表記を出すかどうかは、次の2つで判断します。

    きっかけ設定する場所目的
    投稿メタ _curation_has_ad編集画面の「広告・PR表記」パネル書き手・編集者が明示的に「広告を含む」と宣言する
    本文に広告リンクがある設定 > 表示設定「広告リンクのドメイン」チェックの付け忘れがあっても表記が出るようにする

    投稿メタは第6回の監修者と同じく register_post_meta() で登録し、auth_callback で「その投稿を編集できる人だけが変更できる」ようにしています。

    includes/pr-notice.php(curation_tools_post_has_ad() の抜粋)

    $has_ad = (bool) get_post_meta( $post_id, '_curation_has_ad', true );
    $hosts  = (array) get_option( 'curation_tools_sponsored_hosts', array() );
    if ( ! $has_ad && $hosts ) {
    	$processor = new WP_HTML_Tag_Processor( (string) get_post_field( 'post_content', $post_id ) );
    	while ( $processor->next_tag( 'A' ) ) {
    		$href = $processor->get_attribute( 'href' );
    		if ( is_string( $href ) && curation_tools_is_sponsored_url( $href, $hosts ) ) {
    			$has_ad = true;
    			break;
    		}
    	}
    }

    広告リンクのドメインは、register_setting() で表示設定のページに入力欄を追加しました。保存は WordPress の設定 API が行うため、nonce(=正規の画面から送信されたことの確認)と管理者権限(manage_options)の確認は WordPress に任せられます。入力値は sanitize_callback で「小文字のホスト名だけ」に整えます(https:// やパスは取り除き、使えない文字を含む行は捨てる)。

    📰 出典:WordPress Developer Resources「register_setting()」

    PR 表記をタイトルの直後と一覧に出す

    表記は、記事タイトル(post-title ブロック)の表示に render_block_core/post-title フィルターで追加します。第3回で作った3種類の記事テンプレートすべてでタイトルは post-title ブロックなので、テンプレートを1つずつ直す必要がありません。

    includes/pr-notice.php(curation_tools_add_pr_notice_to_title() の抜粋)

    $post_id = isset( $instance->context['postId'] ) ? (int) $instance->context['postId'] : 0;
    if ( ! $post_id || 'post' !== get_post_type( $post_id ) || ! curation_tools_post_has_ad( $post_id ) ) {
    	return $content;
    }
    wp_enqueue_style( 'curation-tools-pr-notice' );
    
    // 表示中の記事そのもののタイトルなら文章の表記、それ以外(一覧の中のタイトル)なら「PR」ラベル。
    if ( is_singular( 'post' ) && get_queried_object_id() === $post_id ) {
    	return $content . '<p class="curation-pr-notice" role="note">' . esc_html( curation_tools_pr_notice_text() ) . '</p>';
    }
    return '<p class="curation-pr-label">PR</p>' . $content;

    表記の文言は既定で「【広告】この記事にはアフィリエイトリンクなどの広告が含まれます。リンク先で商品・サービスを申し込むと、当サイトが報酬を受け取ることがあります。」とし、フィルター curation_tools_pr_notice_text で変更できます。文言は社内で決めて差し替えてください。

    CSS はプラグインの assets/pr-notice.css に置き、PR 表記を出すページでだけ読み込みます。色はテーマの「注意・PR表記」の色(第1回)を使い、テーマにその色がない場合でも既定の色で表示されるようにしています。

    広告リンクに rel=”sponsored” を付ける

    Google は、広告や有料のリンクに rel="sponsored" を付けることを推奨しています。本文の表示時に、登録したドメインへのリンクにだけ sponsored(と noopener)を追加します。書き手が付けた nofollow などの既存の値は残します。

    📰 出典:Google 検索セントラル「外部リンクの rel 属性(nofollow/ugc/sponsored)」

    pr-notice.php の全文と、編集画面のパネル

    includes/pr-notice.php の全文です。上の抜粋のほか、投稿メタ _curation_has_ad と設定「広告リンクのドメイン」の登録、表示設定の画面の入力欄、rel="sponsored" を付ける処理、編集画面のパネルの読み込みを含みます。

    wp-content/plugins/curation-tools/includes/pr-notice.php の全文(237行・新規)
    <?php
    /**
     * 広告・PR表記: 「広告を含む」記事に PR 表記を自動で表示し、広告リンクに rel="sponsored" を付ける。
     *
     * - 投稿メタ _curation_has_ad(エディターの「広告・PR表記」パネルでオン/オフ)
     * - 設定「広告リンクのドメイン」(設定 > 表示設定)に一致するリンクが本文にある記事も、
     *   メタがオフでも「広告を含む」とみなす(表記の付け忘れを防ぐ)
     * - 表記はテーマではなくプラグインで出す(テーマを替えても表記が消えないように)
     *
     * 表記の文言・位置が法令上十分かどうかの判断はこのコードでは行わない。社内(必要に応じて専門家)で決める。
     *
     * @package Curation_Tools
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    /**
     * 投稿メタ「広告を含む」と、設定「広告リンクのドメイン」を登録する。
     */
    function curation_tools_register_pr_settings() {
    	register_post_meta(
    		'post',
    		'_curation_has_ad',
    		array(
    			'type'              => 'boolean',
    			'description'       => '広告(アフィリエイトリンク等)を含む記事なら true',
    			'single'            => true,
    			'default'           => false,
    			'show_in_rest'      => true,
    			'sanitize_callback' => 'rest_sanitize_boolean',
    			'auth_callback'     => static function ( $allowed, $meta_key, $post_id ) {
    				return current_user_can( 'edit_post', $post_id );
    			},
    		)
    	);
    
    	register_setting(
    		'reading',
    		'curation_tools_sponsored_hosts',
    		array(
    			'type'              => 'array',
    			'description'       => '広告リンクとして扱うドメインの一覧',
    			'default'           => array(),
    			'sanitize_callback' => 'curation_tools_sanitize_hosts',
    			'show_in_rest'      => false,
    		)
    	);
    }
    add_action( 'init', 'curation_tools_register_pr_settings' );
    
    /**
     * ドメインの一覧(改行区切りの文字列または配列)を、小文字のホスト名の配列にする。
     *
     * @param mixed $value 入力値。
     * @return string[]
     */
    function curation_tools_sanitize_hosts( $value ) {
    	$lines = is_array( $value ) ? $value : preg_split( '/[\r\n,]+/', (string) $value );
    	$hosts = array();
    	foreach ( $lines as $line ) {
    		$line = strtolower( trim( sanitize_text_field( $line ) ) );
    		$line = preg_replace( '#^https?://#', '', $line );
    		$line = preg_replace( '#[/?\#].*$#', '', $line );
    		if ( '' !== $line && preg_match( '/^[a-z0-9.-]+$/', $line ) ) {
    			$hosts[] = $line;
    		}
    	}
    	return array_values( array_unique( $hosts ) );
    }
    
    /**
     * 設定 > 表示設定 に「広告リンクのドメイン」の入力欄を追加する。
     *
     * 保存は WordPress の設定 API(options.php)が行い、nonce と manage_options の確認も任せる。
     */
    function curation_tools_add_pr_settings_field() {
    	add_settings_field(
    		'curation_tools_sponsored_hosts',
    		'広告リンクのドメイン',
    		static function () {
    			$hosts = (array) get_option( 'curation_tools_sponsored_hosts', array() );
    			printf(
    				'<textarea name="curation_tools_sponsored_hosts" id="curation_tools_sponsored_hosts" rows="4" class="large-text code">%s</textarea>',
    				esc_textarea( implode( "\n", $hosts ) )
    			);
    			echo '<p class="description">1行に1つ(例: affiliate.example.com)。このドメインへの本文中のリンクに rel="sponsored" を付け、その記事に PR 表記を出します。</p>';
    		},
    		'reading',
    		'default',
    		array( 'label_for' => 'curation_tools_sponsored_hosts' )
    	);
    }
    add_action( 'admin_init', 'curation_tools_add_pr_settings_field' );
    
    /**
     * URL のホストが広告リンクのドメイン(またはそのサブドメイン)かどうか。
     *
     * @param string   $url   URL。
     * @param string[] $hosts ドメインの一覧。
     * @return bool
     */
    function curation_tools_is_sponsored_url( $url, $hosts ) {
    	$host = strtolower( (string) wp_parse_url( $url, PHP_URL_HOST ) );
    	if ( '' === $host ) {
    		return false;
    	}
    	foreach ( $hosts as $sponsored ) {
    		if ( $host === $sponsored || str_ends_with( $host, '.' . $sponsored ) ) {
    			return true;
    		}
    	}
    	return false;
    }
    
    /**
     * 記事が広告を含むかどうか(メタがオン、または本文に広告リンクがある)。
     *
     * @param int $post_id 投稿ID。
     * @return bool
     */
    function curation_tools_post_has_ad( $post_id ) {
    	static $cache = array();
    	$post_id      = (int) $post_id;
    	if ( isset( $cache[ $post_id ] ) ) {
    		return $cache[ $post_id ];
    	}
    
    	$has_ad = (bool) get_post_meta( $post_id, '_curation_has_ad', true );
    	$hosts  = (array) get_option( 'curation_tools_sponsored_hosts', array() );
    	if ( ! $has_ad && $hosts ) {
    		$processor = new WP_HTML_Tag_Processor( (string) get_post_field( 'post_content', $post_id ) );
    		while ( $processor->next_tag( 'A' ) ) {
    			$href = $processor->get_attribute( 'href' );
    			if ( is_string( $href ) && curation_tools_is_sponsored_url( $href, $hosts ) ) {
    				$has_ad = true;
    				break;
    			}
    		}
    	}
    
    	$cache[ $post_id ] = $has_ad;
    	return $has_ad;
    }
    
    /**
     * PR 表記の文言。
     *
     * @return string
     */
    function curation_tools_pr_notice_text() {
    	/**
    	 * 記事冒頭の PR 表記の文言。
    	 *
    	 * @param string $text 文言。
    	 */
    	return (string) apply_filters( 'curation_tools_pr_notice_text', '【広告】この記事にはアフィリエイトリンクなどの広告が含まれます。リンク先で商品・サービスを申し込むと、当サイトが報酬を受け取ることがあります。' );
    }
    
    /**
     * PR 表記の CSS を登録する(使うときだけ読み込む)。
     */
    function curation_tools_register_pr_style() {
    	wp_register_style( 'curation-tools-pr-notice', CURATION_TOOLS_URL . 'assets/pr-notice.css', array(), CURATION_TOOLS_VERSION );
    }
    add_action( 'init', 'curation_tools_register_pr_style' );
    
    /**
     * 記事タイトル(post-title ブロック)に PR 表記を付ける。
     *
     * 記事ページではタイトルの直後に文章の表記を、一覧(クエリーループ)ではタイトルの前に「PR」ラベルを出す。
     * post-title ブロックはクエリーループの情報(queryId)を受け取らないため、表示中の記事かどうかで分ける。
     *
     * @param string   $content  ブロックの HTML。
     * @param array    $block    ブロックの情報。
     * @param WP_Block $instance ブロックのインスタンス。
     * @return string
     */
    function curation_tools_add_pr_notice_to_title( $content, $block, $instance ) {
    	$post_id = isset( $instance->context['postId'] ) ? (int) $instance->context['postId'] : 0;
    	if ( ! $post_id || 'post' !== get_post_type( $post_id ) || ! curation_tools_post_has_ad( $post_id ) ) {
    		return $content;
    	}
    	wp_enqueue_style( 'curation-tools-pr-notice' );
    
    	// 表示中の記事そのもののタイトルなら文章の表記、それ以外(一覧の中のタイトル)なら「PR」ラベル。
    	if ( is_singular( 'post' ) && get_queried_object_id() === $post_id ) {
    		return $content . '<p class="curation-pr-notice" role="note">' . esc_html( curation_tools_pr_notice_text() ) . '</p>';
    	}
    	return '<p class="curation-pr-label">PR</p>' . $content;
    }
    add_filter( 'render_block_core/post-title', 'curation_tools_add_pr_notice_to_title', 10, 3 );
    
    /**
     * 本文中の広告リンクに rel="sponsored" を付ける(既存の rel の値は残す)。
     *
     * @param string $content 本文の HTML。
     * @return string
     */
    function curation_tools_add_sponsored_rel( $content ) {
    	$hosts = (array) get_option( 'curation_tools_sponsored_hosts', array() );
    	if ( ! $hosts || false === stripos( $content, '<a' ) ) {
    		return $content;
    	}
    
    	$processor = new WP_HTML_Tag_Processor( $content );
    	while ( $processor->next_tag( 'A' ) ) {
    		$href = $processor->get_attribute( 'href' );
    		if ( ! is_string( $href ) || ! curation_tools_is_sponsored_url( $href, $hosts ) ) {
    			continue;
    		}
    		$rel    = $processor->get_attribute( 'rel' );
    		$tokens = is_string( $rel ) ? preg_split( '/\s+/', trim( $rel ) ) : array();
    		$tokens = array_filter( array_merge( $tokens, array( 'sponsored', 'noopener' ) ) );
    		$processor->set_attribute( 'rel', implode( ' ', array_unique( $tokens ) ) );
    	}
    	return $processor->get_updated_html();
    }
    add_filter( 'the_content', 'curation_tools_add_sponsored_rel', 21 );
    
    /**
     * 投稿の編集画面に「広告・PR表記」パネル(editor/pr-panel.js のビルド結果)を読み込む。
     */
    function curation_tools_enqueue_pr_panel() {
    	$screen = get_current_screen();
    	if ( ! $screen || 'post' !== $screen->base || 'post' !== $screen->post_type ) {
    		return;
    	}
    	$asset_file = CURATION_TOOLS_DIR . 'build/pr-panel.asset.php';
    	if ( ! file_exists( $asset_file ) ) {
    		return;
    	}
    	$asset = require $asset_file;
    	wp_enqueue_script( 'curation-tools-pr-panel', CURATION_TOOLS_URL . 'build/pr-panel.js', $asset['dependencies'], $asset['version'], true );
    }
    add_action( 'enqueue_block_editor_assets', 'curation_tools_enqueue_pr_panel' );

    wp-content/plugins/curation-tools/assets/pr-notice.css(新規・全文)

    /*
     * 広告・PR表記(プラグインで読み込む。テーマを替えても表記が見えるように、色はテーマの値がなければ既定値を使う)
     *
     * 表記が「小さい・薄い・目立たない」ものにならないよう、本文と同じ文字サイズで、背景色と枠線を付ける。
     */
    .curation-pr-notice {
    	margin-block: var(--wp--preset--spacing--30, 1rem);
    	padding: var(--wp--preset--spacing--20, 0.5rem) var(--wp--preset--spacing--30, 1rem);
    	border-left: 4px solid var(--wp--preset--color--notice, #8a5300);
    	background: var(--wp--preset--color--notice-bg, #fff4e0);
    	color: var(--wp--preset--color--notice, #8a5300);
    	font-size: inherit;
    	font-weight: 700;
    }
    
    .curation-pr-label {
    	display: inline-block;
    	margin: 0 0.5em 0.25em 0;
    	padding: 0 0.5em;
    	border: 1px solid var(--wp--preset--color--notice, #8a5300);
    	background: var(--wp--preset--color--notice-bg, #fff4e0);
    	color: var(--wp--preset--color--notice, #8a5300);
    	font-size: 0.875em;
    	font-weight: 700;
    	line-height: 1.6;
    }

    編集画面の「広告・PR表記」パネルです。第6回の「監修」パネルと同じ仕組み(PluginDocumentSettingPanel)で、オン・オフの切り替え(ToggleControl)1つを投稿メタに保存します。

    wp-content/plugins/curation-tools/editor/pr-panel.js(新規・全文)

    /**
     * 投稿の編集画面(右側の「投稿」タブ)に「広告・PR表記」パネルを追加する。
     *
     * オンにすると投稿メタ _curation_has_ad が true になり、公開ページのタイトル直後と一覧に PR 表記が出る。
     */
    import { Notice, ToggleControl } from '@wordpress/components';
    import { useSelect } from '@wordpress/data';
    import { useEntityProp } from '@wordpress/core-data';
    import {
    	PluginDocumentSettingPanel,
    	store as editorStore,
    } from '@wordpress/editor';
    import { registerPlugin } from '@wordpress/plugins';
    
    function PrPanel() {
    	const postType = useSelect(
    		( select ) => select( editorStore ).getCurrentPostType(),
    		[]
    	);
    	const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' );
    
    	if ( 'post' !== postType || ! meta ) {
    		return null;
    	}
    
    	const hasAd = !! meta._curation_has_ad;
    
    	return (
    		<PluginDocumentSettingPanel name="curation-pr" title="広告・PR表記">
    			<ToggleControl
    				__nextHasNoMarginBottom
    				label="この記事は広告を含む"
    				help="アフィリエイトリンク、広告主からの依頼・商品提供を受けた紹介などを含む場合にオンにします。"
    				checked={ hasAd }
    				onChange={ ( value ) =>
    					setMeta( { ...meta, _curation_has_ad: value } )
    				}
    			/>
    			{ hasAd && (
    				<Notice status="info" isDismissible={ false }>
    					公開ページのタイトルの直後と記事一覧に PR
    					表記が表示されます。
    				</Notice>
    			) }
    		</PluginDocumentSettingPanel>
    	);
    }
    
    registerPlugin( 'curation-pr', { render: PrPanel } );

    このパネルもビルドの対象に加えます。wp-content/plugins/curation-tools/webpack.config.js に次の変更を加えます(+ が追加、- が削除)。

    --- a/wp-content/plugins/curation-tools/webpack.config.js
    +++ b/wp-content/plugins/curation-tools/webpack.config.js
    @@ -2,7 +2,7 @@
      * @wordpress/scripts の標準設定に、ブロック以外のエディター用 JS(監修パネル)を追加する。
      *
      * 標準設定は --webpack-src-dir(blocks/)の block.json からエントリーを自動で探す。
    - * ここではその結果に editor/ のパネル(監修)を足している。
    + * ここではその結果に editor/ のパネル(監修・広告PR表記)を足している。
      */
     const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
     
    @@ -11,5 +11,6 @@ module.exports = {
     	entry: {
     		...defaultConfig.entry(),
     		'supervisor-panel': './editor/supervisor-panel.js',
    +		'pr-panel': './editor/pr-panel.js',
     	},
     };

    ランキングと関連記事の一覧にも「PR」ラベルを出します。

    wp-content/plugins/curation-tools/blocks/ranking/render.php に次の変更を加えます(+ が追加、- が削除)。

    --- a/wp-content/plugins/curation-tools/blocks/ranking/render.php
    +++ b/wp-content/plugins/curation-tools/blocks/ranking/render.php
    @@ -29,6 +29,10 @@ if ( empty( $curation_ids ) ) {
     	<ol>
     		<?php foreach ( $curation_ids as $curation_id ) : ?>
     			<li>
    +				<?php if ( curation_tools_post_has_ad( $curation_id ) ) : ?>
    +					<?php wp_enqueue_style( 'curation-tools-pr-notice' ); ?>
    +					<span class="curation-pr-label">PR</span>
    +				<?php endif; ?>
     				<a href="<?php echo esc_url( get_permalink( $curation_id ) ); ?>"><?php echo esc_html( get_the_title( $curation_id ) ); ?></a>
     			</li>
     		<?php endforeach; ?>

    wp-content/plugins/curation-tools/blocks/related/render.php に次の変更を加えます(+ が追加、- が削除)。

    --- a/wp-content/plugins/curation-tools/blocks/related/render.php
    +++ b/wp-content/plugins/curation-tools/blocks/related/render.php
    @@ -24,6 +24,10 @@ if ( empty( $curation_ids ) ) {
     	<ul>
     		<?php foreach ( $curation_ids as $curation_id ) : ?>
     			<li>
    +				<?php if ( curation_tools_post_has_ad( $curation_id ) ) : ?>
    +					<?php wp_enqueue_style( 'curation-tools-pr-notice' ); ?>
    +					<span class="curation-pr-label">PR</span>
    +				<?php endif; ?>
     				<a href="<?php echo esc_url( get_permalink( $curation_id ) ); ?>"><?php echo esc_html( get_the_title( $curation_id ) ); ?></a>
     			</li>
     		<?php endforeach; ?>

    プラグイン本体で2つのファイルを読み込み、バージョンを 0.9.0 にします。wp-content/plugins/curation-tools/curation-tools.php に次の変更を加えます(+ が追加、- が削除)。

    --- a/wp-content/plugins/curation-tools/curation-tools.php
    +++ b/wp-content/plugins/curation-tools/curation-tools.php
    @@ -2,7 +2,7 @@
     /**
      * Plugin Name:       Curation Tools
      * Description:       キュレーションメディア用の小さな機能群(引用元カード・ランキング・関連記事・構造化データなど)。連載の各回で機能を追加していく。
    - * Version:           0.8.0
    + * Version:           0.9.0
      * Requires at least: 6.8
      * Requires PHP:      8.2
      * Author:            third-hero
    @@ -17,7 +17,7 @@ if ( ! defined( 'ABSPATH' ) ) {
     	exit;
     }
     
    -define( 'CURATION_TOOLS_VERSION', '0.8.0' );
    +define( 'CURATION_TOOLS_VERSION', '0.9.0' );
     define( 'CURATION_TOOLS_FILE', __FILE__ );
     define( 'CURATION_TOOLS_DIR', plugin_dir_path( __FILE__ ) );
     define( 'CURATION_TOOLS_URL', plugin_dir_url( __FILE__ ) );
    @@ -33,6 +33,8 @@ require_once CURATION_TOOLS_DIR . 'includes/ranking.php';
     require_once CURATION_TOOLS_DIR . 'includes/related.php';
     require_once CURATION_TOOLS_DIR . 'includes/toc.php';
     require_once CURATION_TOOLS_DIR . 'includes/schema.php';
    +require_once CURATION_TOOLS_DIR . 'includes/ogp.php';
    +require_once CURATION_TOOLS_DIR . 'includes/pr-notice.php';
     
     /**
      * ブロックを登録する。

    動作確認の方法

    第0回・第2回の手順の後、プラグインをビルド(プラグインのフォルダで npm run build)してから確認しました(筆者の環境で確認済みです)。

    # OGP と X カードの meta タグを確認する
    curl -s http://localhost:8088/<記事のスラッグ>/ | grep -E 'og:(title|description|image|url|type)|twitter:card'
    # 記事に「広告を含む」を設定する(編集画面のパネルと同じ)
    docker compose run --rm wpcli wp post meta update <記事ID> _curation_has_ad 1
    • アイキャッチ(横長)のある記事は summary_large_image、ない記事は summary。説明文は抜粋または本文の先頭から作られる
    • パスワード保護の記事は og:description が出ない。タイトルに " や & を含む記事でも属性値が正しくエスケープされる
    • 「広告を含む」をオンにした記事でだけ、タイトルの直後に PR 表記が出る。トップページ・特集ページの一覧、ランキング・関連記事では、その記事のタイトルの前に「PR」ラベルが出る
    • 広告リンクのドメインを登録すると、本文中の該当リンクが rel="nofollow sponsored noopener" になり、チェックがオフの記事でも PR 表記が出る。ほかのドメインへのリンクは変わらない
    • 表示設定の入力欄に https://Aff2.Example.NET/path や不正な文字列を入れて保存すると、aff2.example.net だけが保存される
    • 編集画面の「広告・PR表記」パネルでスイッチをオンにして保存すると、投稿メタが保存される(ヘッドレスブラウザで確認)
    • 寄稿者のアカウント(アプリケーションパスワード)で、他人の記事の「広告を含む」を REST API から変えようとすると 403 になる

    実際の SNS(X・Facebook など)に URL を貼ったときのカード表示は、ローカル環境では確認できないため、公開環境の URL で確認してください。SNS 側はカードの情報をキャッシュするため、OGP を直しても表示がすぐ変わらないことがあります。

    つまずきやすい点・セキュリティ上の注意

    • 表記をテーマに置かない:テーマのテンプレートに表記を書くと、テーマの変更やテンプレートの編集で消えてしまいます。法令や信頼に関わる表示は、機能としてプラグインに置きましょう。
    • 「広告を含む」の判断基準を決める:アフィリエイトリンクだけでなく、広告主から商品の提供を受けた紹介や、依頼を受けて書いた記事も対象になり得ます。どの場合にオンにするかを編集ルールに書いておきます。
    • ドメインの登録漏れ:自動検出は登録したドメインにしか効きません。短縮 URL や計測用のリダイレクト URL を使う広告は検出できないため、チェックの運用と併用します。
    • OGP の二重出力:SEO プラグインと併用する場合は、どちらか一方にします。

    発注者向けメモ:PR 表記は「文言」「位置」「付け忘れ防止」を要件にする

    ステマ規制への対応は、法務の判断と画面の実装の両方が必要です。開発会社に依頼するときは、次の点を要件として書いておきましょう。

    • 文言:記事の表記と一覧のラベルの文言。社内(必要に応じて専門家)で決めたものを渡す
    • 位置と見た目:本文より前に、本文と同じ大きさで、背景色などで区別して表示する
    • 付け忘れ防止:チェックの仕組みに加えて、広告リンクの自動検出や公開前のレビュー(第11回で扱います)を組み合わせる
    • テーマ変更への耐性:テーマを替えても表記が残る作りになっているか

    工数が増えるのは、広告の種類ごとに文言を変えたい場合、外部の広告配信サービスのタグを扱う場合、過去記事をまとめて見直す場合です。

    打ち合わせでは、次のように聞いてみてください。

    • 「PR 表記は、テーマを変更しても表示されますか? どこで出力していますか?」
    • 「書き手が『広告を含む』のチェックを忘れた場合、どうやって気づけますか?」
    • 「SNS で共有したときのタイトル・画像は、記事ごとにどこから取られますか? 画像がない記事ではどうなりますか?」

    まとめと次回予告

    第9回では、OGP・X カードと広告・PR表記を作りました。

    • OGP は記事のタイトル・抜粋・アイキャッチから自動で作り、パスワード保護の記事では説明文を出さない
    • PR 表記は、投稿メタのチェックと広告リンクの自動検出の両方をきっかけに、タイトルの直後と一覧に出す
    • 表記は本文と同じ大きさで、本文より前に、区別できる見た目で表示する(消費者庁の運用基準の例を参考にした)
    • 広告リンクには rel="sponsored" を付ける
    • 文言や位置が法令上十分かの判断は、コードではなく社内のルールと専門家への確認で行う

    次回は「表示速度の改善 ― 画像サイズ・キャッシュ・計測」です。画像サイズの整理、キャッシュの設定と、改善前後の計測方法を扱います。

    この連載の記事一覧

    この記事は連載「WordPressで作るキュレーションメディア」の1回です。連載のほかの回は次のとおりです(連載の一覧ページ)。

    システム制作・運用・保守のお問い合わせはこちら

      お問い合わせ


      よかったらシェアしてね!
      • URLをコピーしました!
      • URLをコピーしました!

      この記事を書いた人

      株式会社THIRD HERO代表取締役 朝野貴朗
      Webシステム開発を中心に、toC向けサービスサイトの運営、ツール開発などを行ってまいりました。

      コメント

      コメント一覧 (1件)

      目次