ShareThis Social Bookmarking Counters on your WordPress Blog

buttons

This is the whole enchilada.

// output the sharethis scripts and some CSS to hide annoying bugs from the ShareThis JavaScript
function sharethis_script() {
	?>
	<script src="http://wd.sharethis.com/button/buttons.js" type="text/javascript"></script>
	<script type="text/javascript">
stLight.options({publisher:'YOUR PUBLISHER KEY'});
</script>
	<style type="text/css">
	#menu-top-nav span,.post_sig span {
		display: none;
		position: absolute
	}
</style>
	< ?php
}

// a filter for adding buttons to the_content
function sharethis_content_filter($content) {
	if (is_single() &&; !in_category('Asides') ||
		is_home() && !in_category('Asides')) {
		$before = sharethis_button();
	}
	if (is_single()) {
		$after = sharethis_button();
	}
	return $before.$content.$after;
}

// write the button code with the appropriate st_url and st_title attributes.
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" displayText="Email"></span>
		<span  '.$st_url.$st_title.'class="st_sharethis_vcount" displayText="Share"></span>
		</p>
		';
}

// register actions and filters!
add_action('wp_footer','sharethis_script');
add_filter('the_content','sharethis_content_filter');

Extra Credit

The 4 counters used in this tutorial are just the default services included in the copy & paste code from the ShareThis site. You can see I’ve picked some different services for my blog. You can change which counters you are using by creating your own span tags with different class names and displayText attribute values. For example, class="st_digg_vcount" displayText="Digg" Will produce a Digg counter.

How do I know what to put into these attributes?

I couldn’t find a good reference for what class names to use but here’s a method that worked for me: Hover over the “Share” button, this is the counter for ShareThis. When you hover this counter it will pop up a box that lists several services and a link to display even more services. When you mouse over one of these links your browser should display the URL some where (usually somewhere across the bottom of the browser window). In this URL look for destination= For example in the URL for the Google Buzz link you will find destination=gbuzz.

Now you just add gbuzz into your class name like this: class="st_gbuzz_vcount". Next you just give it a display name in your displayText attribute like this: displayText="Buzz" (This name can be anything you want it’s just for display — but keep it short).

Some final thoughts:

I might make this into a plug-in if I thought there would be a use for it. One would hope ShareThis would eventually publish one. To make this a plug-in one would have to be able to control where the buttons are displayed and which services to have buttons for via the admin panel, and of course all the usual extra work that goes into getting a plug-in onto the WordPress Plugins Directory.

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>