Use your WordPress featured image in Google+ and Facebook Shares

When sharing a post on Google+ or Twitter your best image to use may be your featured image set in WordPress. You can tell Google+ and Facebook to use your post thumbnail with 3 lines of code added the the header.php of your theme.

Note: The Yoast SEO Plugin can do this for you. If you are using Yoast SEO plugin check under “Social” and select “Add OpenGraph meta data”. Using Yoast’s plugin, you will get the follow open graph settings (which work with Google Plus): og:locale, og:title, og:url, og:site_name, og:type, og:image.

Manual Method: Place this code snippet in your themes header.php:

<?php if (is_single() && has_post_thumbnail()) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<meta property="og:image" content="<?php echo $image[0]; ?>"/>
<?php } ?>

This checks if the current page is a post and has a post thumbnail. The post thumbnail link is then placed in an Open Graph meta tag utilized by both Facebook and Google Plus.

Method 2: Show an image other than one displayed in the post or used as the post thumbnail

  1. Upload an image you want to use for social shares.
  2. Edit the post where you want it to appear and under “Custom Fields” enter a new custom field named “Share Image”.

Add this code to your header.php:

<?php if (is_single() && get_post_meta($post->ID, 'Share Image', true)) : ?>
<meta property="og:image" content="<?php echo get_post_meta($post->ID, 'Share Image', true); ?>"/>
<?php endif; ?>

Set the “Share Image” on every post that doesn’t have a suitable image within the posts content.

This entry was posted in Social Media. Bookmark the permalink.

3 Responses to Use your WordPress featured image in Google+ and Facebook Shares

    Leave a Reply

    Your email address will not be published. Required fields are marked *