WordPress.org

Forums

Post Avatar
[resolved] Generate meta property="og:image" for Post Avatar image? (8 posts)

  1. jamminjames
    Member
    Posted 1 year ago #

    It would be great if Post Avatar could generate a meta property="og:image" line in the url for the image.

    This is for the "Open Graph protocol" used in Facebook to decide what images to choose from when people share the post.

    Sometimes in a post, there are no other images, other than the Post Avatar image, so it should be included in the Open Graph protocol.

    How can we make sure that is generated? Is there any simple code we can add to do this?

    Thanks for any help.

    https://wordpress.org/plugins/post-avatar/

  2. Vicky Arulsingam
    Member
    Plugin Author

    Posted 6 days ago #

    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.

  3. jamminjames
    Member
    Posted 6 days ago #

    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.

  4. jamminjames
    Member
    Posted 6 days ago #

    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?

  5. Vicky Arulsingam
    Member
    Plugin Author

    Posted 5 days ago #

    Let me see if there's any conflict with Yoast SEO plugin.

  6. jamminjames
    Member
    Posted 5 days ago #

    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.

  7. Vicky Arulsingam
    Member
    Plugin Author

    Posted 5 days ago #

    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;
    }
  8. jamminjames
    Member
    Posted 5 days ago #

    Thanks, that seems to work! I appreciate your help.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic