Creating a function that hooks into the wp_head action will let you define the post avatar image as one of the Open Graph image options:
add_action( 'wp_head', 'gklpa_ogimage' );
function gklpa_ogimage(){
global $post;
$og_image = '';
if( is_single() ){
$avatar_array = gkl_get_postavatar($post);
$og_image = '<meta property="og:image" content="' . esc_url( $avatar_array['avatar_url'] ) . '" />' . "\n";
}
echo $og_image;
}
This code can go in your theme’s functions.php file or in a custom plugin.
Wow, awesome! After all this time, I didn’t think anyone would suggest anything on this. Thank you so much, I’ll give it a try.
If there is both a post avatar and a featured image in a post, would this code cause the avatar to get picked as the og image, or would both be included? In such a case, I’d prefer the featured image get picked up, if only one could.
Well, unfortunately, this didn’t work. Here are the Open Graph additions to the page, from looking at the page source:
<meta property="og:locale" content="en_US" />
<meta property="og:type" content="article" />
<meta property="og:title" content="Rick Perry's Smug Shot - Will Durst, Humor Times" />
<meta property="og:description" content="Knew he shouldn't. Couldn't help himself. Talking about the beaming leer in Texas Governor Rick Perry's mug shot. Or to be more precise, his smug shot." />
<meta property="og:url" content="http://www.humortimes.com/28330/rick-perry-smug-shot/" />
<meta property="og:site_name" content="Humor Times: Political Satire, Cartoons, Videos and More" />
<meta property="article:publisher" content="https://www.facebook.com/HumorTimes" />
<meta property="article:author" content="https://www.facebook.com/will.durst.9" />
<meta property="article:tag" content="commentary,humor,news,politics,Rick Perry,satire" />
<meta property="article:section" content="Humor Columns" />
<meta property="article:published_time" content="2014-08-24T20:55:39+00:00" />
<meta property="fb:admins" content="1045169005" />
<meta property="og:image" content="http://www.humortimes.com/wp-content/uploads/images1/HT-cover.jpg" />
Only one image is loaded, the default image.
Btw, I am using the Yoast WordPress SEO plugin’s settings to add the Open Graph meta data.
Any ideas?
Let me see if there’s any conflict with Yoast SEO plugin.
What about making the function go last, there’s a way to do that, isn’t there? That way, whatever Yoast does, this will get included after that.
I’ve figured it out. The above code was being placed after the open graph SEO code.
Here’s an improved version of the code that hooks into WordPress SEO filters and will only show if there is no featured image:
add_action( 'wpseo_opengraph', 'gklpa_ogimage', 99 );
function gklpa_ogimage(){
global $post;
$og_image = '';
if( has_post_thumbnail( $post->ID ) )
return;
if( is_single() ){
$avatar_array = gkl_get_postavatar($post);
$og_image = '<meta property="og:image" content="' . esc_url( $avatar_array['avatar_url'] ) . '" />' . "\n";
}
echo $og_image;
}
Thanks, that seems to work! I appreciate your help.