3

When validating my wordpress posts using Google's Structured Data Testing Tool, I get the following error:

"Image: missing and required"

I have the official wordpress AMP plugin installed that generated the AMP pages for me. The problem is that it doesn't popular the "image" attribute for BlogPosting.

In the plugin there is a code that I think should generate it, but it's not running anywhere:

private function get_post_image_metadata() {
    $post_image_meta = null;
    $post_image_id = false;

    if ( has_post_thumbnail( $this->ID ) ) {
        $post_image_id = get_post_thumbnail_id( $this->ID );
    } else {
        $attached_image_ids = get_posts( array(
            'post_parent' => $this->ID,
            'post_type' => 'attachment',
            'post_mime_type' => 'image',
            'posts_per_page' => 1,
            'orderby' => 'menu_order',
            'order' => 'ASC',
            'fields' => 'ids',
            'suppress_filters' => false,
        ) );

        if ( ! empty( $attached_image_ids ) ) {
            $post_image_id = array_shift( $attached_image_ids );
        }
    }

    if ( ! $post_image_id ) {
        return false;
    }

    $post_image_src = wp_get_attachment_image_src( $post_image_id, 'full' );

    if ( is_array( $post_image_src ) ) {
        $post_image_meta = array(
            '@type' => 'ImageObject',
            'url' => $post_image_src[0],
            'width' => $post_image_src[1],
            'height' => $post_image_src[2],
        );
    }

    return $post_image_meta;
}

How can I populate the image tag for each post using this AMP WordPress plugin? I want the page to pass the Structured Data Testing Tool, so he can pass the AMP validation as well.

Update: the reason that the image is not showing is because there is not embedded image in the post. Is there a way to put a default image in case there isn't one, so it will pass the AMP/Schema validation.

unor
  • 92,415
  • 26
  • 211
  • 360
Liron Harel
  • 10,819
  • 26
  • 118
  • 217

2 Answers2

2

For conforming to the AMP HTML Specification, you don’t have to use Schema.org structured data.

If the Google SDTT says that a property is "missing and required", it does not mean that it’s required by AMP or Schema.org. It only means that Google won’t show one of their Google Search results features (like Rich Snippets) for your page.

For example, there’s the Top Stories with AMP feature: a carousel that links to AMP pages, showing an image from each page. That’s why Google requires the Schema.org image property for this feature (for articles). But it’s perfectly fine not to provide an image for your article, the only thing that "happens" is that your page doesn’t get a chance to appear in that Top Stories carousel.

You could of course populate $post_image_meta with a default image (e.g., a placeholder) if is_array( $post_image_src ) is false, but that’s very likely not a good idea: the image wouldn’t be relevant, so users searching on Google Search wouldn’t find it useful, so Google Search would have an interest not to show your result to their users. (But if and in which way that’s actually the case is an SEO issue, which belongs to Webmasters SE.)

Community
  • 1
  • 1
unor
  • 92,415
  • 26
  • 211
  • 360
  • I agree that's not wise to add default image. I'll leave it like that. Thanks! – Liron Harel Feb 29 '16 at 23:21
  • Good true answer. Still debating whether to add a default image or not. Really wish Google would specify this in their AMP guides, since the Webmaster Tools will continue treating these missing images as "issues" of type "Info: Invalid structured data element" forever, clogging up my review of actual errors in that space. Frustrating for old posts (that I will never go back and fix) and new posts without any meaningful image. – jerclarke Oct 04 '16 at 18:59
  • 1
    Update: They answered me on their GitHub and what they intend is NO DEFAULT IMAGES: "The best guideline here is the quote you included from the docs, to only use an image that directly belongs to the article. As you mention, we want to avoid the bad user experience of getting uninformative images." https://github.com/ampproject/amphtml/issues/5404#issuecomment-251818433 – jerclarke Oct 07 '16 at 21:48
1

Default image for old posts without images

add_filter( 'amp_post_template_metadata', 'bbm_amp_modify_json_metadata', 10, 2 );
function bbm_amp_modify_json_metadata( $metadata, $post ) {
    if (!array_key_exists('image', $metadata)) {
    $metadata['image'] = array(
            '@type' => 'ImageObject',
            'url' => get_template_directory_uri() . '/images/default.png',
            'height' => 512,
            'width' => 1024,
        );
    }
    return $metadata;
}

From amp plugin support. I've checked and it works. Images should be at least 696 pixels wide.

Iggy
  • 2,014
  • 25
  • 21
  • Technically this violates the AMP guidelines which demand that the 'image' be directly related to the post. See comment on other answer. – jerclarke Oct 07 '16 at 21:49