Automatically add Open Graph to your posts
Automatically add Open Graph to your posts
Open Graph is a protocol created by facebook which allow you to make your content easily recognizable by social networks.
To automatically add Open Graph metadata to your posts, let’s start by pasting the following code snippet into your functions.php file.
function wptuts_opengraph_for_posts() {
if ( is_singular() ) {
global $post;
setup_postdata( $post );
$output = ‘
‘ . “n”;
$output .= ‘
‘ . “n”;
$output .= ‘
‘ . “n”;
$output .= ‘
‘ . “n”;
if ( has_post_thumbnail() ) {
$imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), ‘medium’ );
$output .= ‘
‘ . “n”;
}
echo $output;
}
}
add_action( ‘wp_head’, ‘wptuts_opengraph_for_posts’ );
Then, open your header.php and replace the tag by this: