Fixing Twitter for WordPress : “No public Twitter messages.” Error

Published by John on October 19, 2012 Under Wordpress
Update 07/12/2013: The below will no longer work, due to changes in Twitter’s API. Please see here for more information.

I have used the now apparently un-maintained Twitter for WordPress plugin on a couple of websites that I did several years ago.

Recently, the clients notified me that new tweets were no longer being displayed and instead they were just getting a “No public Twitter messages.” error message.

After a bit of research, I found that the call to get the User’s rss feed was using an old-url.

On about line 54 of twitter-for-wordpress/twitter.php, within function twitter_messages the call was:

$messages = fetch_rss('http://twitter.com/statuses/user_timeline/'.$username.'.rss');

Twitter recently changed their RSS feed url to a different format, so this would no longer work.

If you goto the page, you would get an “Sorry, that page does not exist” error message.

Quick Fix

The quick fix is to update line 54 of twitter-for-wordpress/twitter.php as follows:

	$messages = fetch_rss('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$username);

Note the new format url that calls to api.twitter.com. You can also use different formats, like json or xml, but for this case, you would need to stick to rss.

However, the WordPress function fetch_rss has been depreciated, so eventually it will be removed from WordPress and the plugin will break again, so this is not the best way to fix it.

A Slightly Better Fix

Since the fetch_rss function is depreciated, it would be better to use fetch_feed. Otherwise, you will probably end up fixing this again at some point, likely the next time there is a major WordPress release.

It can be replaced with fetch_feed, with only a few changes. You can replace the entire twitter_messages function in twitter-for-wordpress/twitter.php with the below:


function twitter_messages($username = '', $num = 1, $list = false, $update = true, $linked  = '#', $hyperlinks = true, $twitter_users = true, $encode_utf8 = false) {
 
    global $twitter_options;
    include_once(ABSPATH . WPINC . '/feed.php');
     
    //$messages = fetch_rss('http://twitter.com/statuses/user_timeline/'.$username.'.rss');
    $messages = fetch_feed('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$username);
 
    if ($list) echo '<ul class="twitter">';
     
    if ($username == '') {
        if ($list) echo '<li>';
        echo 'RSS not configured';
        if ($list) echo '</li>';
    } else {
                 
            if (is_wp_error( $messages ) ) {
                if ($list) echo '<li>';
                echo 'No public Twitter messages.';
                if ($list) echo '</li>';
            } else {
             
                $maxitems = $messages->get_item_quantity($num); 
                $rss_items = $messages->get_items(0, $maxitems); 

		if($maxitems <= 0){
                if ($list) echo '<li>';
                echo 'No public Twitter messages.';
                if ($list) echo '</li>';


		} else {
                 
                    foreach ( $rss_items as $item ) {
                         
                        $msg  = $item->get_description();
                        $link = $item->get_permalink();
                         
                        if($encode_utf8) 
                            $msg = utf8_encode($msg);
                 
                        if ($list)
                            echo '<li class="twitter-item">'; 
                        elseif ($num != 1) 
                            echo '<p class="twitter-message">';
 
                        if ($hyperlinks) 
                            { $msg = hyperlinks($msg); }
                             
                        if ($twitter_users)  
                            { $msg = twitter_users($msg); }
                             
                    if (!empty($linked)) {
                        if($linked == 'all')  { 
                            $msg = '<a href="'.$link.'" class="twitter-link">'.$msg.'</a>';  // Puts a link to the status of each tweet 
                        } else {
                            $msg = $msg . '<a href="'.$link.'" class="twitter-link">'.$linked.'</a>'; // Puts a link to the status of each tweet
               
                        }
                    } 
 
                    echo $msg;
           
           
                if($update) {               
                    $time = strtotime($item->get_date('Y-m-d G:i:s'));
           
                     
                    if ( ( abs( time() - $time) ) < 86400 )
                        $h_time = sprintf( __('%s ago'), human_time_diff( $time ) );
                    else
                        $h_time = date(__('Y/m/d'), $time);
 
                    echo sprintf( __('%s', 'twitter-for-wordpress'),' <span class="twitter-timestamp"><abbr title="' . date(__('Y/m/d H:i:s'), $time) . '">' . $h_time . '</abbr></span>' );
                }          
                   
                if ($list) echo '</li>'; elseif ($num != 1) echo '</p>';
                 
            }
            }
        }
         
    }
     
    if ($list) echo '</ul>';
     
}

I believe this should work the same, including the date and such, but it uses the new object oriented fetch_feed styling.

Todo / Fix : I think it would be wise to tweak the date format a bit, as at least in the United States Year/Month/Day is a little non-standard. That entire section could do with a rewrite, but using the more standard method of pulling the WordPress date format setting would be better. Also, I don’t think the strtotime is needed, as you can probably get that from the RSS object.

Optional: Change Feed Cache Time Limit

By default, wordpress will cache the feed for 12 hours, so if you update your twitter feed a lot, you might want to tweak it.

In your theme’s functions.php add the following code:

function kcr_set_feed_cache(){

$cache_time_in_hours = 1;
return $cache_time_in_hours * 60 * 60;

}

Make sure to update $cache_time_in_hours to whatever you want and then in twitter-for-wordpress/twitter.php, update the “fetch_feed” line as follows:

    add_filter( 'wp_feed_cache_transient_lifetime', kcr_set_feed_cache);
    $messages = fetch_feed('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$username);
    remove_filter( 'wp_feed_cache_transient_lifetime', kcr_set_feed_cache);

This sets the feed cache for this call only and then resets it afterwards.

If you don’t do this, the feed may not update as frequently as you want.

Optional: Disable Updates for this plugin

You may also want to update the plugin description and title, as well as change the version number to something really high, so nobody accidentally updates it without checking it out first.

An Even Better Fix

Given that the plugin hasn’t been updated in over 2 years, it would probably be better to do a complete code review and potentially rewrite.

I will likely be removing this plugin shortly, as it has not been updated in a few years, but for now the clients Tweets are showing up again and I believe all the original plugin functionality was maintained.

Also, have been meaning to see how Twitters recent changes to their api affect any of my sites, if it does at all…

Update 07/12/2013: Due to changes in the Twitter API, the above will no longer work. To print a user timeline, go here.


10 Comments |

Comments:

  1. Ed on Oct 22, 2012

    How would you hide the @replies as these seem to display even when the option is set to hidden.

  2. john on Oct 22, 2012

    Do you mean how the @replies username is hyperlinked in tweets, listed as “Discover @replies” in the widget?

    If so, setting that to false within the actual call to twitter_messages correctly removed the @twitter_username, I just checked it with a few different names and displaying multiple tweets.

    However, I don’t use the widget, so if you are using the twitter widget and still see those links even after toggling the ‘Discover @replies’ option, the way the widget_twitter function is setting options might need to be looked at(probably does, saw one unrelated error when I checked it now.)

    Probably a quick fix would be to hard code ‘twitter_users’ to false in the actual twitter_messages function.

    IE, within the widget_twitter function change the call to:

    twitter_messages($item[‘username’], $item[‘num’], true, $item[‘update’], $item[‘linked’], $item[‘hyperlinks’], false, $item[‘encode_utf8’]);

    That should ensure that the @twitter_username is not hyperlinked within the tweet and you can then go in and check the settings. Also, if you change the number of tweets or the other settings in the widget and they still don’t work, that would be another indication the widget needs to be fixed.

  3. Jaro on Oct 23, 2012

    Thanks for sharing this fix! Easy to implement and it works.

  4. Ketharaman on Nov 09, 2012

    “Quick Fix” didn’t work for me, but “A Slightly Better Fix” did! Thanks a lot!

  5. john on Nov 10, 2012

    Sorry the quick fix didn’t work for you Ketharaman, it did at the time of testing.

    Really though, since the ‘fetch_rss’ function is depreciated, it is probably better to use the other fix anyway. Or, possibly installing a different plugin.

    Also, if you do a lot of updating, which one of my clients does, you will almost certainly need to adjust the caching as mentioned in the “Change Feed Cache Time Limit.” Otherwise, the twitter feed won’t update as quickly.

  6. Sarika on Nov 23, 2012

    Thanks so much..Works…

  7. Cat on Dec 03, 2012

    Perfect! Thanks much!

  8. RLT on Dec 05, 2012

    Thanks John. The quickfix did it for me and all is as it should be.

    thanks again

  9. Andy on Dec 13, 2012

    thanks. very quick clean fix.

  10. Carl Frederic on Jan 10, 2013

    Thanks for your quick fix! Now it works fine again.

    Best regards from Germany

    Carl Frederic

Add a Comment