Home

Wilder Tweedale - blog

Navigation

Quick Tip: Automatically Add Google +1 Button Under Each Post in WordPress

This tip can be applied to any other content you’d like to dynamically add to the end of your posts, but here’s how to add a Google +1 recommendation button under each post:

Open your theme’s functions.php file and paste the following code:

For many other social sharing buttons you can skip the part about adding a script into the head tag, but the Google +1 button requires this to function.

Related posts:

  1. Conditional IE Styling in WordPress

22 Responses to “Quick Tip: Automatically Add Google +1 Button Under Each Post in WordPress”

  1. Kapil Kumar says:
    Thanks for sharing it with us but what are the benefits of adding Google +1 on the wordpress page? If ever we change our theme will it be same or have to paste it again?
    • Wilder says:
      If you change your theme, WordPress will be accessing the new theme's functions.php file, and will not show the button. So, yes, to show the button in the new theme you'll have to re-paste it into that theme's functions.php file. The benefit of using this method of using WordPress' hooks to add content to your site is you can add any content you like -- scripts, html markup, shortcodes, text, images, video links. Using functions.php to add or manipulate WordPress' output is especially useful when you cannot find a plugin to do exactly what you want.
      • Kapil Kumar says:
        Thanks Wilder, For your clarifications and also help me to understand the benefits of it. Now, I have also added it in my wordpress and will recommend it to my friends to do it. Thanks Again
  2. AJ says:
    I've got this code running on my WordPress Site, works fine and looks good. Thanks.
  3. Dudu Do SEO says:
    Very nice! Tested and aproved. It's work fine!
  4. Køreskole says:
    Thank you, but I can`t find the button on this page ? Would be nice to vote :-)
    • Wilder says:
      Right you are. It should be there now. I was experimenting with different social bookmarking plugins and forgot that I removed it. If anyone would like to use the share buttons I've used below my posts, search for 'AddThis Social Bookmarking Widget' in the WordPress plugin directory.
  5. Wilder says:
    Update: Mashable recently published an article detailing an alternate method of adding the +1 button to a widget-ready area in your theme: http://mashable.com/2011/06/01/plus-1-button-wordpress/ As long as your theme has a widget-ready "sidebar" underneath the post content, this way could be simpler for some people.
  6. Colleen says:
    Is it possible to move it to the top of your post? I'm not sure what I would need to change in order to do so but would love to move it if possible.
    • Wilder says:
      Yep, that's possible. Just alter the WordPress filter in the code above which modifies the $content like this: function add_google_plus_one($content) { if(!is_feed() && !is_home() && !is_page()) { $content = 'PUT_GOOGLE_PLUS_ONE_TAG_HERE' . $content; } return $content; } Where my code says PUT_GOOGLE_PLUS_ONE_TAG_HERE, replace with the google plus one tag which looks like '< g : plusone>< / g : plusone >' as shown in the post above (I put spaces in the tags to prevent it from rendering a +1 button instead of just showing the code). WordPress is actively rendering it in the comments so I can't type it unfortunately without showing the actual +1 Button.
  7. I think this is the easiest way of add Google +1 button to each post in Wordpress. Good job. Thanks.
  8. vivojack says:
    So I am wondering if I am a moron. I pasted your code into my functions.php file for my theme, uploaded it, refreshed my page and... nothing. I disabled all my plugins and removed the code I had added to add a google +1 button to the top of the page and... still nothing. I am using a modified version of the 2010 theme. Might I have inadverently removed a hook that I need? I still have wp_head() in the header and get_template_part( 'loop', 'index' ); in my index file. Any thoughts? I love the idea of adding the social media icons this way so I have complete control. Thanks!
    • Wilder says:
      Hmm. I'm not quite sure why that might be happening; I just tested both the below and above versions of the code on my local (WampServer) WP installation running the Twenty Ten theme. It could be something in the 'modified' version of your Twenty Ten theme; mine is just a vanilla, out-of-the-box version. You shouldn't have to disable all your plugins, but make sure when you're pasting the code to generate a +1 button above each post, that you are replacing only the add_google_plus_one function from my main post, not the whole code snippet. (You should still have the add_google_plus_one_script function below it, as well as the add_filter and add_action lines.) If you've pasted the code properly (it might have to be at the bottom of your functions.php file), then I'm not really sure. It could be some unknown factor in your modified theme, or quite honestly my code could just not work with every theme. Good luck with your experimentation!
      • vivojack says:
        Fair enough. I will play around with it. I will try it on a clean install of the Twenty Ten theme and see if that works. One thing you wrote threw me though. "...make sure when you’re pasting the code to generate a +1 button above each post, that you are replacing only the add_google_plus_one function from my main post, not the whole code snippet." What do you mean by "replacing only the add_google_plus_one function"? I just took your above code in its entirety and pasted it to the bottom of my functions.php file and uploaded it. Was there something I was supposed to be replacing? Thanks again!
        • Wilder says:
          Oh all I meant was that if you are trying to get the button to display above your posts, then I assume you are using the code snippet in one of my comments on this post (just scroll up a bit; it was in reply to one woman's comment). If that's the case and you are copying that snippet, I was just cautioning you not to replace the entire chunk of code you got from this blog post with that snippet, but only the function called 'add_google_plus_one'. The other function, 'add_google_plus_one_script', must of course still be present in your functions.php file, or the tag will not be processed. Sorry for the bad english ;)
          • vivojack says:
            now I understand. No, I was just cutting and pasting the code at the top. So anyway, I think I know why this was not working for me. this code snippet places the Google+1 button on the single.php pages but not on the index.php page under each post as it is listed on that page correct? So I was trying to get it to show up on the index.php page and never saw the button, but as I was just playing around with it, I clicked through to view a post by itself and there it was. Do you know how I could add it to the index.php page under each of my most recent posts? Thanks for a third time! PS I Google+1'ed your post as it works exactly as you describe, I just missed it!
          • Wilder says:
            My code has a statement which prevents it from rendering on WordPress 'pages': if(!is_feed() && !is_home() && !is_page()) { $content = 'PUT_GOOGLE_PLUS_ONE_TAG_HERE' . $content; } return $content; } To make the +1 button display on both posts and pages, simply remove the part of the if statement that says ' && !is_page()' ...this mandates that the plus one button will not run if the current page is a WordPress 'page'; remove it, and the plus one button will display on both posts and pages. So your modified function should look like this: if(!is_feed() && !is_home()) { $content = 'PUT_GOOGLE_PLUS_ONE_TAG_HERE' . $content; } return $content; }
  9. Eli says:
    how to add it automatically sir to a normal, none wordpress site? and is it possible to make it appear when a user post a link only, you know when you google, you see the +1 on each link so lets say i have a social bookmarking site,like digg.com, is it possible to make that +1 appear whenever a user post a link on the ?
  10. My brother suggested I may like this website. He used to be entirely right. This put up actually made my day. You cann't believe simply how so much time I had spent for this information! Thanks!
  11. I wonder if it also has a dislike feature which is frequently abused for example on YouTube. I understand that any rating counts for more than no rating but rating with the intention to do harm rather than good to someone is pathetic. We will try to get as much as +1 we can because we know the benefit of it with seo prespective (and soon we will see many people started to sell +1 as well) but a normal user do not know about it and where facebook like button is one step ahead from it.
  12. basekit says:
    I have got this running on my Wordpress site. I am rubbish with coding so this really helps! Thank you!