wp Add Facebook Open Graph Meta Tags

Add Facebook Open Graph Meta Tags

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);

Leave a comment