Skip to content

Instantly share code, notes, and snippets.

@dattard21
Created September 21, 2016 18:36
Show Gist options
  • Save dattard21/d16cef685953b4b6bab45abddf8d7328 to your computer and use it in GitHub Desktop.
Save dattard21/d16cef685953b4b6bab45abddf8d7328 to your computer and use it in GitHub Desktop.
//Add Open Graph Meta Info from the actual article data, or customize as necessary
function insert_fb_in_head() {
global $post;
if ( !is_singular()) //if it is not a post or a page
return;
//You'll need to find you Facebook profile Id and add it as the admin
echo '<meta property="fb:admins" content="XXXXXXXXX-fb-admin-id"/>';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="article"/>';
echo '<meta property="og:url" content="' . get_permalink() . '"/>';
// Customize the below with the name of your site
echo '<meta property="og:site_name" content="Your Site NAME Goes HERE"/>';
if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
//Create a default image on your server or an image in your media library, and insert it's URL here
$default_image="http://example.com/image.jpg";
echo '<meta property="og:image" content="' . $default_image . '"/>';
}
else{
$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] ) . '"/>';
}
echo "
";
}
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