Adding Facebook Open Graph Code does not work for Media Library Attachment Pages
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), the FB Object Debugger complains
"Error parsing input URL, no data was cached, or no data was scraped."
I've examined the header on generated pages and they appear identical to other Pages, Posts and other Custom Post types. We were using Yoast and I -thought- that this was the problem, but I disabled it and still not working.
Eg. the WordPress URL is
http://example.com/part-150-study/
Which contains a link to:
http://example.com/wp-content/uploads/part-150-study.pdf
Which works fine.
Here is the code I put in my header. Which (again) works FINE for all other 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);
}
$default_image="http://example.com/wp-content/uploads/airplane-250.png";
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 );
Here is the section of an example rendered page source HTML:
meta property="og:title" content="2017 POS CAFR FINAL 5.2.2018"/
meta property="og:type" content="article"/
meta property="og:url" content="http://example.com/2017-pos-cafr-final-5-2-2018-2/"/
meta property="og:site_name" content="Example Site"/
meta property="og:description" content="2017 POS CAFR FINAL 5.2.2018"/
meta property="og:image" content="http://example.com/wp-content/uploads/airplane-large-circles-stretched-250.png"/
meta property="og:image:width" content="250"/
meta property="og:image:height" content="250"/
AGAIN, I asked this question in another thread and it was suggested to try using Yoast and changing the setting in the "Redirect attachment URLs to the attachment itself?" But that made no difference and in fact, de-activating Yoast entirely made no difference.
Ideas?
Topic media-library open-graph Wordpress
Category Web