Installing Google Plus Button on WordPress

If you’ve been anywhere on the internet in the past few months, you’ve seen the +1 button from Google right next to Facebook, Twitter, StumbleUpon and other social media sharing buttons.  It’s pretty clear that Google intends the “plus one” to be THE most important form of approval on the ‘net, integrating all those “plusses” into search results as well as showing them within the Google+ Plus network.

Here’s how to integrate the Google Plus button on the WordPress Twenty Eleven theme.

Installing the Google Plus JavaScript code snippet

The first step to adding the Google Plus button to this theme is to add the JavaScript code snippet that “talks” to Google.  This code can be placed anywhere in the theme, but lets install it in our footer.php file, right before the the closing </body> tag:

1
2
3
4
5
6
7
8
9
<script type="text/javascript">
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
</script>

</body>

Adding Google Plus button on front page

To add the Google Plus button to each post displayed on the first page, we need to modify the content.php file.  Like all of the other modifications we’ve made to create a child theme like creating a custom header and custom footer, copy the content.php file into your child theme folder. To display the Google Plus button underneath the title, we need to place the following code, after the <div class="entry-content"> and before the <?php the_content> tag:

1
2
3
4
<div class="entry-content">
<!-- Place this tag where you want the +1 button to render -->
<g:plusone href="<?php the_permalink(); ?>"></g:plusone>
<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>

In the Google button code, the piece of code that references the_permalink is a WordPress function that passes the link of blog post to the Google Plus button.  Without this addition to the button, Google will use the URL of the page you are on…in my case, the 3 buttons on the front page would all reference www.randyzwitch.com, which means a visitor wouldn’t be able to recommend an individual post.

Adding a Google Plus button to an individual blog post

To add the Google Plus button on an individual article or blog post using the “Standard” format in Twenty Eleven, we can do the same basic process as above, except we’ll modify the content-single.php file (again, copy this file into your child theme folder!).  We’ll add the code after the <h1> tag and before the “If” statement that starts building the content:

1
2
3
4
5
<h1 class="entry-title"><?php the_title(); ?></h1>
<!-- Place this tag where you want the +1 button to render -->
<g:plusone href="<?php the_permalink(); ?>"></g:plusone>

<?php if ( 'post' == get_post_type() ) : ?>

Technically, you don’t need to specify the the_permalink reference, since there will only be a single button on an individual blog post, and the Google Plus button will use the URL of the page.  But, it doesn’t hurt, so for documentation sake I’ve left it in.

Once you’ve added the button code on the individual page, you’re done! Visitors to your blog can now recommend your content on Google search and on the Google+ network.

Optional steps

For this tutorial, I only provided the code for modifying the “Standard” page format for a single blog post.  If you are using multiple formats across your blog, you’ll need to modify each template you are using.  They all begin with content-<format>.php, and the button code goes after the <h1> tag and before any other code.

It’s also possible to modify how large the Google Plus button is and what is displayed.  I’m using the default bubble annotation, but if you’d like to have a larger or smaller button, or just different options, you can use the code builder from Google to see the button options.

  • WordPress to Jekyll: A 30x Speedup
  • Bulk Downloading Adobe Analytics Data
  • Adobe Analytics Clickstream Data Feed: Calculations and Outlier Analysis
  • Adobe: Give Credit. You DID NOT Write RSiteCatalyst.
  • RSiteCatalyst Version 1.4.8 Release Notes
  • Adobe Analytics Clickstream Data Feed: Loading To Relational Database
  • Calling RSiteCatalyst From Python
  • RSiteCatalyst Version 1.4.7 (and 1.4.6.) Release Notes
  • RSiteCatalyst Version 1.4.5 Release Notes
  • Getting Started: Adobe Analytics Clickstream Data Feed
  • RSiteCatalyst Version 1.4.4 Release Notes
  • RSiteCatalyst Version 1.4.3 Release Notes
  • RSiteCatalyst Version 1.4.2 Release Notes
  • Destroy Your Data Using Excel With This One Weird Trick!
  • RSiteCatalyst Version 1.4.1 Release Notes
  • Visualizing Website Pathing With Sankey Charts
  • Visualizing Website Structure With Network Graphs
  • RSiteCatalyst Version 1.4 Release Notes
  • Maybe I Don't Really Know R After All
  • Building JSON in R: Three Methods
  • Real-time Reporting with the Adobe Analytics API
  • RSiteCatalyst Version 1.3 Release Notes
  • Adobe Analytics Implementation Documentation in 60 Seconds
  • RSiteCatalyst Version 1.2 Release Notes
  • Clustering Search Keywords Using K-Means Clustering
  • RSiteCatalyst Version 1.1 Release Notes
  • Anomaly Detection Using The Adobe Analytics API
  • (not provided): Using R and the Google Analytics API
  • My Top 20 Least Useful Omniture Reports
  • For Maximum User Understanding, Customize the SiteCatalyst Menu
  • Effect Of Modified Bounce Rate In Google Analytics
  • Adobe Discover 3: First Impressions
  • Using Omniture SiteCatalyst Target Report To Calculate YOY growth
  • Google Analytics Individual Qualification (IQ) - Passed!
  • Google Analytics SEO reports: Not Ready For Primetime?
  • An Afternoon With Edward Tufte
  • Google Analytics Custom Variables: A Page-Level Example
  • Xchange 2011: Think Tank and Harbor Cruise
  • Google Analytics for WordPress: Two Methods
  • WordPress Stats or Google Analytics? Yes!
  • JuliaCon 2015: Everyday Analytics and Visualization (video)
  • Vega.jl, Rebooted
  • Sessionizing Log Data Using data.table [Follow-up #2]
  • Sessionizing Log Data Using dplyr [Follow-up]
  • Sessionizing Log Data Using SQL
  • Review: Data Science at the Command Line
  • Introducing Twitter.jl
  • Code Refactoring Using Metaprogramming
  • Evaluating BreakoutDetection
  • Creating A Stacked Bar Chart in Seaborn
  • Visualizing Analytics Languages With VennEuler.jl
  • String Interpolation for Fun and Profit
  • Using Julia As A "Glue" Language
  • Five Hard-Won Lessons Using Hive
  • Using SQL Workbench with Apache Hive
  • Getting Started With Hadoop, Final: Analysis Using Hive & Pig
  • Quickly Create Dummy Variables in a Data Frame
  • Using Amazon EC2 with IPython Notebook
  • Adding Line Numbers in IPython/Jupyter Notebooks
  • Fun With Just-In-Time Compiling: Julia, Python, R and pqR
  • Getting Started Using Hadoop, Part 4: Creating Tables With Hive
  • Tabular Data I/O in Julia
  • Hadoop Streaming with Amazon Elastic MapReduce, Python and mrjob
  • A Beginner's Look at Julia
  • Getting Started Using Hadoop, Part 3: Loading Data
  • Innovation Will Never Be At The Push Of A Button
  • Getting Started Using Hadoop, Part 2: Building a Cluster
  • Getting Started Using Hadoop, Part 1: Intro
  • Instructions for Installing & Using R on Amazon EC2
  • Video: SQL Queries in R using sqldf
  • Video: Overlay Histogram in R (Normal, Density, Another Series)
  • Video: R, RStudio, Rcmdr & rattle
  • Getting Started Using R, Part 2: Rcmdr
  • Getting Started Using R, Part 1: RStudio
  • Learning R Has Really Made Me Appreciate SAS