Quick Tip: Automatically Add Google +1 Button Under Each Post in WordPress
June 1st, 2011 by WilderThis 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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//Google +1 Button below each post
function add_google_plus_one($content) {
if(!is_feed() && !is_home() && !is_page()) {
$content .= '<g:plusone></g:plusone>';
}
return $content;
}
add_filter('the_content', 'add_google_plus_one');
//add Google +1 Button script in head
function add_google_plus_one_script() {
if(!is_feed() && !is_home() && !is_page()) {
echo '<script type="text/javascript" src="http://apis.google.com/js/plusone.js"></script>
<script src="http://apis.google.com/js/plusone.js" type="text/javascript"><!--mce:0--></script>';
}
}
add_action('wp_head', 'add_google_plus_one_script'); |
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:
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.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; }