Default image for article in Yoast SEO Schema

I’m a big fan of the new schema functionality in Yoast SEO. It includes all the information search engines want to see, and it’s incredibly extensible through filters and extending classes.

If a post has a featured image, Yoast SEO will add a graph piece for primaryImageOfPage and will attach that to the Article graph piece. If there’s no featured image, no image is attached to the article.

When an image is required

I recently updated my site to be Native AMP, and the next day I received a Google Search Console email describing issues with some of my articles.

Google has slightly different schema requirements for AMP pages. These are what caused my warnings above:

  • Every Article must have at least one image provided
  • The image must be at least 1200px wide

To fix this issue, I’m using my site logo as the default image, which is set in SEO > Search Appearance > Organization Logo. Make sure the image you have uploaded there is at least 1200px wide.

Yoast SEO Default Article Image

/**
 * Default image in Article schema
 * If there is no featured image, or featured image is
 * < 1200px wide, use the site logo instead.
 *
 * @link https://www.billerickson.net/yoast-seo-schema-default-image/
 *
 * @param array $graph_piece
 * @return array
 */
function be_schema_default_image( $graph_piece ) {
	$use_default = false;
	if( has_post_thumbnail() ) {
		$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
		if( empty( $image_src[1] ) || 1199 > $image_src[1] )
			$use_default = true;
	} else {
		$use_default = true;
	}

	if( $use_default ) {
		$graph_piece['image']['@id'] = home_url( '#logo' );
	}
	return $graph_piece;
}
add_filter( 'wpseo_schema_article', 'be_schema_default_image' );

Note: this only applies to the Article graph piece. If you have other graph pieces that require a default image, add an additional filter to the bottom using the format wpseo_schema_{type}.

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Comments

  1. Jono Alderson says

    Hey Bill, this is a great example of extending/adapting our schema framework – thanks for taking the time to explore and publish this!

    Just one potential issue, though… Google’s documentation is pretty keen on having the `image` “represent the article”. I think that the generic site image approach probably doesn’t qualify, so I’d potentially be a bit nervous about them having issues with that.

    On our side, I’ll definitely explore how we could add some warnings when a site is running AMP and using an image which is too small!

    • Bill Erickson says

      Thank you. I agree, a generic image isn’t the best option. I’m going through my popular articles now to update featured images to be at least 1200px wide, so this should only apply to older and less popular articles.

      Since Google requires an image for AMP pages, I think a generic image is better than no image at all. I wouldn’t recommend this for non-AMP pages unless Google extends the requirement to them in the future.

  2. Ana A says

    Hi Bill,

    Thanks for the great code snippets on your website, bookmarked! It works out well.

    Is it possible to use a custom image with URL, without using the logo image in Yoast settings?

    We tried to create the following code, but it does not seem to work as expected. Can you help?

    /**
    * Change the default featured image
    * Yoast article schema #primaryimage
    */

    function change_article_primaryimage( $data ) {
    $image_url = ‘https://URL-to-1200px.png’;

    $context = new WPSEO_Schema_Context();
    $data[‘@id’] = $context->canonical . WPSEO_Schema_IDs::PRIMARY_IMAGE_HASH;
    $image_id = $data[‘@id’];

    $schema_image = new WPSEO_Schema_Image($image_id);
    $data[‘image’] = $schema_image->generate_from_url($image_url);

    return $data;
    }
    add_filter( ‘wpseo_schema_article’, ‘change_article_primaryimage’ );

    Thanks in advance,
    Ana

    • Bill Erickson says

      You might try reaching out to Yoast SEO support for guidance here. I haven’t tried using an image not already in the schema.

      Please follow up here once you find the solution. Thanks

  3. Jackie D'Elia says

    Thanks, Bill. This solves the immediate issue while I go through and update my posts with AMP sized featured images.

Leave A Reply