How to add social sharing links to your WordPress theme (without a plugin)

One of the things that I prefer to do when building a new WordPress theme is to keep performance in mind, and part of this means looking for opportunities to do reduce the number of plugins I’ll need to rely on.

One way to score an easy win is to integrate social media sharing links directly into your theme. This assumes you just need to provide users with basic functionality. If you need additional features like tweet counts and such then you may want to go a plugin route. But for straightforward sharing links, save yourself the trouble of yet another third-party dependency and do it yourself in minutes.

First, open up your code editor and add the following. Here we’re going to add Twitter, Facebook, Google+, Linkedin, and Pinterest sharing links.

<!-- begin sharing -->
<aside class="sharing">

    <a href="<?php echo urlencode(the_title()); ?>&url=<?php echo urlencode(the_permalink()); ?>" title="Share on Twitter" rel="nofollow" target="_blank">Tweet this</a>

    <a href="<?php echo urlencode(the_permalink()); ?>"
   onclick=", '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');return false;"
   target="_blank">Share on Facebook</a>

    <a href="<?php the_title(); ?> - <?php the_permalink(); ?>" onclick="'<?php the_title(); ?> - <?php the_permalink(); ?>','gplusshare','width=750,height=500,left='+(screen.availWidth/2-225)+',top='+(screen.availHeight/2-150)+'');return false;">Share on Google+</a>

    <a href="<?php the_permalink(); ?>" rel="nofollow" target="_blank">Share on LinkedIn</a>

    <a href="<?php the_permalink();?>&media=<?php echo $pinterestimage[0]; ?>&description=<?php the_title();?>">Pin this</a>

</aside><!-- /sharing -->

Save the file as sharing.php in your theme’s directory.

Next, place the following in each theme template where you want the links to show:

<?php get_template_part( 'sharing' ); ?>

If you prefer to use icons instead of text links, check out Fontello.

Have fun.

Post a comment

Fred looking at his camera phone with a dumb grin on his hairy face About

I'm an Ottawa-based web designer and front end developer who's been in the industry since HTML was written in all-caps with inline CSS. I like writing about the web as well as my other creative interests. Follow me on Twitter


Requests for help are welcome but my schedule means I may be unable to respond right away.