Quickly add Open Graph to WordPress theme

I’ve found a lot of benefit adding Open Graph properties to my blogs. Primarily: more visitors. By adding simple code to my WordPress theme headers, social plugins like the ‘Like’ button will make your content display more meaningful within Facebook.

Which of these two articles would you be drawn to? Which would cause you to stop, read, and consider clicking through to?

Yet another easily ignorable line item:

Or this rich image and excerpt:

With WordPress I can easily drop a few lines of code into my theme’s header and make any ‘Liked’ content a little more compelling.

The Source

In the header, I’ve added a quick snippet to grab only the image URL of the featured image for the current post (via wpcanyon.com):


$thumb = get_the_post_thumbnail($post->ID);
$pattern= "/(?<=src=['|\"])[^'|\"]*?(?=['|\"])/i";
preg_match($pattern, $thumb, $thePath);
$theSrc = $thePath[0];

I then add the following to define the required Open Graph properties:


<meta property="fb:admins" content="FACEBOOK ID" />
<? if (is_single()) { ?>
<meta property="og:title" content="<?php echo get_the_title(); ?>"/>
<meta property="og:type" content="article"/>
<meta property="og:image" content="<?php echo $theSrc; ?>" />
<meta property="og:url" content="<?php the_permalink() ?>" />
<meta property="og:description" content="<?php the_excerpt_rss() ?>" />
<meta propert="og:site_name" content="<?php bloginfo('name'); ?>" />
<? } ?>

And just like that, you’ve added all the necessary properties to your theme to tell anything that respects Open Graph (primarily Facebook) to your blog articles.

Be sure to use the Facebook URL Linter to test this out. Here’s an example of a recent post of mine.

This entry was posted in Internet, WordPress on by .

About Devin Reams

My name is Devin Reams and I founded this site to provide a useful news and review resource for Colorado skiers and snowboarders (and mountain enthusiasts). I've been skiing since I was a little kid (we moved out here when I was five years old) and I plan to ski for years beyond that. Although cosnow is not my full-time job it is my full-time winter hobby. I've been an "Epic Local" passholder since 2006 (when it was called a "Colorado Pass" or "Five Mountain Pass"). My favorite resorts are Beaver Creek and Breckenridge.

3 thoughts on “Quickly add Open Graph to WordPress theme

  1. Pingback: Open Graph – Facebooki “salarelv” veebi vallutamiseks « OKIA Blogi

Leave a Reply

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