I have code in my page header which properly creates the Open Graph tags for pages, posts, etc. However, when I attempt to Share an attachment post from the Media Library (not the actual attachment image, but rather the URI) no Open Graph tags get shared. Is this possible to do? If so, how?

Eg. the WordPress URL is


Which properly redirects (301) to


When I use the Facebook OG Object Debugger, it shows a response code of 206

 The following will be treated as a redirect by the crawler:

    A HTTP redirect
    A <link rel="canonical" href=".." /> tag
    A <meta property="og:url" content=".." /> tag

 The final URL, which we tried to extract metadata from is highlighted in bold.

It also shows that the final page (the PDF) is a 301 redirect.

Here is the code I put in my header. Which (again) works FINE for all post types. But not attachments.

function insert_fb_in_head() {

    $og_title = get_post_meta( get_the_ID(), '_yoast_wpseo_title', true);
    if( ! $og_title)    
        $og_title = get_the_title();

    echo ('<meta property="og:title" content="' . $og_title . '"/>' . PHP_EOL);
    echo ('<meta property="og:type" content="article"/>' . PHP_EOL);
    echo ('<meta property="og:url" content="' . get_permalink() . '"/>' . PHP_EOL);
    echo ('<meta property="og:site_name" content="Noise Information"/>' . PHP_EOL);
    echo ('<meta property="og:description" content="' . strip_tags( get_post_meta( get_the_ID(), '_yoast_wpseo_metadesc', true), '' ) . '"/>' . PHP_EOL);

    if( has_post_thumbnail( $post->ID )) { //the post has a featured image, use a default image
    $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
    echo ('<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>' . PHP_EOL);
    echo ('<meta property="og:image:width" content="' . esc_attr( $thumbnail_src[1] ) . '"/>' . PHP_EOL);
    echo ('<meta property="og:image:height" content="' . esc_attr( $thumbnail_src[2] ) . '"/>' . PHP_EOL);      

    echo ('<meta property="og:image" content="' . $default_image . '"/>' . PHP_EOL);
    echo ('<meta property="og:image:width" content="250"/>' . PHP_EOL);
    echo ('<meta property="og:image:height" content="250"/>' . PHP_EOL);        
 add_action( 'wp_head', 'insert_fb_in_head', 5 );

I've Googled until I'm blue in the face and found nothing.

  • What does your open graph code look like before the DOM loads? And when you say your page header...which file specifically have you modified to add your code to the header? – klewis Aug 2 '18 at 0:47
  • I'm sorry, do you mean the url of an image of the attachment-page of the media? Use Yoast? og tags are present on the html head element. image-files don't have a head-element. – admcfajn Aug 2 '18 at 1:38
  • @admcfajn I mean the URL of the attachment page.. -not- the referenced JPG or PDF – jchwebdev Aug 2 '18 at 5:38
  • @blackhawk I mean header.php. This code works -fine- (and has done for 2 years) for posts, pages and all custom post types. It only doesn't work for attachment pages created in the Media Library. – jchwebdev Aug 2 '18 at 5:39
  • 1
    @jchwebdev There's an option for it within Yoast SEO at (as of the latest version, at least) /wp-admin/admin.php?page=wpseo_titles#top#media "Media & attachment URLs Learn more about the Media and attachment URLs setting We recommend you set this to Yes. Redirect attachment URLs to the attachment itself?" Turning that off should stop the media pages from being redirected & allow the opengraph data to pick-up – admcfajn Aug 3 '18 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.