Customizing your WordPress template to set images within blog post content as Facebook thumbnail

// May 25th, 2011 // Programming, Social Media, Web Design, Web Development, WordPress //

Using the og:image meta property tag, Facebook allows you to manually set the thumbnail for the page. For example:

<meta property=”og:image” content=”http://www.widgetsandburritos.com/wp-content/uploads/154349_182859125063635_118277658188449_731837_4043510_n-300×231.jpg” />

This code would set the thumbnail to:
I want to draw a cat for you
Resulting in:
Facebook Thumbnail

What if you want to do it with WordPress? There are several methods that involve custom fields, featured images, plugins, or completely customized solutions. In my case, I was working on ImageFreedom.com, a site with an existing blog, but a brand new design. All of the previous blog posts didn’t have custom fields or featured images. To avoid having to edit each of those posts manually, we decided the best solution was to take the first image referenced in the post as the thumbnail image, unless one of two scenarios occurred: Either we wanted to use a different image further down in the post, or the post doesn’t have an image, in which case we would use site-wide default image.

On any image we didn’t want showing up as the Facebook thumbnail, we added rel=”nofb” (i.e. “No Facebook”) to the <img> tag

As a result, I created the following function in the functions.php file:

  1. <?php
  2. function set_meta_thumbnail($post_id = null) {
  3.         // This is our default image if no other image is found
  4.         $default_logo = "http://www.widgetsandburritos.com/wp-content/themes/irresistible/images/logo.png";
  5.  
  6.         // user manually sets post id
  7.         if (isset($post_id)) {
  8.                 $post = get_post($post_id);
  9.                 if (isset($post)) {
  10.                         $content = $post->post_content;
  11.                 }
  12.         // user is inside the loop
  13.         } else {
  14.                 $content = get_the_content();
  15.         }
  16.  
  17.  
  18.  
  19.         // variable for our image src
  20.         $src = null;
  21.  
  22.         // if our html content is not empty, then look for img tags
  23.         if (!empty($content)) {
  24.                 // turn off errors to avoid weird issues with DOMDocument
  25.                 $prev_val = libxml_use_internal_errors(true);
  26.  
  27.                 // parse the HTML content
  28.                 $DOM = new DOMDocument;
  29.                 $DOM->loadHTML($content);
  30.  
  31.                 // get all <img> tags
  32.                 $items = $DOM->getElementsByTagName(‘img’);
  33.  
  34.                 // loop through all the images
  35.                 if (isset($items)) {
  36.                         for ($i=0; $i < $items->length; $i++) {
  37.                                 // get the specific image
  38.                                 $item = $DOM->getElementsByTagName(‘img’)->item($i);
  39.                                 if (isset($item)) {
  40.                                         // assume this image is the image we want unless otherwise specified
  41.                                         $proceed = true;
  42.  
  43.                                         // if <img tag has rel="nofb" in it, do not use this as our image
  44.                                         if ($item->hasAttribute("rel")) {
  45.                                                 $rel = $item->getAttribute("rel");
  46.                                                 if (strtolower($rel) == "nofb")
  47.                                                         $proceed = false;
  48.                                         }
  49.  
  50.                                         // if it’s okay to use the current image, set $src and then break out of the loop
  51.                                         if ($proceed) {
  52.                                                 $src = $item->getAttribute("src");
  53.                                                 if (!empty($src))
  54.                                                         break;
  55.                                         }
  56.                                 }
  57.                         }
  58.                 }
  59.                 // revert to previous error display options
  60.                 libxml_use_internal_errors($prev_val);
  61.         }
  62.  
  63.         // if $src is set, then use that as our og:image, otherwise use the default
  64.         if (isset($src))
  65.                 echo "<meta property=\"og:image\" content=\"".$src."\" />\n";
  66.         else
  67.                 echo "<meta property=\"og:image\" content=\"".$default_logo."\" />\n";
  68. }
  69.  
  70. ?>

Now all you have to is place <?php set_meta_thumbnail($post->ID); ?> in the <head> section of your header.php file.

So for example, say for this specific blog post, I want the Facebook thumbnail image to be this silly Doctor Who reference cat picture:

Are you my mummy?

Then all I need to do, is set rel=”nofb” to all images prior to the previous one.

<img rel=”nofb” src=”http://www.widgetsandburritos.com/wp-content/uploads/154349_182859125063635_118277658188449_731837_4043510_n-300×231.jpg” />

<img rel=”nofb” src=”http://www.widgetsandburritos.com/wp-content/uploads/facebook-thumbnail.jpg” />

Then as soon as the function finds the above image in the code, it will set that as the og:image.

Why don’t you click here to share this link on Facebook to see that it works for yourself!

/* Facebook */

to “Customizing your WordPress template to set images within blog post content as Facebook thumbnail”

Leave a Reply