• Hi!

    I have been looking for a good solution to integrate latest tweets of a particular account into my sidebar, without a plugin.
    I have tried the solution given by Joost De Valk here:http://yoast.com/display-latest-tweet/

    Here is my code:

    In my functions.php:

    // get latest tweet and number of followers
    function get_tweet_info(){
    	require_once(ABSPATH . 'wp-includes/class-snoopy.php');
    $tweet   = get_option("lasttweet");
    $url  = "http://twitter.com/statuses/user_timeline/jeherve.json?count=20";
    if ($tweet['lastcheck'] < ( mktime() - 60 ) ) {
      $snoopy = new Snoopy;
      $result = $snoopy->fetch($url);
      if ($result) {
        $twitterdata   = json_decode($snoopy->results,true);
        $i = 0;
        while ($twitterdata[$i]['in_reply_to_user_id'] != '') {
          $i++;
        }
        $pattern  = '/\@([a-zA-Z]+)/';
        $replace  = '<a href="http://twitter.com/'.strtolower('\1').'">@\1</a>';
        $output   = preg_replace($pattern,$replace,$twitterdata[$i]["text"]);  
    
        $tweet['lastcheck'] = mktime();
        $tweet['data']    = $output;
        $tweet['rawdata']  = $twitterdata;
        $tweet['followers'] = $twitterdata[0]['user']['followers_count'];
        update_option('lasttweet',$tweet);
      } else {
        echo "Twitter API not responding.";
      }
    } else {
      $output = $tweet['data'];
    }
    echo "<p>\"".$output."\"</p>";
    };
    
    add_action('middlesidebar', 'get_tweet_info');

    and my hook in my sidebar of course:
    <?php do_action('middlesidebar'); ?>

    For some reason, it is still really slow.

    Would there be a mistake in my code, or would there be a better solution?

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Jeremy Herve

    (@hd-j)

    Alright, I gave up with the API, I am pulling the tweets from the feed now, it works fine:

    $username = "jeherve"; // Your twitter username.
    $limit = "3"; // Number of tweets to pull in.
    
    /* These prefixes and suffixes will display before and after the entire block of tweets. */
    $prefix = ""; // Prefix - some text you want displayed before all your tweets.
    $suffix = ""; // Suffix - some text you want displayed after all your tweets.
    $tweetprefix = ""; // Tweet Prefix - some text you want displayed before each tweet.
    $tweetsuffix = "<br />"; // Tweet Suffix - some text you want displayed after each tweet.
    
    $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=" . $limit;
    
    function parse_feed($feed, $prefix, $tweetprefix, $tweetsuffix, $suffix) {
    
    $feed = str_replace("<", "<", $feed);
    $feed = str_replace(">", ">", $feed);
    $clean = explode("<content type=\"html\">", $feed);
    
    $amount = count($clean) - 1;
    
    echo $prefix;
    
    for ($i = 1; $i <= $amount; $i++) {
    $cleaner = explode("</content>", $clean[$i]);
    echo $tweetprefix;
    echo $cleaner[0];
    echo $tweetsuffix;
    }
    
    echo $suffix;
    }
    
    $twitterFeed = file_get_contents($feed);
    parse_feed($twitterFeed, $prefix, $tweetprefix, $tweetsuffix, $suffix);

    Thanks to Instantshift: http://www.instantshift.com/2009/05/25/10-twitter-hacks-for-your-wordpress-blog/

    Hello,
    I don’t understand where I must copy the previous code to have the feed in my side bar ?
    I tried into a new html code but I don’t work.

    Thanks in advance,
    Yoann

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Integrate latest tweets without plugin’ is closed to new replies.