As some of you might know, Twitter has changed their API.

With that some of my plugins / codes stopped working. I tried to fix them on my own, but failed. I tried googling for the new API URL and I also searched here. I tried to insert the URL provided in this post, but once applied I only got an error message.

I need to update the following two codes with the new API, but don't know how to!

Twitter followers' count snippet:

function rarst_twitter_user( $username, $field, $display = false ) {
    $interval = 3600;
    $cache = get_option('rarst_twitter_user');
    $url = 'http://api.twitter.com/1/users/show.json?screen_name='.urlencode($username);

    if ( false == $cache )
        $cache = array();

    // if first time request add placeholder and force update
    if ( !isset( $cache[$username][$field] ) ) {
        $cache[$username][$field] = NULL;
        $cache[$username]['lastcheck'] = 0;

    // if outdated
    if( $cache[$username]['lastcheck'] < (time()-$interval) ) {

        // holds decoded JSON data in memory
        static $memorycache;

        if ( isset($memorycache[$username]) ) {
            $data = $memorycache[$username];
        else {
            $result = wp_remote_retrieve_body(wp_remote_request($url));
            $data = json_decode( $result );
            if ( is_object($data) )
                $memorycache[$username] = $data;

        if ( is_object($data) ) {
            // update all fields, known to be requested
            foreach ($cache[$username] as $key => $value)
                if( isset($data->$key) )
                    $cache[$username][$key] = $data->$key;

            $cache[$username]['lastcheck'] = time();
        else {
            $cache[$username]['lastcheck'] = time()+60;

        update_option( 'rarst_twitter_user', $cache );

    if ( false != $display )
        echo $cache[$username][$field];
    return $cache[$username][$field];

Show latest tweets snippet:

function recentTweets($username, $number){ 

 $tweet = fetch_feed("http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=" . $number ); 

 if (!is_wp_error( $tweet ) ) : 
 $maxitems = $tweet->get_item_quantity($number);  
 $rss_items = $tweet->get_items(0, $maxitems);  

 if ($maxitems == 0) echo '<li>No Tweets.</li>'; 
 else foreach ( $rss_items as $item ) { 

 $content = html_entity_decode($item->get_content()); 
 $link = html_entity_decode($item->get_permalink()); 
 $date = $item->get_date('U'); // retrives the tweets date and time in Unix Epoch terms
 $blogtime = current_time('U'); // retrives the current browser client date and time in Unix Epoch terms
 $dago = human_time_diff($date, $blogtime) . ' ago'; // calculates and outputs the time past in human readable format

 echo "<li>$content &#8226;<a href='$link'>$dago</a></li>"; 

Thank you so much in advance. Here's my blog for reference.


New Twitter API requires authentication, you need to go to dev twitter and to create tokens. See documentation

You need to use a library. I use this one

EDIT: You need to create an application on dev twitter (it's just a basic information, fulfill the form) and then you'll get access to your tokens. The library will manage connection to the API if you have those tokens.

I wrote a lot of articles about that but it's in French so I doubt you will read them and I'm not here to advertize. Probably a better idea to read articles from this website

| improve this answer | |
  • Thank you for your answer. This sounds extremely complicated. First of all, I can't find anything about "tokens" at the website you linked to. Only about "Tweet cards" and other things, but no "tokens". The documentation also didn't answer this question. Could you elaborate a bit more for somebody who doesn't know much about how Twitter works in the background. Thank you! – japanworm Jun 13 '13 at 15:02
  • I've edited my answer – JMau Jun 13 '13 at 15:15
  • Thank you. I created an application and got my tokens. I also found a tutorial, but it only gives the code for creating a widget. I don't want that. I only want to know how I need to modify my old code to work with the new API. – japanworm Jun 16 '13 at 1:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.