ShareThis Social Bookmarking Counters on your WordPress Blog

buttons

So I was really impressed with the new buttons that ShareThis is offering now but was disappointed to find out that they don’t currently support them for WordPress (If you use their WordPress plug-in you get the old version of the ShareThis button).The new ShareThis WordPress plug-in has been released and you can find it here. But, they did provide some copy & paste code, so I figured where there’s a will there’s a way (As you can see from the counters below this post there’s a happy ending to this story!).

NOTE: This tutorial involves making edits to the functions.php file in your WordPress theme.

So here’s the copy & paste code provided by ShareThis:

This code goes in the loop with the post:

<span class="st_twitter_vcount" displayText="Tweet"</span>
<span class="st_facebook_vcount" displayText="Share"></span>
<span class="st_email_vcount" displayText="Email"></span>
<span class="st_sharethis_vcount" displayText="Share"></span>

This code goes in the footer of your page:

<script src="http://wd.sharethis.com/button/buttons.js" type="text/javascript"></script>
<script type="text/javascript">
stLight.options({publisher:'YOUR PUBLISHER KEY'})
</script>

This particular code is for the vertical counters if you pick a different button type you will get different results. Note however, that if you aren’t going to use the counters then you may have issues outside of the scope of this tutorial. I’ve provided the above as an example of what the code looks like when we start out, you should really go to the ShareThis site yourself (sign up if you haven’t already) and copy and paste the button code from them because it will already have your publisher key embedded in it.

How to add the URL and title of your post:

ShareThis provides some attributes to control your counters. You need to add the st_url and st_title attributes to each one of the spans in the loop code shown above. If you don’t add these attributes then by default the counters will share and track the URL and title of the current page. Which is fine if we are viewing a single post, but on the home page for example, the buttons will be tracking and sharing the home page and not the individual posts with which they are associated.

How do we fix this?

You will need to use the get_permalink() and get_the_title() functions to return the URL and title of the current post in the loop. Then insert these values into all of your spans. This brings us to our first function which we will add to functions.php found in your themes directory.

sharethis_button()

function sharethis_button() {
$st_url = 'st_url="'.get_permalink().'" ';
$st_title = 'st_title="'.get_the_title().'" ';
	return '
		<p>
		<span  '.$st_url.$st_title.'class="st_twitter_vcount" displayText="Tweet"></span>
		<span  '.$st_url.$st_title.'class="st_facebook_vcount" displayText="Share"></span>
		<span  '.$st_url.$st_title.'class="st_email_vcount&amp" displayText="Email"></span>
		<span  '.$st_url.$st_title.'class="st_sharethis_vcount" displayText="Share"></span>
		</p>
		';
}

First we write our post’s URL and title to variables including the rest of the attribute definitions as well. So the value of these variables would be something like: st_url="http://www.piddmedia.com/tutorials/sharethis-social-bookmarking-counters-on-your-wordpress-blog" and st_title="ShareThis Social Bookmarking Counters on your WordPress Blog" Then we return our original spans but with the new attributes inserted into each one. I also wrapped the spans in p tags so the counters don’t misbehave with the layout. You might add some other styling depending on your needs.

NEXT:How to insert the counters into your posts:

About C. Scott Asbach

Scott is a freelance web designer/developer and stay-at-home dad. He holds an AA degree in Electronic Imaging and Media Arts and is going back to school to earn a BA in Computer Information Systems.
This entry was posted in Tutorials and tagged , , , , . Bookmark the permalink.

Related Posts:

5 Responses to ShareThis Social Bookmarking Counters on your WordPress Blog

  1. I was doing some cross-browser testing and noticed that most versions of IE don’t like buttons.jsI’m no longer having issues with this script in IE (except on closing the ShareThis popup, but that’s minor). so I’m using Reverse Conditional Comments to remove the script from everything less than IE 8 (there’s still some error messages in 8 but at least the buttons still work). The comments look like this: <!--[if gt IE 7]<!--> stuff in the middle <!--<![endif]--> and are wrapped arround the script and style tags just inside the php tags. You will find that code inside the sharethis_script() function. Hopefully the fine people at ShareThis will work out the IE issues and the other weird class name issues with WordPress so I don’t have to keep coming up with hacks for buttons.js

  2. UPDATE: Added dynamic st_title attribute along with the st_url attribute, should have done this in the first place:P

  3. Jon Bishop says:

    Didn’t know ShareThis was doing vertical count buttons until now. Feel kind of silly just reading about this.

    I am a big fan of what I call “actionable” social bookmarks so this is huge. Of course I still think using the official buttons is best its nice because these buttons are more or less the same size. I’m hoping to incorporate these buttons into the next version of my own WordPress plugin. We shall see.

    By the way, nice twenty-ten child theme.

    • I like that with the ShareThis API it’s pretty simple to change which social buttons I display and in what formats, and with one API I don’t have to remember or hunt down how to customize each button every time. I also like the level of control you can get coding it yourself in a theme or plugin versus using the official plugin. It was a necessity before the official plugin was released but now it’s just a preference.

      And thanks!

  4. Pingback: Joe Vaughn for Congress | piddmedia

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>