Add A Post To Twitter Button To Your Wordpress Posts

4th June 2009 - 3 minutes read time

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.

http://twitter.com/home/?status=text+to+post

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:

https://www.hashbangcode.com/article/add-post-twitter-button-your-wordpress-posts

Into this.

http://tinyurl.com/62l65b

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.

Comments

Permalink
i always update my Twitter and i love to twitter my daily activities to my friends and loved ones. i also maintain a personal blog for entries which requires more detail.

Jake Gomez (Sun, 02/21/2010 - 06:15)

Permalink
Hi. Thanks for the tip. I had to use: get_permalink <a href="" title="Update your status on Twitter!" target="_blank">Post To Twitter</a>

Mat (Wed, 03/17/2010 - 15:15)

Permalink
Yes, get_permalink() will get the permalink (hence the name I suppose!) whereas the_permalink() will print out the permalink. The correct function should actually be get_permalink(), so I have changed that. Thanks for the feedback Mat! :)
Permalink
I find the this post very informative and relevant especially to those who want to consolidate their social media accounts into one page in the internet without further opening another browser. Sometimes it could be a hassle to anyone to open a new browser, type the url and deploy the logging details that sometimes consumes few minutes. So with the help of the simple instructions above I could enjoy posting and twitting on just one browser. What a great simple advancement it is.

Guest (Fri, 02/11/2011 - 16:29)

Permalink
What a great idea!! I may enjoy all the social media networking sites just in one browser. I agree with the post from the Guest above that it could eliminate idle time just doing the deploying of login details in the site. Does this make an automatic login? Or not? Because from my experience before wordpress automatically gets the details from the social sites? Am I understanding it right? But this is a big plus to the bloggers like me.

Fernie (Sat, 02/12/2011 - 09:29)

Add new comment

The content of this field is kept private and will not be shown publicly.