Spring Sale! Save 15% on WordPress books with code: SPRING22
Web Dev + WordPress + Security

Stupid Twitter Tricks

[ Twitter ] Might as well face it, Twitter is here to stay. Not that it’s all that bad, just used to be a lot more laid-back and enjoyable. These days it seems to have been taken over by the lowest common-denominator, mostly high-school twits or useless commercial propaganda. Even so, I still enjoy tweeting the occasional profound thought once in awhile, and even like to play around with various types of “advanced” Twitter functionality. You know, cool stuff like including “Tweet This!” links with short URLs, showing off my number of Twitter followers, displaying the number of tweets for each post, and even backing up my marvelous tweets quickly and easily. As you might have guessed, these are the kind of stupid Twitter tricks that you will find in this article. So kick back, relax, and enjoy these twitterific techniques that will make you go.. tweet.

“Tweet This!” short links for posts

I like to include an easy way for readers to “tweet” my posts, but the URLs are generally way over Twitter’s 140-character limit. Fortunately, we can use PHP’s handy file_get_contents function to grab short versions of our URLs from a free minifying service like TinyURL.

All we need to do is add the following script to your web pages or your functions.php file:

function shortenURL($longURL) {
	$shortURL = file_get_contents("http://tinyurl.com/api-create.php?url=".$longURL);
	echo $shortURL;

Then, in the location where you would like to display your shortened “Tweet This!” links (e.g., after your post loop), call the function and create the link with the following code:

<a href="http://twitter.com/home?status=Reading:%20<?php the_title(); ?>:%20<?php shortenURL(get_permalink()); ?>" rel="nofollow">Tweet This!</a>

This is basically a link using the Twitter API to specify the contents of our tweet. For each post, the value of the_title() and get_permalink() will change to reflect its title and URL, respectively. If you are using a platform other than WordPress, you will need to use the equivalent of these dynamic tags.

Once you have this code in place, it will generate “Tweet This!” links that send the following information to Twitter (using this post as an example):

[ Reading: Stupid Twitter Tricks: http://tinyurl.com/yhz5ok3 ]

As you can see, the original long URL for this post was automatically shortened via the TinyURL service. Nice, clean and effective.

For more social media links, check out my comprehensive article, Fully Valid, SEO-Friendly Social Media Links for WordPress.

Custom short links using your own domain name

If you would rather not rely on a shortening service (such as TinyURL) for your shortened links, fret not — it is easy to create your own, customized URLs. There are several good reasons why you might prefer to use your own domain for your shortened URLs, including:

  • preservation of valuable link juice
  • prevention of link loss if the shortening service dies
  • control over the appearance and branding of your links

The key to setting up customized shortened “Tweet This!” links is the inherent post ID that is associated with each of your posts. Regardless of whether or not you have permalinks setup, every one of your posts has its own unique post ID, such as seen in these examples:

We can use these post IDs to fashion our own shortened URLs, and then use those shortened URLs to create our own customized “Tweet This!” links. All we need is touch of HTAccess placed in the same .htaccess file as our WordPress permalinks:

RewriteRule ^x/([0-9]+)$ http://domain.tld/?p=$1 [R=301,L]

This directive will redirect our custom shortened URLs to our default WordPress URLs, which WordPress will then redirect according to any existing permalink rules. Once this technique has been implemented, the following redirects will take place for each of your custom URLs:

[ Diagram showing the flow of redirects used in this technique ]

Once we have the HTAccess code in place, we use the same basic markup as in our previous example to create the link. Add the following code to the desired location in your theme file (e.g., after the post loop):

<a href="http://twitter.com/home?status=Reading:%20<?php the_title(); ?>:%20<?php echo get_option('home'); ?>/x/<?php the_ID(); ?>" rel="nofollow">Tweet This!</a>

This code will create “Tweet This!” links that send the following information to Twitter (using this post as an example):

[ Reading: Stupid Twitter Tricks: https://perishablepress.com/x/719 ]

As you can see, the original long URL for this post is now a customized short URL using the same domain. Feel free to change the “x” to whatever you want — just remember to edit both the markup link and the .htaccess directive if you decide to do so.

I should also note that, for any of these “shortened” URL examples, we are only using Twitter as an example application. Many sites these days simply include a shortened version of the post URL for people to copy & paste anywhere they wish. To do something similar with the current technique, we would simply include the following snippet:

<?php echo get_option('home'); ?>/x/<?php the_ID(); ?>

Drop-dead simple short URLs using the post ID

For the sake of completeness, keep in mind that we can use the post ID to (re)create our original WordPress URL structure:


That’s pretty short, and may work perfectly for your short links if the format is suitable for you. All we would need to do is include the following code into your WordPress theme:

<?php echo get_bloginfo('url')."/?p=".$post->ID; ?>

WordPress will automatically redirect such URLs to the corresponding permalink (if you have permalinks enabled).

Display the number of your Twitter followers in plain text via JavaScript

Another fun thing to do with Twitter is show off your subscriber number on your website. Many sites these days prominently display two numbers: one for their Feedburner subscriber count and another for their Twitter follower count. Displaying either count with a button or badge is a no-brainer, but displaying the numbers in plain text allows for greater stylistic control. Plain text is desirable because you can do just about anything with it, whereas a badge just kind of sits there, looking ugly.

To get our Twitter follower count in plain text, we need to include some free JavaScript made available to us at TwitterCounter. Just place this code wherever you would like to display your follower count:

<script type="text/javascript" language="javascript" src="http://twittercounter.com/widget/index.php?username=username"></script>

Simply edit the “username” to match your own and you are off to the races.

Display the number of your Twitter followers in plain text via PHP

If you would rather not rely on JavaScript and a third-party service to deliver your follower count data, we can use PHP’s excellent curl() functionality (edit the “www.domain.tld” to match your own):

function curl($url) {
	$ch = curl_init($url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch,CURLOPT_HEADER, 0); 
	$data = curl_exec($ch);
	return $data;

That curl snippet will grab the content of the input URL and prepare it for parsing via the following code:

function followerCount($username) {
	$followers = curl("http://twitter.com/statuses/user_timeline/".$username.".xml?count=1");
	$xml = new SimpleXmlElement($followers, LIBXML_NOCDATA);
	return = $xml->status->user->followers_count;
echo followerCount("username");

This second snippet uses SimpleXML to parse the curl output via the Twitter API. SimpleXML is pretty common on most servers running at least PHP 5. The only thing you need to edit is the “username” in the last line. Everything else is roses. Drop this code into a web page and enjoy your follower count displayed anywhere you like, and in plain-text, no less.

Display the number of tweets for each page or post

If you are running SimpleXML (you probably are, ask your host), displaying the total number of tweets for any given post or page is easily accomplished with the following slice of PHP:

function tweetCount($url) {
	$content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url);
	$element = new SimpleXmlElement($content);
	$tweets = $element->story->url_count;
 	echo $tweets . " tweets!";

If you are using WordPress, you may place that code into your active theme’s functions.php file. Then, to display the total number of tweets for your posts, you would add the following function call to your post loop:

<?php tweetCount($post->permalink); ?>

You can also show the number of tweets for a particular page, even if it is not on your site. For example:

<?php tweetCount("http://perishable.biz"); ?>

Backup your tweets

Once you have been tweeting for awhile, you may begin to wonder what would happen if Twitter ever lost all of your tweets. It could happen, and if it does, do your tweets contain anything of value? If they do, I would highly recommend making regular backups (like monthly or something) of all your tweets. Doing so is easy. All you need to do is login to your Twitter account and enter the following URL into your browser:


Before this will work, you need to replace the “username” with your actual username, and also change the “n” with the number of tweets you would like to download. To download all of your tweets, check your number of updates on your Twitter profile page. After entering this URL into your browser, you will be taken to an XML page containing your tweets. To make a backup, just save that page to a safe location on your computer.

Now you can rest so easy just knowing deep down that all your precious tweets are safe and sound ;)

Jeff Starr
About the Author
Jeff Starr = Designer. Developer. Producer. Writer. Editor. Etc.

15 responses to “Stupid Twitter Tricks”

  1. Rob Duncan 2010/07/15 12:26 pm

    permalink); ?>

    Doesn’t seem to be working for me… url in “ ” seems to work fine… hmmmm

  2. Jeff Starr 2010/07/18 3:42 pm

    Just a friendly reminder to wrap your code in <code> tags to prevent WordPress from eating it. Thanks!

  3. Rob Duncan 2010/07/19 5:05 am

    <?php tweetCount($post->permalink); ?>

    just can’t get this working…

Comments are closed for this post. Something to add? Let me know.
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
Blackhole Pro: Trap bad bots in a virtual black hole.
Coding never ends. Bring a snack.
Not posting anything online usually means I’m busy working on my next project.
Today is my 50th B-day :)
Deep in the code.
Making good progress on my next pro plugin, Simple Ajax Chat Pro :)
Events definitely are speeding up. For those who are paying attention.
New wave of spam comments easily blocked, add =https:// to WP Discussion settings ▸ Disallowed Comment Keys.
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.