How to add Google +1 button to your WordPress theme
Google plus one button makes searching social. When you click a +1 button on a site, your friends see that page on higher levels when they make a related search. Here’s how to add this button to your wordpress site.
1. Header code
First open functions.php
and add the code below at the bottom of the file:
function shailan_header_codes(){ ?> <!-- Google Plus One --> <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> <?php } add_action( 'wp_head', 'shailan_header_codes' );
If you see a ?>
sign at the end of the file just remove it.
This code will add the required script to the head part of your site.
Now you can use plus one code in your single and page templates.
2. Button code
Open single.php in your theme folder and paste this code before <?php the_content(...) ?>
call (In some themes that call is located in loop.php
or loop-single.php
files):
<g:plusone size="tall"></g:plusone>
This code is for “tall button with count”. You can get other button codes here.
You can also add this code after the_content()
call, So users will see it before and after the content.
If you want it to appear on your pages, then you need to repeat this step for page.php
file, too.
And the last step; Save and Upload your files.
Bonus Tip
For the best practice, create a file called share.php
and put all your button codes in that file. Then include this file anywhere you like using:
<?php get_template_part('share', 'single'); ?>
This way your files will be more organized and re-usable.
Cheers!