• The plugin doesn’t add the attribute “image” to the BlogPosting. There is a function ‘get_post_image_metadata’ that seems that it should do it, but it doesn’t. If I have a post without image, can’t it be validated? Maybe use a default image so it will pass validation?

    Any idea how to solve this?

    Thanks.

    https://wordpress.org/plugins/amp/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Brad Brown

    (@bradbrownmagic)

    Sure, you can add code to put a default image in the schema if there isn’t one defined. The WordPress AMP docs show how to use a filter to edit schema data: https://github.com/Automattic/amp-wp/blob/master/readme.md#schemaorg-json-metadata

    Below is the code I wrote up for my site:

    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;
    }

    Thank you very much @brad Brown worked perfect for me!!

    I discovered this morning that I’m having this problem too (‘Image missing and required’ errors in GWT).

    Reading this thread gave me an idea…

    At least in my case, it turns out that these errors are appearing for older blog posts of mine that don’t have featured images selected, likely because they were posted before WordPress introduced the feature and I wasn’t going to manually update hundreds of posts.

    After setting a featured image for one of the older posts, the Structured Data Testing Tool no longer sees the ‘Image missing and required’ error, although it does still throw a warning, claiming that ‘The attribute image.width has an invalid value’ even though it seems to have a correct value.

    Since I’m still not interested in updating hundreds of old posts, I used @brad Brown’s suggested code in my functions.php file, and now all the errors are gone. Thanks!

    Hello Guys, could you be more precise concerning the exact file and the exact place in the file to put this code ? Thanks a lot !!

    hmmm I checked one of the pages as having issue and it has a featured image as per all my pages.

    not sure if my issue is different as the validator error is A value for the image field is required.

    Had similar problem (missing image) and the only way to fix it was to open each post and then associate an image to it. It would have meant updating 100s of post.

    Applied @bradbrown’s fix a few days back and at GWT, I see a huge drop in ‘image errors’. Thank you Brad.

    There is no need to edit Every post for just a featured image.
    How AMP Works
    First system search for Featured image.
    If featured image not found then system check post image.
    You can directly set featured image for all your posts via editing
    amp/includes/class-amp-post-template.php file

    For more details :
    Image Missing required solution in AMP

    Once you fix the default image problem then you’ll get “The attribute image.width has an invalid value.” It’s because the image width IS incorrect. So is the height, BTW, because there is supposed to be quotes around the data part of the JSON data, {“height”:100,”width”:100} should be {“height”:”100″,”width”:”100″}. The plugin is borked.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Image missing and required’ is closed to new replies.