Support » Plugin: AMP for WordPress » Structured Data error for Attachment Pages (Media)

  • Resolved shrewdies

    (@shrewdies)



    As far as I can see, all attachment pages cause an error in the structured data testing tool – “A value for the image field is required.”

    From a quick look at function amp_get_post_image_metadata() in amp-helper-functions.php it seems to me that the logic doesn’t cater for attachment page structured data. But I’m not proficient enough to be certain.

    Is this a bug, or am I doing something else wrong?

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Ryan Kienstra

    (@ryankienstra)

    Hi @shrewdies,
    Wow, you’re running Native AMP!

    Yes, this looks to be a bug. Thanks for letting me know about it.

    I also saw that, where the schema.org data doesn’t have an image value for attachment pages:

    https://www.goutpal.com/11607/gout-in-shoulder/shoulder-in-gout-mri-media/
    No image value

    It looks like you’re right that amp_get_post_image_metadata() isn’t getting the data of an image if the page is an attachment page.

    I’ll request a GitHub issue for this. In the meantime, placing this in your theme should add the image. It worked for me locally:

    
    /**
     * Add data for an image when the page is an attachment page.
     * 
     * On attachment pages, like pages for an image, there wasn't a schema.org value for 'image.'
     * So this adds it via a filter.
     * 
     * @param array   $metadata The schema.org metadata.
     * @param WP_Post $post     The post to which it applies.
     * @return array $metadata The filtered schema.org metadata.  
     */
    add_filter( 'amp_post_template_metadata', function( $metadata, $post ) {
    	if ( empty( $metadata['image'] ) && ( 'attachment' === $post->post_type ) ) {
    		$post_image_src = wp_get_attachment_image_src( $post->ID, 'full' );
    		if ( is_array( $post_image_src ) ) {
    			$metadata['image'] = array(
    				'@type'  => 'ImageObject',
    				'url'    => $post_image_src[0],
    				'width'  => $post_image_src[1],
    				'height' => $post_image_src[2],
    			);
    		}
    	}
    
    	return $metadata;
    }, 10, 2 );
    
    • This reply was modified 2 months, 4 weeks ago by  Ryan Kienstra.
    • This reply was modified 2 months, 4 weeks ago by  Ryan Kienstra.
    Plugin Support Ryan Kienstra

    (@ryankienstra)

    GitHub Issue

    Hi @shrewdies,
    Here’s the GitHub issue for this, so you know when a fix is deployed:
    https://github.com/Automattic/amp-wp/issues/1157

    Thank you very much for your fast response @ryankienstra

    I’ve since noticed that this also applies to pdf attachments. But I’m not familiar with GitHub. So I wonder if this should be commented there, or raised as a new issue?

    Thanks again.

    Plugin Support Ryan Kienstra

    (@ryankienstra)

    Hi @shrewdies,
    Thanks for your great review. It’s exciting to see your site running Native AMP.

    Yes, I also see that the issue exists for .pdf attachments as well. And it looks like the Google Structured Data Testing Tool accepts .pdf documents in the ImageObject.

    Feel free to open a GitHub issue for that:

    https://github.com/Automattic/amp-wp/issues/new

    • This reply was modified 2 months, 2 weeks ago by  Ryan Kienstra.
    Plugin Support Ryan Kienstra

    (@ryankienstra)

    Also, simply commenting on this GitHub issue and linking to this support topic would be fine:

    https://github.com/Automattic/amp-wp/issues/1157

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.