Adding Retweets to Twitter Widget Pro

I mentioned yesterday that I have added my twitter feed to the website using the WordPress widget plugin ‘Twitter Widget Pro‘. Although it seemed to be working ok, I noted that the number of tweets being displayed was different from what I’d specified in the widget’s settings. Subsequently I discovered that the reason was connected with another problem that I didn’t initially realize I had – the widget was not displaying my ‘retweets’, i.e. twitter comments from other people I follow that I add to my own feed. However, it was apparently including the missing retweets in its total count of the tweets it was retrieving, causing the number of tweets displayed to be wrong.

Checking the support forum over at xavisys.com revealed that a number of others have mentioned this problem, going back at least as far as January this year. Given this apparent limitation, I could have gone and looked for a different plugin, but I decided instead to see if I could figure out why retweets weren’t being displayed.

The problem turned out to be with the way the plugin invokes the Twitter API. This happens in the php function _getFeedUrl() in file wp-twitter-widget.php, which is located in the plugin directory <root>/wp-content/plugins/twitter-widget-pro. The original code looks like this –

629
630
631
632
633
634
635
636
637
638
639
640
private function _getFeedUrl($widgetOptions, $type = 'json', $count = true) {
	if (!in_array($type, array('rss', 'json'))) {
		$type = 'json';
	}
	if ( $count ) {
		$num = ($widgetOptions['hidereplies'])? 100:$widgetOptions['items'];
		$count = sprintf('?count=%u', $num);
	} else {
		$count = '';
	}
	return sprintf('http://twitter.com/statuses/user_timeline/%1$s.%2$s%3$s', $widgetOptions['username'], $type, $count);
}

The code that generates the URI used to retrieve the twitter data in JSON format can be seen at line 639. In my case this would produce something like:

http://twitter.com/statuses/user_timeline/markmthomson.json?count=8

However, when I looked at the Twitter API documentation, I wasn’t able to find an example that matches the pattern shown in this example. Instead, the URI that does appear to provide the required data looks like this –

http://api.twitter.com/1/statuses/user_timeline.json?screen_name=markmthomson&include_rts=true&count=8

Critically, the query parameter ‘include_rts=true’ is explicitly required in order for the list of tweets returned to include retweets. Evidently the plugin was using an older version of the API that doesn’t support this capability. In order to generate the URI for the published API, I modified the code as follows –

629
630
631
632
633
634
635
636
637
638
639
640
private function _getFeedUrl($widgetOptions, $type = 'json', $count = true) {
	if (!in_array($type, array('rss', 'json'))) {
		$type = 'json';
	}
	if ( $count ) {
		$num = ($widgetOptions['hidereplies'])? 100:$widgetOptions['items'];
		$count = sprintf('&count=%u', $num);
	} else {
		$count = '';
	}
	return sprintf('http://api.twitter.com/1/statuses/user_timeline.%1$s?screen_name=%2$s&include_rts=true%3$s', $type, $widgetOptions['username'], $count);
}

Note that there are changes to both line 635 and line 639. With this modification, the plugin now displays retweets and the count is correct. However, there is one problem remaining…

The text of some retweets retrieved in this way is truncated and terminated with an ellipsis (I’m guessing because the leading attribution text “RT <screen_name>” that gets added probably puts the tweet over the 140 character limit). Further examination of the JSON data returned by Twitter revealed that the full text of each retweet is actually present (without the attribution prefix), but it is stored in a different part of the object, specifically within an embedded object referenced by a key named “retweeted_status”. This object also includes data about the author of the retweet. What this means is that if the JSON object corresponding to a single retweet is represented by a php variable “$tweet”, the full text of the tweet can be accessed as “$tweet->retweeted_status->text”, while the author’s screen name is given by “$tweet->retweeted_status->user->screen_name”.

The html used to render the tweets is generated in function display() in wp-twitter-widget.php. From that function, the following is a fragment of the loop over tweets that generates the content of each tweet –

502
503
504
505
506
foreach ($tweets as $tweet) {
	if ( $args['hidereplies'] != 'true' || empty($tweet->in_reply_to_user_id)) {
		// Set our "ago" string which converts the date to "# ___(s) ago"
		$tweet->ago = $this->_timeSince(strtotime($tweet->created_at), $args['showts'], $args['dateFormat']);
		$entryContent = apply_filters( 'widget_twitter_content', $tweet->text );

The modification required to retrieve the full text of retweeted tweets is as follows –

502
503
504
505
506
507
508
509
510
511
foreach ($tweets as $tweet) {
	if ( $args['hidereplies'] != 'true' || empty($tweet->in_reply_to_user_id)) {
		// Set our "ago" string which converts the date to "# ___(s) ago"
		$tweet->ago = $this->_timeSince(strtotime($tweet->created_at), $args['showts'], $args['dateFormat']);
		if ( isset($tweet->retweeted_status) ) {
			$tweet_text = "RT @".$tweet->retweeted_status->user->screen_name.":".$tweet->retweeted_status->text;
		} else {
			$tweet_text = $tweet->text;
		}
		$entryContent = apply_filters( 'widget_twitter_content', $tweet_text );

Note the lines 506-510 have been added and line 511 has been modified by replacing “$tweet->text” with “$tweet_text”. With this modification, retweeted tweets are now rendered correctly. Problem solved!

This entry was posted in Software Development, Website. Bookmark the permalink.

13 Responses to Adding Retweets to Twitter Widget Pro

  1. Adam says:

    Hi Mark,

    Love the tweak idea you had. I like this Widget but the lack up updates has been an issue for me.

    I tried the above code but it didn’t seem to do anything. I still don’t get the retweets. Can you post a fully modified php file for download so I can see if I missed something?

    Thanks.

    -Adam

  2. Mark says:

    Adam,

    You can get my entire wp-twitter-widget.php file here: https://gist.github.com/767818 . You should be able to drop it straight into your wp-content/plugins/twitter-widget-pro folder. I have a retweet sitting in my sidebar at the moment, so you can see that it does indeed work.

    The gist file differs from what I wrote above only in that some of the original lines I replaced are shown commented out, rather than actually deleted. Also note that the line numbers shown above for the first modification at 629-640 apply before making the second modification at 502-511, so they’re slightly different in the file I’ve posted.

    Mark.

  3. Adam says:

    Mark,

    Works great!

    Love the change.

    Have you approached the developer with the fix and perhaps he could just include it in an update? With another option in the settings to add/remove retweets, this widget is one of the best ones out there I think…

    Thanks again for the update.

    -Adam

  4. Mark says:

    Hi, glad you got it to work ok. I hadn’t thought of contacting the developer directly, except that I posted a couple of links to this page on his support forum, so I figured he would see it. But maybe I should send him a note directly. Thanks.

  5. Amber says:

    Just wanted to let you know that it works for me, but there is a typo in your code above. In your modified _getFeedUrl function on line 639, you have “&include_rts” instead of “&include_rts”. Thanks for posting your fix!

  6. Amber says:

    Ack, the HTML makes it not show up correctly.
    Correction: You have & a m p ; instead of just the ampersand sign in the twitter API link.

  7. Mark says:

    Thanks Amber. I’ve fixed it. I must have opened the wordpress editor at some point in visual mode instead of html – it seems to screw things up.

  8. Mark says:

    Update: somehow in the process of editing, the line numbers got deleted – I suspect again to do with opening it in wordpress visual mode. Hopefully everything is fixed now.

  9. Jack Kern says:

    Thanks for posting this! This works great. I just commented out the old function and replaced it with yours. I’m surprised they haven’t taken your patch yet!?

  10. Will says:

    That is a great fix you have found! I was quite baffled by this at first, only I didn’t dig into it. I more or less shrugged it off and hopes that there would be an update soon.

    I do hope this gets passed along to Xavisys, it would be a shame to see their plugin fade into obscurity.

  11. Mark says:

    Glad I could help Will. I did send a note to Aaron at xavisys, but didn’t get a response. However I am getting a fair bit of traffic to this page, so hopefully others are making use of the fix.

  12. Kane says:

    2012 and this post is still helping out. Thanks, and thanks for the link to the updated wp-twitter-widget.php file. Retweets are now showing up. :)

  13. Mark says:

    Good to hear! Thanks for the +ve feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *