Set Facebook Share Image Thumbnail in WordPress

When someone shares an image from your WordPress blog to Facebook, they usually do a pretty good job of guessing which image is the right image to turn into a thumbnail, but did you know you can set the image so Facebook doesn’t have to guess? In header.php before the closing head tag, add the following code:

<?php if ( is_single() ) {
$image_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
<meta property="og:image" content="<?php echo $image_url; ?>" />
<?php } ?>

The og:image means that facebook is following open graph protocol. The check for is_single just confirms to WordPress that we are on a single post page before including it in the header.