Add Facebook Open Graph Meta Tags

This document is deprecated! The information on this page refers to a Thesis version that is now obsolete. Please visit the Thesis Docs for current documentation.

Place the following code in custom_functions.php (making changes where noted):

function add_facebook_open_graph_tags() {
	if (is_single()) { 
	global $post; 
	$image = get_post_meta($post->ID, 'thesis_post_image', $single = true); 
	if (!$image)
		$image = 'ENTER URL TO DEFAULT IMAGE HERE';
	?>	
	<meta property="og:title" content="<?php the_title(); ?>" />
	<meta property="og:type" content="article" />
	<meta property="og:image" content="<?php echo $image; ?>" />
	<meta property="og:url" content="<?php the_permalink(); ?>" />
	<meta property="og:description" content="<?php echo get_bloginfo('description'); ?>" />
	<meta property="og:site_name" content="<?php echo get_bloginfo('name'); ?>" />
	<meta property="fb:admins" content="ENTER YOUR FACEBOOK USER ID HERE" />
	<?php }
}
add_action('wp_head', 'add_facebook_open_graph_tags',99);  

If you want to use the Post Excerpt for the description instead, then use this version:

function add_facebook_open_graph_tags() {
	if (is_single()) {
		global $post;
		setup_postdata($post);
		$image = get_post_meta($post->ID, 'thesis_post_image', $single = true);
		if (!$image)
			$image = 'ENTER URL TO DEFAULT IMAGE HERE';
		?>
		<meta property="og:title" content="<?php the_title(); ?>" />
		<meta property="og:type" content="article" />
		<meta property="og:image" content="<?php echo $image; ?>" />
		<meta property="og:url" content="<?php the_permalink(); ?>" />
		<meta property="og:description" content="<?php echo get_the_excerpt();  ?>" />
		<meta property="og:site_name" content="<?php echo get_bloginfo('name'); ?>" />
		<meta property="fb:admins" content="ENTER YOUR FACEBOOK USER ID HERE" />
		<?php }
}
add_action('wp_head', 'add_facebook_open_graph_tags',99);

What if you would like a more advanced handler of your Facebook Open Graph images?

The below works on the following logic:

If there are one or more images in the content of the post, use the first image in your post contents. If there are no images to use from the post contents, use the Thesis “post image” — and if there’s neither an in-content image nor a post image available for the given post, use a static “fallback” image as defined by the site operator.

function facebook_first_image_grabber() {
if (is_single()) {
global $post, $posts;
$my_post_image = get_post_meta($post->ID, 'thesis_post_image', $single = true);
$first_img_located = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img_located = $matches [1] [0];
	if(empty($first_img_located)){
$first_img_located = $my_post_image;
		}
	if(empty($my_post_image)){
$first_img_located = "ENTER_YOUR_ABSOLUTE_URL_TO_DEFAULT_IMAGE_HERE";
		}
	return $first_img_located;
	}
}

function open_graph_tags() {
if (is_single()) {
global $post;
?>
<meta property="og:title" content="<?php the_title(); ?>" />
<meta property="og:type" content="article" />
	<meta property="og:image" content="<?php if (function_exists('facebook_first_image_grabber')) {echo facebook_first_image_grabber(); }?>" />  
<meta property="og:url" content="<?php the_permalink(); ?>" />
<meta property="og:description" content="<?php echo get_bloginfo('description'); ?>" />
<meta property="og:site_name" content="<?php echo get_bloginfo('name'); ?>" />
<meta property="fb:admins" content="ENTER YOUR FACEBOOK USER ID HERE" />
<meta property="fb:app_id" content="ENTER YOUR FACEBOOK APPLICATION ID HERE" />
<?php }
}
add_action('wp_head', 'open_graph_tags', 99);

Note that you must provide your own fallback image URL in addition to your Facebook “admins” and “app_id” values.