WordPress.org

Ready to get started?Download WordPress

Forums

Twitter Shortcode intergration (3 posts)

  1. wp_alba
    Member
    Posted 3 years ago #

    Hi, Im trying to integrate my twitter function into wordpress via a shortcode, now everything works fine except it's not allowing me to set a number of tweets to display and is only allowing the 1 tweet to be displayed.

    Here's the twitter function im using to pull the tweets;
    credit: http://blog.spookyismy.name/2010/05/18/twitter-integration-class-using-php5-and-curl/

    <?php
    class Twitter
    {
      public $tweets = array();
      public function __construct($user, $limit = 5)
      {
        $user = str_replace(' OR ', '%20OR%20', $user);
        $feed = curl_init('http://search.twitter.com/search.atom?q=from:'. $user .'&rpp='. $limit);
        curl_setopt($feed, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($feed, CURLOPT_HEADER, 0);
        $xml = curl_exec($feed);
        curl_close($feed);
        $result = new SimpleXMLElement($xml);
        foreach($result->entry as $entry)
        {
            $tweet = new stdClass();
            $tweet->id = (string) $entry->id;
            $user = explode(' ', $entry->author->name);
            $tweet->user = (string) $user[0];
            $tweet->author = (string) substr($entry->author->name, strlen($user[0])+2, -1);
            $tweet->title = (string) $entry->title;
            $tweet->content = (string) $entry->content;
            $tweet->updated = (int) strtotime($entry->updated);
            $tweet->permalink = (string) $entry->link[0]->attributes()->href;
            $tweet->avatar = (string) $entry->link[1]->attributes()->href;
            array_push($this->tweets, $tweet);
        }
        unset($feed, $xml, $result, $tweet);
      }
      public function getTweets() { return $this->tweets; }
    }
    ?>

    Here's the shortcode im using

    <?php 
    
    function twitter_feed($atts, $content = null) {
    
    	extract(shortcode_atts(array(
    		"user"  => '',
    		"limit"  => ''
    	), $atts));
    
    	 $feed = new Twitter($user , $limit);
    $tweets = $feed->getTweets();
    
    foreach ($tweets as $tweet) {
    
    return '<ul><li>'. $tweet->content .' by <a href="http://twitter.com/'. $tweet->user .'">'. $tweet->author .'</a></li></ul>';
    
     }
    
    }
    add_shortcode("twitter", "twitter_feed");
    
    ?>

    Example Shortcode
    [twitter user="YourTwitterUserName" limit="5"]

    As I said above this code works fine apart from the tweet limit it, only ever pulls 1 tweet.

    Now I've tried putting the code directly into the theme to see if it was a problem with the function but it works fine, so I must be doing something wrong in the shortcode. Any idea's folks?

  2. likehate534
    Member
    Posted 3 years ago #

    I would try using the shortcode:
    [twitter user="YourTwitterUserName", limit="5"]

    If that doesn't work then no idea, it looks fine. I'm only suggesting that as a solution because you've said the shortcod ei would have used doesn't work.

  3. wp_alba
    Member
    Posted 3 years ago #

    Thanks for the reply, it still doesn't work though.

    hopefully someone will have a solution sooner or later.

Topic Closed

This topic has been closed to new replies.

About this Topic