DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Google+ is a new service from Google, which can bring traffic to your blog just like other social networking sites as such as Twitter or Facebook. Today, I’m going to show you a simple piece of code to automatically add a Google+ button to your posts.

Open your functions.php file and paste the following code in it:

add_filter('the_content', 'wpr_google_plusone');
function wpr_google_plusone($content) {
	$content = $content.'<div class="plusone"><g:plusone size="tall" href="'.get_permalink().'"></g:plusone></div>';
	return $content;
}
add_action ('wp_enqueue_scripts','wpr_google_plusone_script');
function wpr_google_plusone_script() {
	wp_enqueue_script('google-plusone', 'https://apis.google.com/js/plusone.js', array(), null);
}

Once saved, a Google+ button will be automatically added to all your posts.

Thanks to Spyre Studios for the great piece of code!

Tags

9 Comments

  1. Exactly what I am looking for. Thanks! And hereis a great place for wordpress resources and tutorials!

  2. Actually, which would be better for performance?

    Calling it the way it’s shown or the way facebook and twitter are called in one of your previous functions?

    I was trying to think of the best way to call all three at the same time.

    http://www.wprecipes.com/automatically-add-twitter-and-facebook-buttons-to-your-posts

  3. Nice snippet, thanks!

  4. I try to simplify my websites, not using too many plugins but snippets and I was just looking these days for something simplified for G+. This is the perfect Google Plus PHP snippet.

    I just wish there is something similar but JS for the non PHP websites I have. Anyone?

  5. I am still wondering if I should use Google +. For now I am using only facebook and twitter, but I guess eventually I should start using Google + too. Maybe when they make it an seo signal :D. But still it should increase ctr in the serp, at least for logged in users.

  6. Great and simple 🙂

    @ Nikolay

    You better start using it (if not already implemented) as Google has now intorduced Search Plus Your World too. 🙂

  7. Can anyone tell me where in the functions.php file this code snippet should be placed?

    My first attempt putting at the bottom of the code broke the whole blog and made it inaccessible, and my second attempt putting it at the top didn’t work either. I have no clue about php, so any help on placement of this snippet would be great! Thanks in advance!

  8. Hy Chris .. I beleive you shoud put the conde in the bottom , inside the tags 😀 ? 😀

  9. This can also be done using the wp_enqueue_scripts…!

Leave a Comment

Your email address will not be published. Required fields are marked *