waynedunkley
3/3/2015 - 4:04 AM

Add Open Graph Meta information to <head>

Add Open Graph Meta information to

//Add the Open Graph in the Language Attributes
function add_opengraph_doctype( $output ) {
		return $output . ' xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"';
	}
add_filter('language_attributes', 'add_opengraph_doctype');

//Add Open Graph Meta Info
function insert_og_meta() {
	global $post;
	if ( is_singular() ) {//if this is a post or page

		$blog_title = get_the_title();
		$description = strip_tags( $post->post_content ); // Get Post content
		$description = str_replace('"', '\'', $description); // Replace all quotes
		if( strlen($description) > 300  ){ // Limit to 300 characters
			$description = substr($description, 0, 300);
		};

		echo '<meta property="og:title" content="' . get_the_title() . '"/>';
		echo '<meta property="og:type" content="article"/>';
		echo '<meta property="og:url" content="' . get_permalink() . '"/>';
		echo '<meta property="og:site_name" content="' . $blog_title . '"/>';
		echo '<meta property="og:description" content="'. $description .'"/>';
		if( has_post_thumbnail( $id ) ) {
			$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
			$image = $image[0];
			echo '<meta property="og:image" content="' . $image . '"/>';
		}

		echo "";
	}
}
add_action( 'wp_head', 'insert_og_meta', 5 );