Where did my Twitter feed go?

As of March 2013, Twitter say that “we will require every request to the API to be authenticated“. In practical terms, this means your old widget on your blog will most likely break. What to do? Well, there will certainly be some new WordPress widgets coming soon. If you’re building something now and don’t want to wait, it’s slightly more difficult but only slightly.

I’m now using the plugin “oAuth Twitter Feed for Developers” and an edited version of the example code to layout tweets by Emma from Red Earth Design. The example code meets Twitters new design guidelines, too, bonus! I’ve tweaked both a little – this post will tell you how and why. I’m not going to go into how to use the plugin, it has easy to follow directions.

First, the changes I wanted to make:

  • Retweets were being truncated eg:
  • Replies were being included. Mine are usually stupid. I wanted them out.
  • I wanted to use it as a shortcode in which the number of tweets was passed as a parameter
  • After I had sorted all of the above, the tweets were showing up at the top of the page instead of where they were supposed to be. This needed to be fixed.

Instead of going through the changes one by one, I’ll tell you which files I edited and why.

File 1: oauth-twitter-feed-for-developers/StormTwitter.class.php in the oAuth Twitter Feed for Developers plugin files

Towards the end of this file is this line:

$result = $connection->get('statuses/user_timeline', array('screen_name' => $screenname, 'count' => 20, 'trim_user' => true));

I’ve edited it to this:

$result = $connection->get('statuses/user_timeline', array('screen_name' => $screenname, 'count' => 100, 'trim_user' => false, 'exclude_replies' => true));

Here’s what I added / changed:

‘exclude_replies’ => true;  this should be pretty obvious. I wanted to exclude replies.

‘trim_user’ => false;  I had to change this to get the screen name (eg mine is tharsheblows) for authors of retweeted statuses

‘count’ => 100; when I had twenty, I only ended up with fourteen non-reply tweets – I guess the count is applied first, then the replies are excluded? Not sure, can’t be bothered to trawl to find out a definitive answer.

Also, for testing purposes, I changed the cache time below (currently set at one hour) which is in this file. The main thing is to remember to change it back before putting it anywhere you could hit your rate limit:

if ($cache['time'] < (time() - 3600)) 

File 2: Example code to layout tweets

edit 31 December 2012: the code below throws out some empty <a></a>’s. I don’t know why and it still works (but looks ugly and doesn’t validate if you’re into that) so it’s going to the bottom of the list to be fixed. I will update again if I do.

Using the code linked to above, the retweets were causing some problems – once I had the whole tweet, the display went a little wonky. Their information is in the retweeted_status object and needed to be found there. For some reason, I couldn’t find the original tweeter’s screen name, so had to access it like a regular tweet. Anyway.

I also added some extra divs to make it eaiser to style. You’ll end up with something like this:

<div class=”twitter_box”>
<div class=”tweet”>THE TWEET</div>
<div class=”twitter_intents”><p>LINKS TO REPLY, RETWEET AND FAVORITE</p></div>
<p class=”timestamp”>TIMESTAMP LINKED TO STATUS</p>
</div>

Looking at that, I’m not sure I like that layout. I might change it. It really should be a list, shouldn’t it.

The tweets showing up at the top of the page thing was due to the function echo-ing the output as it went along instead of returning it. I changed this.

Here is the final code (it goes in functions.php as usual) – oh, and if you use this, you will want to change “tharsheblows” to whatever your Twitter screen name is. I can’t be bothered to make this non-hard coded:


// dealing with Twitter - adapted from https://github.com/stormuk/storm-twitter-for-wordpress/wiki/Example-code-to-layout-tweets
// http://wordpress.org/extend/plugins/oauth-twitter-feed-for-developers/

function twitter_display($tweet, $status, $the_tweet){
/*
Twitter Developer Display Requirements
https://dev.twitter.com/terms/display-requirements

2.b. Tweet Entities within the Tweet text must be properly linked to their appropriate home on Twitter. For example:
i. User_mentions must link to the mentioned user's profile.
ii. Hashtags must link to a twitter.com search with the hashtag as the query.
iii. Links in Tweet text must be displayed using the display_url
field in the URL entities API response, and link to the original t.co url field.
*/

// i. User_mentions must link to the mentioned user's profile.
foreach($status['entities']['user_mentions'] as $key =&gt; $user_mention){
$the_tweet = preg_replace(
'/@'.$user_mention['screen_name'].'/i',
'&lt;a href="http://www.twitter.com/'.$user_mention['screen_name'].'" target="_blank"&gt;@'.$user_mention['screen_name'].'&lt;/a&gt;',
$the_tweet);
}

// ii. Hashtags must link to a twitter.com search with the hashtag as the query.
foreach($status['entities']['hashtags'] as $key =&gt; $hashtag){
$the_tweet = preg_replace(
'/#'.$hashtag['text'].'/i',
'&lt;a href="https://twitter.com/search?q=%23'.$hashtag['text'].'&amp;src=hash" target="_blank"&gt;#'.$hashtag['text'].'&lt;/a&gt;',
$the_tweet);
}

// iii. Links in Tweet text must be displayed using the display_url
//      field in the URL entities API response, and link to the original t.co url field.
foreach($status['entities']['urls'] as $key =&gt; $link){
$the_tweet = preg_replace(
'<code>'.$link['url'].'</code>',
'&lt;a href="'.$link['url'].'" target="_blank"&gt;'.$link['url'].'&lt;/a&gt;',
$the_tweet);
}
return $the_tweet;
}//end twitter_display

//twitter shortcode function
function twitter_shortcode($atts){

extract( shortcode_atts( array(
'number' =&gt; '5',
), $atts ) );

$tweets = getTweets($number);
if(is_array($tweets)){

// to use with intents
echo '&lt;script type="text/javascript" src="//platform.twitter.com/widgets.js"&gt;&lt;/script&gt;';
$tweet_box = '&lt;div&gt;';
foreach($tweets as $tweet){

if($tweet['text']){

if (isset($tweet['retweeted_status'])){
$the_tweet = "RT  @".$tweet['retweeted_status']['user']['screen_name'].": ".$tweet['retweeted_status']['text'];
//I had to add in this next foreach to get the tweeter's link correct - not sure why
foreach($tweet['entities']['user_mentions'] as $key =&gt; $user_mention){
$the_tweet = preg_replace(
'/@'.$user_mention['screen_name'].'/i',
'&lt;a href="http://www.twitter.com/'.$user_mention['screen_name'].'" target="_blank"&gt;@'.$user_mention['screen_name'].'&lt;/a&gt;',
$the_tweet);
}
$the_tweet = twitter_display($tweet, $tweet['retweeted_status'], $the_tweet);

}
else{
$the_tweet = $tweet['text'];
$the_tweet = twitter_display($tweet, $tweet, $the_tweet);

}

$tweet_box .= '&lt;div&gt;'.$the_tweet.'&lt;/div&gt;';

// 3. Tweet Actions
//    Reply, Retweet, and Favorite action icons must always be visible for the user to interact with the Tweet. These actions must be implemented using Web Intents or with the authenticated Twitter API.
//    No other social or 3rd party actions similar to Follow, Reply, Retweet and Favorite may be attached to a Tweet.
// get the sprite or images from twitter's developers resource and update your stylesheet
$tweet_box .= '
&lt;div&gt;
&lt;p&gt;&lt;a href="https://twitter.com/intent/tweet?in_reply_to='.$tweet['id_str'].'"&gt;Reply&lt;/a&gt; |
&lt;a href="https://twitter.com/intent/retweet?tweet_id='.$tweet['id_str'].'"&gt;Retweet&lt;/a&gt; |
&lt;a href="https://twitter.com/intent/favorite?tweet_id='.$tweet['id_str'].'"&gt;Favorite&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;';

// 4. Tweet Timestamp
//    The Tweet timestamp must always be visible and include the time and date. e.g., “3:00 PM - 31 May 12”.
// 5. Tweet Permalink
//    The Tweet timestamp must always be linked to the Tweet permalink.
$tweet_box .= '
&lt;p&gt;
&lt;a href="https://twitter.com/tharsheblows/status/'.$tweet['id_str'].'" target="_blank"&gt;posted at
'.date('g:i A \o\n d M Y',strtotime($tweet['created_at'])).'
&lt;/a&gt;
&lt;/p&gt;';
} else {
$tweet_box .= '
&lt;br /&gt;&lt;br /&gt;
&lt;a href="http://twitter.com/tharsheblows" target="_blank"&gt;Click here to for more tweets from tharsheblows&lt;/a&gt;';
}
}//foreach $tweets as $tweet
}//if is_array($tweets)

$tweet_box .= '&lt;p&gt;I\'m &lt;a href="http://twitter.com/tharsheblows" target="_blank"&gt;tharsheblows&lt;/a&gt; on Twitter.&lt;/p&gt;';

$tweet_box .= "&lt;/div&gt;&lt;!-- tweet_box --&gt;";

return $tweet_box;

}//end twitter_shortcode function

add_shortcode('tweets', 'twitter_shortcode');
//end twitter stuff

&nbsp;

I’m using a minimum of css right now:


/*oauth twitter stuff */
.twitter_box{padding: 10px 0 0 0; border-bottom: 1px #E2E2E2 solid;}
.twitter_intents p{text-align: right; font-size: 80%; margin-bottom: 0; padding-bottom: 0;}
p.timestamp {font-size:80%; text-align: right; padding: 0; margin-bottom: 3px; }

And that’s it!

 

 

2 comments

  1. I’m testing a social logins plugin. This one: http://wordpress.org/extend/plugins/oa-social-login/ I’ve just logged in through Twitter, thrilling!

  2. Another test. This would be test 2, in fact. Again with the Twitter.

Leave a Reply

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