I wanted to make it as easy as possible for my blog readers to directly retweet my posts. First I started to install and test several WordPress plugins for this but I just couldn’t seem to find one that I liked. Some of them are to massive and are showing information about how many and who has retweeted your post, I don’t need that. Other required the Twitter Anywhere connection and the user must accept that the retweet application on my blog can connect directly to their Twitter account, I don’t want to force my readers to do this. I decided to create the Retweet functionality by myself so I would have full control on the behavior. This post will tell you step by step how to achieve this.
First I wanted to shorten the post url by using Bit.ly, I am not a php expert or familiar with the Bit.ly API so I googled and found the php code needed for using Bit.ly to shorten my url. I found a great example at www.devmoose.com in a post called Automatically Create a Bit.ly URL for WordPress Posts. Just follow the steps in the referred post at devmoose.com to achieve this. I did a minor modification to the code to make it work in my WordPress : I added “<? php” before the code and “?>” after the code. I also added the “if function exists” check.
The complete code to add in your functions.php file:
<?php
//create bit.ly url
if ( !function_exists('bitly') ) {
function bitly()
{
//login information
$url = get_permalink(); //generates wordpress' permalink
$login = 'yourbitlyusername'; //your bit.ly login
$apikey = 'yourbitlyapikey'; //bit.ly apikey
$format = 'json'; //choose between json or xml
$version = '2.0.1';
<span id="more-1485"></span>
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.
'&longUrl='.urlencode($url).
'&login='.$login.'&apiKey='.$apikey.
'&format='.$format;
//get the url
//could also use cURL here
$response = file_get_contents($bitly);
//parse depending on desired format
if(strtolower($format) == 'json')
{
$json = @json_decode($response,true);
echo $json['results'][$url]['shortUrl'];
}
else //xml
{
$xml = simplexml_load_string($response);
echo 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
}
?>
Now it is just to add code to display a Retweet icon (or whatever you would like to use) and the functionality for posting to Twitter. I wanted to have a small Retweet icon at the top of my posts and also at the end of the post together with the other “share to social media” links.
Since I only want this in my single post view I modified the single.php file and added the following code right after the category is displayed and in the end of the file.
The code to display an icon and link post to twitter with shorten url (bit.ly):
<a href="http://twitter.com/home?status=<?php the_title(); ?>
<?php bitly(); ?> (by @PerOla)" target="blank">
<img src="url to your retweet icon"
alt="Retweet post" title="Retweet post"/></a>
When you click on the Retweet icon a new web page will open www.twitter.com and the post title, short url and (by @PerOla) is displayed and ready to be tweeted.
I had a problem that white spaces was displayed as %20 in twitter. I tried using the encodeurl function and the str_replace function with no luck. After a while I found that if I changed the twitter url from http://www.twitter.com… to http://twitter.com… the problem with the white spaces and %20 was resolved.
Follow me on twitter @PerOla
Share & enjoy
You can subscribe to my comments feed to keep track of new comments.
2 Comments to “Writing your own Twitter Retweet function for your (WordPress) blog”
1 Pingback to “Writing your own Twitter Retweet function for your (WordPress) blog”
-
[...] This post was mentioned on Twitter by Per Ola Sæther, Per Ola Sæther. Per Ola Sæther said: Writing you own Twitter Retweet function for you (WordPress) blog http://bit.ly/9ItTOT (by @PerOla) Testing my new Retweet functionality [...]














Thanks! This worked like a charm and I’m using it on my blog.
Cheers,
Tia
Hi Tia,
I am glad to hear that it worked like a charm and that you are using it