Skip to content

Create Your Own Fast Social Sharing Buttons For WordPress

6 Dec 201033 comments Code, Social Media, Wordpress

Social Networks Logos

Image courtesy of stabilo-boss

Social bookmarking buttons are essential for any blog these days – they’re vital to helping people share content that they find useful and interesting. And with WordPress of course, you have a whole host of plugins to help you add social sharing buttons to your site.

In my experience however, it’s hard to find one that displays the sharing networks that you want to use, with the icons that you want, in the alignment that you want.

In addition, many of these plugins either use Javascript, or a whole host of external references, which are a burden on your server and really slow down your site load time. Aside from being a metric by which Google ranks sites now, it’s well known that sites with slow load times have higher bounce rates, as people look elsewhere for their information.

So I set about creating my own social sharing buttons for Do It With WordPress, which helped removed the last remaining external references on my site and speed up my load time to less than two seconds.

Find your perfect icons

Since you’ll be installing your own buttons from scratch, you’ll need the images that will represent each sharing site that you want to make available from your site.

You can use text links if you prefer, however, I strongly advise that you use images, as your readers will be searching for the familiar iconography to share your article, not a small text link.

To find icons that suit your site, just do a Google search and look for “social sharing icons”, or something along those lines. You’ll find a whole array of icon packs with no end of design varieties. For example, check out this article from Specky Boy, which has a collection of some great social bookmarking icons.

You’ll obviously need to find a set which has icons for every network that you intend to use – you don’t want to mix and match icons from different sets because it will look rubbish.

Upload all the icons to your server so that you will know the URLs of all the images. I recommend placing them in an images folder in your active theme’s folder under the wp-content folder.

Where to add the code

The whole idea of using social sharing buttons is to include them automatically on each post, so that your readers can share every article on your site. This also eliminates the need for you to put them in every post manually (though that is certainly possible if you like making life harder for yourself).

In order to do that, you need to code it into your theme files, which requires editing your theme files, namely single.php (which is the file that is used to display single articles). Have a look at this article if you need a little help in learning how to edit your theme files.

Find where you want to put your social sharing icons in single.php. If you want to put them after the content, you’ll need to look for the following line of code, which displays the main content and start inserting it after that:

< ?php the_content('Read more »'); ?>

Of course, you can put the icons elsewhere – just find the appropriate point in single.php and insert your code there instead. Before you get going though, just be sure to make a backup/copy of your file in case you mess it up.

Creating the buttons will consist of two steps: adding the images (icons) that people will click on and then adding in the code that will link to the sharing site when the icon is clicked.

Add the images

Once you’ve settled on where the icons are going to go, start adding in the images. You’ll need the URLs of the images, which you uploaded earlier.

The syntax of the img tag will be as follows:

<img src="<?php bloginfo('template_directory'); ?/>/images/twitter.png" alt="Tweet this!" />

This is based on the images being uploaded where I mentioned earlier in this article (/wp-content/themes/activetheme/images/). You can change it to a specific URL (starting with http://) if it is located elsewhere. Obviously, you’ll need to change twitter.png to the appropriate filename for each network that you’re accounting for. Likewise, the alt tag will also change (the alt tag is text that appears if an image fails to load).

Repeat this process for all the icons that you’ll be adding.

Add the code

Now you’re ready to add the code to the each image. You might even want to save your progress and view an article to see if the images are displaying the way you had hoped.

All we’re going to do is add a hyperlink to each image, so that when they’re clicked on, they go to the sharing site and send the appropriate information.

In order to add a hyperlink to each image, all you need to is add the following code before and after the img tag that you added in the previous step:

<a href="" title="">
<img />
</a>

The href and title will change depending on which network you are working on. Use the following lines of code to create a hyperlink to each sharing site:

Twitter:

<a href="http://twitter.com/home/?status=<?php the_title(); ?> - < ?php the_permalink(); ?>" title="Tweet this!"></a>

Facebook:

<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=< ?php the_title(); ?>" title="Share on Facebook."></a>

Google Buzz:

<a href="http://www.google.com/buzz/post?message=<?php the_title(); ?>&url=< ?php the_permalink(); ?>" title="Buzz it!"></a>

Reddit:

<a href="http://www.reddit.com/submit?url=<?php the_permalink(); ?>&title=< ?php the_title(); ?>" title="Vote on Reddit"></a>

Digg:

<a href="http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>&title=< ?php the_title(); ?>" title="Stumble it"></a>

Stumbleupon:

<a href="http://digg.com/submit?url=<?php the_permalink(); ?>&title=< ?php the_title(); ?>" title="Digg this!"></a>

LinkedIn

<a href="http://www.linkedin.com/shareArticle?mini=true&title=<?php the_title(); ?>&url=< ?php the_permalink(); ?>" title="Share on LinkedIn"></a>

Del.icio.us:

<a href="http://del.icio.us/post?url=<?php the_permalink(); ?>&title=< ?php the_title(); ?>" title="Bookmark on del.icio.us"></a>

Google+

<a href="https://plusone.google.com/_/+1/confirm?hl=en&url=<?php the_permalink(); ?></a>

Tweaking the Twitter syntax

By virtue of the service, you need to make sure that Twitter messages are as short as possible. You also want to leave room for the reader to add a few words of their own about your article. (Thanks to Kimberly and Brian for this suggestion)

As such, you may want to convert the post URL to a short URL. I’m going to show you how to do using the most popular URL shortening service, bit.ly.

First, you need to create a function to tell WordPress to get a short URL for your post. To do this, you will need a bit.ly account. Once you’ve created an account, go to your Account Settings and take note of your API key and your username.

Now open your functions.php file, scroll all the way to the end and paste in the following code, substituting in your username and API key for yourbitlyusername and yourbitlyAPIkey:

< ?php
function getBitly($url) {
$bitly = file_get_contents("http://api.bit.ly/v3/shorten?login=yourbitlyusername&apiKey=yourbitlyAPIkey&longUrl=$url%2F&format=txt");
return $bitly;
}
?>

Save your functions.php and head back to single.php and change the link for your Twitter button to the following, which replaces the normal full URL with a shortened URL using your bit.ly acount:

<a href="http://twitter.com/home/?status=<?php the_title(); ?> - < ?php $bitly = getBitly(get_permalink($post->ID)); echo $bitly ?>" title="Tweet this!"></a>

You can even take it a step further, like I have and drop in your Twitter username to the status message, so that you become part of the conversation. For example:

<a href="http://twitter.com/home/?status=<?php the_title(); ?> - < ?php $bitly = getBitly(get_permalink($post->ID)); echo $bitly ?> via @daclements" title="Tweet this!"></a>

Are there any networks that I didn’t cover that you’d like to see a link for? Did you have trouble getting it to work? I’d be glad to help you figure it out.

Recommended WordPress Hosting

Save 25% on HostGator hosting packages by using coupon DIWW25PCOFF
Or save $9.94 by using coupon DIWWSAVE10
Hosting packages for WordPress

About the Author

Dave Clements designs awesome websites and offers WordPress consulting services in his spare time, along with eating Wheat Thins, spending time with friends and family, watching Indie films and playing with his array of Apple products.

Like it? Share it!

Comments

  1. bbrian017 December 6, 2010

    Hi Dave,

    I’m a huge fan of adding social buttons to my website. I think the buttons you add should be determined by the niche your in. for example on the blog engage community blog I don’t add many because the articles don’t get submitted much to other sites. But I add blogengage, digg, stumbleupon, tweetmeme and that’s about it.

    On my marketing and SEO blog I have many more because those articles can be submitted to many more sites as opposed to the blog engage blog.

    You should have the tweetmeme here on your blog. right now just to tweet this I have to go to tinyurl.com add your url, get a shortened url, go to twitter and submit it. That’s a lot of manual work buddy!

    • Dave December 6, 2010

      Brian,

      I’d have to agree. I’ve limited the number of networks that I use on here because some of them just aren’t used all that much.

      I don’t have the tweetmeme button, but I do have a submit to Twitter button. It’s in the four buttons at the bottom of the post. However, the fact that you weren’t able to easily find it is indicative of an issue in itself. Did you see all the buttons? Or were you looking specifically for tweetmeme button instead of a Twitter-branded icon?

      Thanks for your in-depth comment. Always appreciated!

      • bbrian017 December 6, 2010

        Hi Dave, oh I seen the button but with twitter comes little or no available characters in a tweet. By using your current button it takes the full url which is very big. So I had to go to tinyurl.com, get a small url, I had to come back to your blog and find your twitter name so I could also add that.

        I guess it’s all about time really. Today I had spare time so I took the time to get all the needed information. On a busy day I might not and the sad part is your blog pays the price. I know if you don’t like tweetmeme there is also another one people use I see often and it also works great.. I don’t however know the name of that one.

        • Dave December 6, 2010

          Brian,

          Well, yes, there is that issue. I don’t use the Twitter website much (at all) as I use it on my iPhone, but I was under the impression that Twitter automatically shortened URLs… I could be very wrong there. I might have to think of a way around this. There must be a way I can get it to create a short URL.

        • Dave December 9, 2010

          Hi Brian,

          Just wanted to let you know that, per your recommendation, I have updated the tutorial to allow you to return a short URL using bit.ly for the Twitter status message and I’ve also incorporated this into my own buttons here on Do It With WordPress.

          Thanks for the suggestion. Really appreciate your candid feedback!

  2. Mike December 6, 2010

    Hi Dave,

    Creating your own button for your blog is a great way to speed it up, but should you concern about adding or removing the code later? I’m using some share buttons for my blog and I could say, I like them much. Although these buttons take some seconds to load and often dragged the page speed down, it’s necessary to include them inside the blog. By the way, I don’t see any share button like Share on BlogEngage, SERPD,… on this blog. You don’t like them, do you?

    • Dave December 6, 2010

      Mike,

      If you’re using a free theme from the wordpress.org repository, then yes, updating your theme in the future would be an issue as the code you entered will simply be overwritten. You either need to buy a premium theme, take note of code you insert, so that you can put it back after updating, or not update your theme.

      I am a member of BlogEngage, but I prefer to keep things on my site as clean and minimalist as possible, so while I don’t doubt that the occasional person might find a BlogEngage button useful, it just clutters up the interface for the majority of users. For that reason, I just use the four most popular networks. If someone wants to share elsewhere, they have to do it the old-fashioned way! You can’t cater to everyone, but you can cater to the masses.

  3. Kimberly Castleberry December 9, 2010

    I cant say I’m a fan of this for Twitter… it does not shorten the link which makes the tweet hard to retweet, it does not include your twitter ID which messes with relationship building for all three parties involved (you, the person that hits the button, and the person that reads the tweet). It takes more code than that to be effective with twitter and I’d highly suggest just using the code for the official tweet button, gotten easily from twitters site, and set up to include your twitter ID. Great buttons all around though!
    Kimberly

    PS: Found your blog through Brian’s recommendations on BlogEngage and glad I did!

    • Dave December 9, 2010

      Kim,

      I’ve received this recommendation from Brian also, so I really appreciate the feedback. Clearly it’s a very valid point. I’m working on a way to get a short URL for the Twitter links.

      I’m also very thankful for your thought out and candid comment. They’re not very common these days, so thanks again. Once I update the tutorial, I’ll reply to your comment again

    • Dave December 9, 2010

      Hi Kim,

      As promised, I updated this tutorial based on your feedback and have now included a section for changing the Twitter syntax to return a short URL instead of the full URL.

      Thanks again for your thorough input. It’s comments like yours that make me strive to improve and find shortcomings in my tutorials.

  4. Kimi December 16, 2010

    Not a new tip, but still very very useful for wordpress bloggers who want to learn the coding and template hierarchy.

    I too, do not use any plugins for my social media buttons.

    Thanks!

    • Dave December 16, 2010

      Glad to hear it. Looks like you also have email and Alexa Review to your sharing options as well. Can’t say I’d heard of Alexa Review before. Do you see this used a lot?

  5. Kimi December 16, 2010

    Dave,

    I have just put this alexa review not so long ago..,

    The idea comes because i removed the alexa widget, but i still want people to review my blog in alexa at least..

    But not luck so far LOL..

    Cheers,

    Kimi.

    • Dave December 16, 2010

      I suppose you’re also subject to the rule of, “If you don’t ask, you don’t get”. I’ve got to be a little better at being more forward for asking for these kind of things.

  6. AstroGremlin July 18, 2011

    I’m kind of confused. Is this code the way to make the cool Facebook and Twitter subscription buttons you have on your site? Is there a plugin that can do something similar? I’m working with a theme I will probably change so not crazy about the idea of hand-crafting with code. (And I goof up code, I’m afraid)
    In the meantime, I saw your comment above about being forward. I appreciate your low-key but super smart guru approach. So I rated your site on Alexa with this comment:
    Dave Clements really knows his WordPress and knows how to communicate in clear English prose. He takes the time to share powerful strategies (including code) to make WordPress do what users want and need. His site has zero fluff and he goes where others fear to tread – the mark of true competency. He also is patient and friendly, and does his best to answer comments respectfully (when most of us would be tempted to conclude the questioner was beyond hope). If I had a criticism of Dave it would be that he urges us all to to become coders. If I had a compliment for Dave it would be that he encourages us all to become coders! Recommendation: Be prepared to put on your thinking cap. To find an individual so fluent in both code and English is remarkable, and it is with enthusiasm that I select five stars (fantastic) as the appropriate Alexa ranking for doitwithwp dot com. AstroGremlin
    AstroGremlin recently wrote How to blog, what is a blog, why blog, what makes a good blog?

    • Dave Clements July 18, 2011

      Hey,

      This tutorial will create the kind of buttons that I have at the bottom of my posts (a set of 4). You can choose the image to go with it though, so it can look however you want it to look. You could avoid the issue of changing themes by creating a functionality plugin (which I’ll be writing about soon) and then your functions won’t be affected by changing themes.

      As for your Alexa review, I really appreciate you taking the time to do that – it means a lot to me. I am very low-key and so it’s hard for me to be bold and ask for these kind of things, so for you to step forward and do that is a big deal to me. I really appreciate the kind words and the support.

      Take care mate.
      Dave Clements recently wrote Remove Nofollow for Comment Authors with a Simple Function

  7. AstroGremlin July 18, 2011

    I see, Dave, these buttons are for sharing ala Add To Any. I keep getting frame error messages from AddToAny so perhaps a better solution on that. Still figuring out the social media angle (and just created a Twitter account for my blog) so your buttons on the upper right are intriguing – lets a subscriber find the Twitter version and follow (assuming anyone would want to! :)
    A sincere pleasure to acknowledge your value to the WordPress blogging community, Dave. Just yesterday I used your tip on No Follow to modify my comment pages to give back link juice. Your low key approach is an asset — you aren’t so busy showing off being an expert and using jargon that you forget your readers’ comprehension!
    AstroGremlin recently wrote Review of doitwithwp.com

  8. Sumon Seleem September 13, 2011

    I think you should also add the Google+ button! It’s going to have a great impact. Let me know what you think of it.

    • Dave Clements September 14, 2011

      I probably will include the Google+ button. However, at the moment, I can’t use Google+, just because Google haven’t activated it for users of Google Apps (which I am). So as soon as they get their act together, you can rest assured that it’ll be on here ;)

  9. J. Brown April 17, 2012

    Really appreciated stumbling upon this. Those social share plugins always seem to me a bit cumbersome. Thought I might chime in on the google+ front. Now that google buzz is gone, I did find a link that allows for a google+ share without javascript. Thought I might share here:

    https://plusone.google.com/_/+1/confirm?hl=en&url=http://www.your-url/

    Cheers.

    • Dave Clements April 19, 2012

      Ooooh, I was looking for this for a long time before I just gave up and installed the official button. This is a nice add – I’ll have to update the tutorial.

  10. Steve Floyd April 18, 2012

    Thanks for putting this together Dave, it was a life saver on one of my projects. The blog we were loading social share buttons on was taking way, way too much time to load which as you know is bad for SEO (not to mention user experience). Using your method dramatically sped up the load time, it is appreciated.

  11. J. Brown April 19, 2012

    Two more things for anyone trying to implement homemade share buttons:

    1. The Facebook share button has been completely depreciated and the above link no longer works. In fact, most of the social share plugins now have inactive facebook share buttons too. However, I found a fix. At first glance, to amateurs like myself, it may seem daunting but I managed to get it to work without much trouble. Here is what I did:

    - Log in to Facebook Developers (https://developers.facebook.com)
    - Create an app. Give it a title like “Share.” Canvas url: http://www.yoursite.com
    - After app is created, copy the API ID/Key.
    - Use the following code for your facebook button:

    <a href="http://www.facebook.com/dialog/feed?app_id=XXXXXXXXXX&link=&redirect_uri=http://yoursite.com/“>

    There are more parameters that can be added:
    https://developers.facebook.com/docs/reference/dialogs/feed/

    2. In order to avoid markup validation errors, replace the “&” character in the above links with “&”

    We don’t need no stinking plugins.
    Cheers.

    • Dave Clements April 20, 2012

      Thanks for that. Truly appreciate you coming back to update everyone with current information. People like you rock :)

    • J. Brown April 20, 2012

      Correction: Please replace the above code for a facebook share button with this:

      href=”http://www.facebook.com/dialog/feed?app_id=XXXXXXXXX&link=&redirect_uri=http://yoursite.com/”>

      Late night, hasty paste.

    • J. Brown April 20, 2012

      Actually the code is just not pasting.

      href=”http://www.facebook.com/dialog/feed?app_id=XXXXXXXXX&link=&redirect_uri=http://yoursite.com/”>

      Add php question marks.
      Use ampersand instead of “&”

      • J. Brown April 20, 2012

        Last try. Dave, maybe you’ll get this right for everyone.

        After &link= goes the php the_permalink() function.
        J. Brown recently wrote Secrets of Tantric Sex

  12. J. Brown April 21, 2012

    OK. You probably think I’m crazy now but I have a question about the twitter tweak. I got it working nicely and having an instantly created shortened url that can then be tracked is super cool. One small thing: I like to include share buttons on my index page and when you use the bitly function on the index page it ends up creating a shortened bitly url for every post on my site. Not an issue on a single post, only in the index. Wondering if there is a way to tweak the function to limit it to just the post associated with the click? I’m a complete hack myself and have no idea if or how such a thing could be achieved but maybe someone else out there knows how to make it happen. If so, this slightly OCD blogger will be a happy man.

    • J. Brown April 21, 2012

      This question can be disregarded. What happens is: when the twitter share button is used once on the index page, shortened urls are created for all the posts in the index. However, this only happens once. The same shortened urls will be used anytime someone shares from the index. I was concerned that new links would be created every time but this is not the case.

  13. frank May 4, 2012

    Thanks for this post! I was looking for a way to add my own custom share buttons without using a plugin and this post is exactly what I needed as I build a theme from scratch and speed is my main goal.

    Question – on the Twitter share, I followed the instructions but for some reason on the share screen it appears as “WordPress Post Title Example – via @twitterusername”

    Any ideas as to what I’m doing wrong as to why a link to the post is not being inserted and the code is appearing instead?

    • frank May 4, 2012

      Update: There’s an extra space just before the < ?php in the title code. If you're having the same issue as I was, just remove this extra space and it works perferctly. Thanks!

  14. frank May 4, 2012

    Any plans for a Pinterest option?

    • Dave Clements May 7, 2012

      I suppose I might have to add one, yes, given the popularity of it.

Add a Comment

Name - Required

Email - Required

Website - Optional

CommentLuv badge