Skip to content

Instantly share code, notes, and snippets.

@abfan1127
Created September 18, 2013 08:12
Show Gist options
  • Save abfan1127/6606119 to your computer and use it in GitHub Desktop.
Save abfan1127/6606119 to your computer and use it in GitHub Desktop.
An update to the "Rotating Tweets" Wordpress plugin. Instead of taking a single twitter screen name, now I space-delimit as many as I wish. Then I loop around them. Finally I sort them by date via the usort function and its related callback.
$screen_names = explode(' ',$tw_screen_name);
$twitterjsons = array();
foreach($screen_names as $screen_name) {
$apioptions = array('screen_name'=>$screen_name,'include_entities'=>1,'count'=>10,'include_rts'=>$tw_include_rts,'exclude_replies'=>$tw_exclude_replies);
if($tw_search) {
$apioptions['q']=$tw_search;
$twitterdata = rotatingtweets_call_twitter_API('search/tweets',$apioptions);
} elseif($tw_get_favorites) {
$twitterdata = rotatingtweets_call_twitter_API('favorites/list',$apioptions);
} elseif($tw_list) {
unset($apioptions['screen_name']);
$apioptions['slug']=$tw_list;
$apioptions['owner_screen_name']=$tw_screen_name;
$twitterdata = rotatingtweets_call_twitter_API('lists/statuses',$apioptions);
} else {
$twitterdata = rotatingtweets_call_twitter_API('statuses/user_timeline',$apioptions);
}
if(!is_wp_error($twitterdata)):
$twitterjson = json_decode($twitterdata['body'],TRUE);
if(WP_DEBUG):
echo "<!-- Rotating Tweets - got new data -->";
endif;
else:
set_transient('rotatingtweets_wp_error',$twitterdata->get_error_messages(), 120);
endif;
$twitterjsons = array_merge($twitterjsons,$twitterjson);
}
usort($twitterjsons,'tweet_sort');
$twitterjson = $twitterjsons;
function tweet_sort($a,$b)
{
$a_date = strtotime($a['created_at']);
$b_date = strtotime($b['created_at']);
return ($a_date < $b_date);
}
@mpntod
Copy link

mpntod commented Sep 18, 2013

Many thanks for this. Love the interface improvement so I've copied it. I've gone with an approach that uses the Twitter Search API to handle multiple accounts.

if(empty($tw_search)):
    $possibledividers = array(' ',';',',');
    $rt_namesarray = false;
    foreach($possibledividers as $possibledivider):
        if(strpos($tw_screen_name,$possibledivider) !== false ):
            $rt_namesarray = explode(' ',$tw_screen_name);
            $tw_search = 'from:'.implode(' OR from:',$rt_namesarray);
        endif;
    endforeach; 
else:
    $tw_search = trim($tw_search);
endif;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment