preload
Jul 07

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:

&lt;?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.
              '&amp;longUrl='.urlencode($url).
              '&amp;login='.$login.'&amp;apiKey='.$apikey.
              '&amp;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-&gt;results-&gt;nodeKeyVal-&gt;hash;
	}
    }
 }
?&gt;

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.

15 Comments to “Writing your own Twitter Retweet function for your (WordPress) blog”

  1. Tia says:

    Thanks! This worked like a charm and I’m using it on my blog.

    Cheers,
    Tia

  2. Hi Tia,
    I am glad to hear that it worked like a charm and that you are using it :)

  3. Thank you for any other informative blog. Where else may just I am getting that type of information written in such an ideal method?

    I have a project that I’m simply now running on, and I’ve
    been at the glance out for such information.

  4. Justine says:

    Excellent beat ! I would like to apprentice
    while you amend your site, how can i subscribe for a
    blog site? The account aided me a acceptable deal.

    I had been tiny bit acquainted of this your broadcast
    offered bright clear idea

  5. kemengfa.com says:

    Hi to every , as I am genuinely keen of reading
    this weblog’s post to be updated daily. It
    contains good data.

  6. zz-show.com says:

    Aw, this was a really nice post. Taking a few minutes and actual effort to create a great article… but what can I
    say… I procrastinate a lot and don’t seem to get nearly anything done.

  7. hi!,I really like your writing so a lot! proportion we keep up a correspondence
    extra about your article on AOL? I need a specialist
    on this house to unravel my problem. Maybe that’s you! Looking
    forward to look you.

  8. Wonderful goods from you, man. I’ve understand your stuff
    previous to and you’re just too excellent. I really like what you have acquired here, really like
    what you’re saying and the way in which you say it.

    You make it enjoyable and you still take care of to keep it smart.
    I can’t wait to read far more from you. This is actually a wonderful
    site.

  9. My family always say that I am killing my time here at net, but I know I am
    getting know-how daily by reading such good content.

  10. Aurora says:

    Thanks for sharing your thoughts on code example – php. Regards

  11. frullatore says:

    What’s up to every one, because I am in fact eager of reading this blog’s
    post to be updated daily. It includes nice material.

  12. magma-pc.com says:

    It’s actually a great and helpful piece of information. I’m happy that you just shared this helpful information with us.
    Please keep us up to date like this. Thanks
    for sharing.

  13. I do not know whether it’s just me or if perhaps everyone else encountering problems with your website.
    It seems like some of the written text on your content are running off the screen.
    Can somebody else please comment and let me know if this is
    happening to them as well? This may be a issue with my internet browser because I’ve had this happen before.
    Many thanks

  14. Anton says:

    Right here is the perfect blog for everyone who really wants to understand this topic.
    You understand a whole lot its almost tough to argue with you
    (not that I really will need to…HaHa). You definitely put a fresh spin on a topic that’s been discussed for ages.

    Wonderful stuff, just great!

  15. Just wish to say your article is as surprising. The clearness in your post is
    simply excellent and i could assume you are an expert on this subject.

    Well with your permission let me to grab your RSS feed to
    keep up to date with forthcoming post. Thanks
    a million and please continue the gratifying work.

1 Pingback to “Writing your own Twitter Retweet function for your (WordPress) blog”

  1. […] 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 […]

Leave a Reply

Subscribe to my comments feed

Subscribe to my feeds Follow me on Twitter
DZone MVB