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

  • Resolved 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)
  • 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 data doesn’t have an image value for attachment pages:
    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 value for 'image.'
     * So this adds it via a filter.
     * @param array   $metadata The metadata.
     * @param WP_Post $post     The post to which it applies.
     * @return array $metadata The filtered 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 );

    GitHub Issue

    Hi @shrewdies,
    Here’s the GitHub issue for this, so you know when a fix is deployed:

    Thread Starter shrewdies


    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.

    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:

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Structured Data error for Attachment Pages (Media)’ is closed to new replies.