Your Short URL for Twitter

Everyone seems to be using Twitter at the moment and it was with this in mind that I decided to include in this site the “Tweet this Post” icon that you can see to the right of each post title.  What is a little different about this Tweet This button is that instead of using one of the many URL shortening services, it uses WordPress functionality to keep the shortened URL on your own domain name.  This post with talk you though how I did this.

First of all it is nothing complicated.  The WordPress default permalink structure is always much shorter than the pretty permalink version of your URLs and therefore I thought why not use this when adding link to Twitter.  The problem is, is that you don’t want to change your permalink structure of your site just so that you can use the links in Twitter.  The great thing is, is that you don’t have to.

Lets use this post as an example.  In the WordPress dashboard I can see that WordPress has assigned this post a post ID of 55. However the permalink of the post is “http://equalmark.net/tutorials/shorttwitterurls”.  This permalink is good as it makes sense to the reader, but it is long.  The great thing with WordPress is that if you actually use the non pretty permalink version i.e. http://equalmark.net/?p=55 it still takes you to the same post and replaces the URL with the correct ‘pretty’ version.  So the question I was trying to solve was how to generate this link for each post.

The best way was to create a PHP function that could be used in your themes function.php file and then call the function wherever it is needed in the theme template files.  I added the following to functions.php.


<?php

// simple function to get a shortened version of your posts URL for twitter.  Uses the format yourdomain.com/?p=postid
function tweeturl() {
$thispost = bloginfo('url');
$tweetmiddle = "/?p=";
global $wp_query;
$theid = $wp_query->post->ID;
echo $thispost . $tweetmiddle . $theid;
}
?>

Lets just take a look at exactly what this does.  Line 4 sets up the name of the function that you need to call in your themes template files to make use of the functions.  Line 5 gets the blogs URL (in this case http://equalmark.net) and stores this in the variable $thispost.  Line 6 builds the middle part of the URL which all your posts will have and stores this in the variable $tweetmiddle. Line 7 and 8 get the current posts ID and then the rest is a matter of stitching everything together.

So to use this in your theme you will need to add the following to your themes template files:

<?php tweeturl(); ?>

That code will output or echo the current posts URL in the shortened version.  I have used it on this site like this:

<a href="http://twitter.com/home?status=Been reading this great article - <?php tweeturl(); ?>">Tweet this Post</a>

The only different is instead of clicking on the text “Tweet this Post” I have replaced this with an image.

The benefits of using a function like this rather than one of the more popular URL shortening services is that whenever you want to include a link in your tweets that is your domain it means that you can keep your domain name in the actual Tweet content.  This means that more people will see your domain name and therefore could increase you traffic.  Let me know how you get on and any problems or issues (hopefully not) that you may come across.

Leave a Reply