Custom AddThis share button

I wanted to have an AddThis share button that would fit better with the site’s design, and if you’re reading this post on its own page you can scroll down and see what I mean. While AddThis provides plenty of ways to customize their buttons, there is no way to assign your own image to the icon, which means doing it the hard way… well, relatively speaking of course.

There are three main steps to creating a customized share button. There’s some javascript to include in the footer.php, few lines of html to add at the end of your posts, and of course some styles and the icon itself.

JavaScript

You’ll want to add this js code in the footer file as probably the last piece of code. This means it won’t be loading until everything else is done. You can read detailed explanation of what you can do on AddThis’ API page but the short of it is — include this script file “http://s7.addthis.com/js/250/addthis_widget.js”. If you want to keep track of various stats AddThis provides, you’ll need to provide your username. This is done with the “addthis_config” variable. So for example, what I have is

<script type="text/javascript">
var addthis_config = { username:"2late2die", data_track_clickback:true };
</script>
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js"></script>

HTML

The HTML is pretty much just a regular link, but with certain href target, class and a couple of special addthis attributes. You can style it anyway you want, as you would any other link. Here’s how said link might look

<a href="http://www.addthis.com/bookmark.php?v=250&amp;username=[username]"
  class="addthis_button" addthis:url="<?php the_permalink() ?>"
  addthis:title="<?php the_title_attribute() ?>">Share</a>

“http://www.addthis.com/bookmark.php?v=250&username=[your username here]” is the url the link should be pointing to. It must have the “addthis_button” class, otherwise AddThis js won’t recognize it, and then the addthis:url and addthis:title attributes. These attributes are self explanatory, just remember to use PHP so that they are assigned dynamic values. If you have any code, or plugins, that insert tags with attributes or class names into the title (like in my case, wp-typography, which puts <span class="caps"> around any caps-only words, like acronyms and such) then use the “the_title_attribute()” function, instead of the typical “the_title()”. It strips the tags and escapes any problematic characters.

CSS

This is really all up to you, depending on how you want to style the button and the tooltips with the various sites. By default the AddThis scripts provides its own style, so if you don’t mind the white tooltip box, all you’ll need to style is the link itself. If you want to style the tooltips as well, then you’ll need to dig into the generated code for them and the styles used by default, and figure out which ones you want to change. (You can check out my styles to see how I gave it a dark theme by switching a few colors.)

If you like optimizing your code you might decide to not include the AddThis styles file at all. You can do this by adding “ui_use_css:false” to “addthis_config”, i.e.

var addthis_config = { ui_use_css:false };

That CSS file weighs in at about 52k, which is actually quite hefty. There are, however, two reasons to keep, instead of removing it.
First of all, by removing it, you will have to provide all the styles to AddThis widget from scratch, as it were, including all those icons for the various services, which are backgrounds of the links, not image tags in the HTML. It’s certainly doable but it’s a hastle that might be not worth it at the end (depending on how anal you are about optimizing your code). The other reason is that, because AddThis is a fairly popular widget, many of your users are likely to have already downloaded that css file when they encountered the AddThis button on other sites. This means it’s cached in the browser and takes little to no time to download.

Now, as for that FB “like” button, looks like it might be tricky…

(4) Comments

  1. Pere
    September 1, 2010

    Excited to find this article. Any more details as to how you modified the AddThis CSS to provide your own styles? Not so clear in your article — did you override their styles in your style sheet? Thanks!

  2. Ilia
    September 1, 2010

    did you over­ride their styles in your style sheet?“
    Yeah, exactly that. If you look at the stylesheet file for this page, towards the end of the code there are the styles for the addthis button, and it’s just straightup overrides, most with !important attribute.
    The catch with these is basically you need to know which styles to override, which means going through the generated code for the tooltip box,.

  3. Simon
    March 3, 2011

    Hello, I am using WordPress to facilitate my website creation, When I install AddThis as a plug-in the default files get dispatched to the server..

    I am trying to upload my own image in place of this one:

    AddThis Share Button

    What I did initially was just replace the .jpg with my own, under the same exact file name, to avoid any problems.. but obviously this wasn’t enough to make the cut, so I am now trying your tutorial..
    I can’t seen to locate a file where to stick this code:

     
    <a href="http://www.addthis.com/bookmark.php?v=250&username=[username]"
      class="addthis_button" addthis:url="" addthis:title="">Share</a>
    

    And also — what is the .css file called where I would stick the .CSS code you provided?

    The .css files I have are:
    options-page.css
    addhis.css

    One last thing — between which tags on each respective file does each code need to go?

    • Ilia
      March 14, 2011

      Simon, that button image goes into the code you use to put the share widget in, i.e. the code you posted, the one you don’t know where to put. That’s the code you put at the end of your posts, inside “single.php”, and in your case, you want to replace “Share” with regular img tag pointing to your custom image.

      And as for the CSS code, that goes into your regular style.css file, or whatever css file you use for the site.

Leave a Reply

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>