Skip to content

Instantly share code, notes, and snippets.

@steezeburger
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steezeburger/231b3982e78fb4080609 to your computer and use it in GitHub Desktop.
Save steezeburger/231b3982e78fb4080609 to your computer and use it in GitHub Desktop.
This sets a pages og:image property to the 'featured image' of that page. Facebook uses og:image when displaying images for posts to Facebook.
<?php
// Adding 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');
// Adding Open Graph meta info
function insert_fb_in_head() {
global $post;
// Use featured image if available
if (has_post_thumbnail( $post->ID )) {
$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium' );
echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
}
} // insert_db_in_head()
// Add function to wp_head hook
add_action( 'wp_head', 'insert_fb_in_head', 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment