Adding Google+ Meta Tags in WordPress
by Bandicoot Marketing on | Posted in Tutorials | 17 comments
If you’ve decided to include a Google+ button on your WordPress site, it’s also suggested that you add a few new meta tags to your header so that you can customize exactly what appears when people share your page on Google+. You can read more about the requirements at http://www.google.com/webmasters/+1/button/.
Here is a quick snippet that you can add to your theme’s functions.php file that will add your post’s title, excerpt and featured image to the appropriate meta tags:
/**
* Add Google+ meta tags to header
*
* @uses get_the_ID() Get post ID
* @uses setup_postdata() setup postdata to get the excerpt
* @uses wp_get_attachment_image_src() Get thumbnail src
* @uses get_post_thumbnail_id Get thumbnail ID
* @uses the_title() Display the post title
*
* @author c.bavota
*/
add_action( 'wp_head', 'add_google_plus_meta' );
function add_google_plus_meta() {
if( is_single() ) {
global $post;
$post_id = get_the_ID();
setup_postdata( $post );
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'thumbnail' );
$thumbnail = empty( $thumbnail ) ? '' : '<meta itemprop="image" content="' . esc_url( $thumbnail[0] ) . '">';
?>
<!-- Google+ meta tags -->
<meta itemprop="name" content="<?php esc_attr( the_title() ); ?>">
<meta itemprop="description" content="<?php echo esc_attr( get_the_excerpt() ); ?>">
<?php echo $thumbnail . "\n"; ?>
<!-- eof Google+ meta tags -->
<?php
}
}
Now you can use the code supplied on http://www.google.com/webmasters/+1/button/ to included the Google+ button wherever you want on your site. I stuck it into the single.php file next to my other social media buttons. You can see them all below.

17 comments for “Adding Google+ Meta Tags in WordPress”