Skip to content

Improved Twitter share button for WordPress

I recently came across a handy little code snippet by Christian Heilmann aimed at creating a scriptless, imageless no third-party Twitter share button for WordPress that did exactly what is said on the tin, but needed a very minor tweak to improve the output.

Now you might be thinking, why not simply use the official version Twitter provides? Sure it has more functionality but I wanted something simple that I had more control over in terms of styling and more importantly, didn’t make so many HTTP requests to Twitter – therefore optimizing my site a little for mobile.

The solution by Christian worked great, with the exception of a few validation issues. To solve this I updated the code snippet as follows:

<a href="http://twitter.com/share
   ?url=<?php the_permalink();?>
   &amp;text=<?php echo str_replace(' ', '%20', get_the_title());?>
   &amp;via={your twitter name}" target="_blank">Share on Twitter
</a>

This worked a treat. Simple, valid code that didn’t request any of the extra bloat you get with the official Twitter buttons. Whilst these validation issue were unlikely to cause any real world problems, the perfectionist within me though it was a worthwhile improvement.

A big thanks to Christian for providing the initial code snippet to save me the time creating it from scratch.