WordPress Yoast SEOプラグインのスキーマAPI

Last Updated:2019年12月15日| | コメントを残す

WordPress 人気SEOプラグインであるYoast SEOには、スキーママークアップを追加する機能が搭載されています。 しかしYoastでは BlogPosting マークアップはサポートされていません。 ここでYoastは次のように述べています。

We'd love to, but we've no way of automatically knowing whether a post (or, a post of a custom post type) should be considered a BlogPosting or not.

We'll be thinking about how we best present options for this, but in the meantime, you could use our API to alter the output – see https://developer.yoast.com/schema-documentation/api/

つまり、ポスト BlogPostingかどうかを自動的に知る方法がないと言います。 代わりに、Yoastが提供するYoast SEO Schema APIドキュメントを参照して出力を変更できます。

参考までに スキーマアプリの構造化データという無料のプラグインは BlogPostingをサポートしているので、投稿(記事)のタイプを 'BlogPosting'に設定したい場合は試してみてください。

  • ページ:http://schema.org/Article
  • 投稿(記事):http://schema.org/Blog転記
  • 検索:http://search.org/SearchResultsPage
  • 投稿者(作成者):http://schema.org/ProfilePage
  • カテゴリー:http://schema.org/CollectionPage
  • タグ:http://schema.org/CollectionPage
  • ブログ:http://schema.org/Blog
  • BreadcrumbList:http://schema.org/BreadcrumbList

スキーマというプラグインを使っても、ブログ記事のTypeが 'BlogPosting'と指定されます。 以外にも Schema&Structured Data for WP&AMP プラグインは、 BlogPostingをはじめ、33種のスキーマタイプをサポートしているそうですね。

Yoast SEOを使用している場合は、「Yoast SEO Schema API」の記事を参照して、Yoast SEOが提供するスキーママークアップを完全に削除し、スキーマ専用プラグインを試してみてください。

Yoast SEOスキーマを完全に削除

次のコードを使用しているテーマの関数ファイルに追加すると、すべてのYoast SEOのスキーマ出力が無効になります。

add_filter( 'wpseo_json_ld_output', '__return_false' );

グラフピース(graph piece)の追加または削除

Schema output for Yoast SEO 資料に記載されているように、いくつかのページに様々なグラフピースが出力されます。 一部を削除したり、追加したい場合は、 wpseo_schema_g​​raph_pieces フィルタを使用することができます。

example use-caseで例を見ることができます。 githubので深化された内容を確認することができます。

グラフピースのデータを変更する

特定のピースの出力を変更する場合 wpseo_schema_ フィルタを使用することができます。 例:

add_filter( 'wpseo_schema_article', 'example_change_article' );

/**
 * Change @type of Article Schema data.
 * Article Schema 데이터의 @type 변경
 *
 * @param array $data Schema.org Article data array.
 *
 * @return array $data Schema.org Article data array.
 */
function example_change_article( $data ) { 
  $data['@type'] = 'NonsensePage'; 
  return $data; 
}

スキーマに画像を追加

オブジェクト(object)にプログラム的に画像を追加するには、次のようなコードを使用します。

add_filter( 'wpseo_schema_article', 'example_change_article' );

/**
 * Add images to Article Schema data.
 * Article 스키마 데이터에 이미지 추가
 *
 * @param array $data Schema.org Article data array.
 *
 * @return array $data Schema.org Article data array.
 */
function example_change_article( $data ) { 
  // This is the attachment ID for our image.
  $image_id = 12345; 
  
  // We instantiate the image class, it always needs an $id, so the output can be referenced by other graph pieces.
  $id = "#image_12345";
  $schema_image = new WPSEO_Schema_Image( $id );

  // Now we just generate and add the image output.
  $data['image'] = $schema_image->generate_from_attachment_id( $image_id );

  return $data; 
}

Gutenberg (Gutenberg)ブロックのスキーマ

ブロックエディタ(」Gutenberg「)のブロックを作成する場合は、スキーマ出力も追加したいでしょう。この場合、XNUMXつの便利なフックを使用できます。

ページ上の任意のブロックがあるかを認識

まず、ページの任意のブロックがあるか知る必要があります。 wpseo_pre-schema_block-type_ アクションに知ることができます。 このアクションがブロックタイプをFireとブロックタイプがページに出力されます。

たとえば、このフィルタを使用して、簡単な次のような関数でFAQブロックで WebPage@タイプを変更することができます。

add_action( 'wpseo_pre_schema_block_type_yoast/faq-block', array( $this, 'prepare_faq_schema' ), 10, 1 );

/**
 * If this fires, we know there's an FAQ block on the page, so filter the page type.
 *
 * @param array $blocks The blocks of this type on the current page.
 */
public function prepare_schema( $blocks ) {
   $this->blocks    = $blocks;
   $this->is_needed = true;
   add_filter( 'wpseo_schema_webpage_type', array( $this, 'change_schema_page_type' ) );
}

このコードは、クラス内で取ってきたもので wpseo_pre-schema_block-type_ アクションは、そのタイプのすべてのブロックの配列(array)を買収(argument)にインポートします。 その後、Yoast SEOでこれらの情報をクラスに保存して、後でこのデータを使用して出力を決定します。

特定のブロックの出力

特定のブロックの出力を追加する場合 wpseo_schema_block_ フィルタを使用することができます。 このフィルタは、3つのパラメータがあります。

  • $ graph:返す必要がありすることブロックに基づいてグラフをフィルタリングできるようにする(which you have to return、which allows you to filter the graph based on your block)
  • $ block:このフィルタがファイア(fire)する対象のブロックのコンテンツ。
  • $ context:サイトのURL、Canonicalのなどの出力に使用できる環境変数の WPSEO_Schema_Context オブジェクトである

たとえば、FAQブロックを次のようにフックすることができます。

add_filter( 'wpseo_schema_block_yoast/faq-block', array( $this, 'render_schema_questions' ), 10, 3 );

それでは、このプラグインこのFAQブロックに会えば、次の関数が呼び出され、自主的に新しいクラスを呼び出します。

/**
 * Add the Questions in our FAQ blocks as separate pieces to the graph.
 *
 * @param array                 $graph   Schema data for the current page.
 * @param WP_Block_Parser_Block $block   The block data array.
 * @param WPSEO_Schema_Context  $context A value object with context variables.
 *
 * @return array $data Our Schema graph.
 */
public function render_schema_questions( $graph, $block, $context ) {
	$questions = new WPSEO_Schema_FAQ_Questions( $graph, $block, $context );
	$graph     = $questions->generate();

	return $graph;
}

より具体的なフィルタ

フィルタに基づいてグラフピースの有効/無効

多くの場合、他の変数に基づいて、グラフの部分を有効または無効にしたいことができます。 たとえば、サイトに を常に表示することができます。 このような状況で wpseo_schema_needs_ フィルタにフックすることができます。

add_filter( 'wpseo_schema_needs_person', '__return_true');
add_filter( 'wpseo_schema_person_user_id', function() { 
  return 1; // Make sure 1 is a valid User ID.
} );

ソーシャルプロフィール

Authorページに表示するプロファイルを変更したい場合 wpseo_schema_person_social_profiles フィルタにフックすることができます。 Yoastでyoast.comの人々の個人サイトだけでなく、人々のGitHubと WordPress プロフィールを sameAs 出力に追加しています。

add_filter( 'wpseo_schema_person_social_profiles', 'yoast_add_social_profiles' );

/**
 * Adds social profiles to our sameAs array.
 * SNS プロフィール을 sameAs 배열에 추가
 *
 * @param array $profiles Social profiles.
 *
 * @return array $profiles Social profiles.
 */
function yoast_add_social_profiles( $profiles ) {
  array_push( $profiles, 'github', 'personal', 'wordpress' );

  return $profiles;
}

その他のフィルタ

便宜のために、以下のようなフィルタも提供されます。

  • wpseo_schema_webpage_type - ページタイプの変更。 上記の例よりもシンプルに使用可能。
  • disable_wpseo_json_ld_search - trueを返す場合は、すべてのページに追加する検索 potentialActionを無効にする。
  • wpseo_json_ld_search_url - サイトの検索URL変更可能。
  • wpseo_schema_article_post_type - 記事 グラフピースを出力するポストタイプに変更可能。
  • wpseo_schema_person_user_id - サイトが人を代理(represent)する場合、サイトを代表するために使用するユーザーのID変更可能。

メモ:


コメントを残す

コメント