Recently, Twitter made some changes and dropped support for RSS (as well as imposing plenty of other measures and limitations along the way). You can read about it here – http://mashable.com/2012/09/05/twitter-api-rss/

A while back, I shared some code on How to get a Twitter feed into your WordPress theme. These new changes to Twitter’s API mean that this method no longer works. So, I’ve updated it.

I’ve also moved it into a Class. This needs updating already, as the JSON API actually gives us access to all sorts of new things, but this should be a good starting point.

// Twitter Class
// Usage:
// $twitter = new Twitter;
// $twitter->twitter_messages();
class Twitter{

  function __construct() {
    $this->Twitter();
  }
	
  function Twitter(){

    define('MAGPIE_CACHE_ON', 1); //2.7 Cache Bug
    define('MAGPIE_CACHE_AGE', 1800);
    define('MAGPIE_INPUT_ENCODING', 'UTF-8');
    define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');

  }

  public function twitter_messages($username = 'douglasradburn', $amount = 5, $error = 'Unable to retrieve tweets at this time.')
  {

    try {
      $feedDetails = $this->curlGet('https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=' .  $username . '&&count=' . $amount);
      $feed = json_decode($feedDetails);
      $i=0;
      foreach($feed as $item){
      printf('
  • %s %s
  • ',$this->twitterify(strip_tags(str_replace($username.': ', '', $item->text))), $item->id, contextualTime(strtotime($item->created_at))); $i++; } } catch (Exception $exception) { echo $error; } } private function twitterify($ret) { # thanks to http://www.snipe.net/2009/09/php-twitter-clickable-links/#axzz0psFrJPHB $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1\\2", $ret); $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1\\2", $ret); $ret = preg_replace("/@(\w+)/", "@\\1", $ret); $ret = preg_replace("/#(\w+)/", "#\\1", $ret); return $ret; } private function curlGet($url) { // is cURL installed yet? if (!function_exists('curl_init')){ echo 'Sorry cURL is not installed!'; return; } $ch = curl_init(); // the target curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return the page curl_setopt($ch, CURLOPT_URL, $url); $data = curl_exec($ch); curl_close($ch); return $data; } }