Adding Google+ Meta Tags in WordPress
by c.bavota | 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.



Very handy tutorial.
Mee to, but is_single not enought meta can have in is_category, is_home, is_page, is_tag… many and many!
I agree, it’s always nice to find exactly what you’re search for. Good site.
Handy and useful tip, thanks.
Thanks, I was wondering how to change the thumbnail that was showing on g+.
Been wondering this for some time as well. It would really help me for a project I’m about to start.
Good explanation…thanks
Thanks for the code!
Wow thanks for that! It’s a pain when I see that the only image options for G+ on my sites are either the header or a not-very-informative and unappealing product image. Much appreciation!
You maid it so simple..Thanks! was looking so much for this
Good tip, but in Germany we have as stringent laws for data protection as in Canada, so I have to rely on plugins that take account of this.
This is a good way to personalize and customize your google+ page.
I didn’t know you could customize what the posting is. Thanks I’m going to update mine soon.
Exactly what i was searching for!
it is so simple.. thanks
Handy and useful tip, thanks. Will try to to update mine soon. Very handy tutorial.
Nice article. one of the bests, this website and desired to permit you be that i’ve been gratified, heading via your site’s posts. I should certainly be signing equally as much as the RSS feed and should certainly wait around for another post. best Regards