This is quite a simple thing to do and it is quite easy to add a little link to your Wordpress posts. If the user is logged into Twitter on the web (which isn't all that common with multiple Twitter apps taking over) then when they click on a link they will be taken to their Twitter profile and their status box will contain the text you have set. You won't actually be able to update a users status for them, they have the final control over that, but some users will update their status so it is worth doing.
This is all done through a URL using the status parameter, like this.
Because this is a URL you will need to encode it properly in order for Twitter to understand it. Using Wordpress you need to put a call to the urlencode() function to encode any string you might want to post.
<?php echo 'http://twitter.com/home/?status='.urlencode('text to post'); ?>
To include a link to the Wordpress blog post that the link it for use the get_permalink() function, which must be called inside The Loop if called without any parameters. Don't use urlencode() on the permalink as this might lead to unpredictable results, just use the function on the text you want.
<?php echo 'http://twitter.com/home/?status='.urlencode('Currently reading ').get_permalink(); ?>
So the full link would be as follows, note that I have added a target="_blank" attribute to the link so that it opens in a new window. This will ensure that the user is able to post to Twitter without leaving your blog.
<a href="<?php echo 'http://twitter.com/home/?status='.urlencode('Currently reading ').get_permalink();?>" title="Update your status on Twitter!" target="_blank">Post To Twitter</a>
Remember that you can't put in more than 140 characters, so if you have a long URL then visit tinyurl.com first and convert it to something a little shorter. Basically, you can turn something like this:
Which gives you lots more characters to play with, but of course you could use the TinyUrl API in order to do this automatically. Here is three lines of code that will convert your long permalink into a TinyUrl.
- $url = get_permalink();
- $tiny = 'http://tinyurl.com/api-create.php?url=';
- $tinyurl = file_get_contents($tiny.$url);
You can then use the $tinyurl variable in place of the get_permalink() function call in the post to Twitter link.