Question

i am having a wordpress function to display the latest 5 tweets of the user, this tweets can be displayed by two ways one is through direct entering the user ID in admin menu which i created, and second is through custom fields,

here i want to avoid custom fields and want to start with shortcode, i know i am weak in short-code cause this is my second question related with it but need short-code in this function

here is the code

function tweet_fetcher(){
global $post;
$doc = new DOMDocument();
if((get_option('tweetID'))!=null){
$meta=get_option('tweetID');
$feed = "http://twitter.com/statuses/user_timeline/$meta.rss"; 
$doc->load($feed); 

}
elseif((get_option('tweetID'))==null){
if ( $meta =get_post_meta($post->ID, 'username:', true) ) { 
$feed = "http://twitter.com/statuses/user_timeline/$meta.rss"; 
$doc->load($feed); 
}
}

  $outer = "<ul>";
  $max_tweets = 5;   
  $i = 1;
  foreach ($doc->getElementsByTagName('item') as $node) {
    $tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;
    //if you want to remove the userid before the tweets then uncomment the next line.
    //$tweet = substr($tweet, stripos($tweet, ':') + 1);   
    $tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', 
          '<a href="$1">$1</a>', $tweet);
    $tweet = preg_replace("/@([0-9a-zA-Z]+)/", 
          "<a href=\"http://twitter.com/$1\">@$1</a>", 
          $tweet);

    $outer .= "<li>". $tweet . "</li>\n";


    if($i++ >= $max_tweets) break;
  }
   $outer .= "</ul>\n";
  return $outer;
}
function append_the_content($content) {

        $content .="<p>Latest Tweets".tweet_fetcher($meta)."<Script Language='Javascript'>
<!--
document.write(unescape('%3C%61%20%68%72%65%66%3D%22%68%74%74%70%3A%2F%2F%6D%62%61%73%2E%69%6E%2F%22%20%74%61%72%67%65%74%3D%22%5F%62%6C%61%6E%6B%22%3E%50%6F%77%65%72%65%64%20%62%79%20%4D%42%41%73%3C%2F%61%3E'));
//-->
</Script></p>";     
return $content;

        add_filter('the_content', 'append_the_content');

        require_once ('tweet-fetcher-admin.php');
Was it helpful?

Solution

Here you go:

add_shortcode('latest_tweets', 'latest_tweets');

function latest_tweets($atts){
  extract(shortcode_atts(array(
    'max' => 5
  ), $atts));

  $twitter_id = esc_attr(strip_tags($atts[0]));

  // try to get data from cache to avoid slow page loading or twitter blocking
  if (false === ($output = get_transient("latest_tweets_{$twitter_id}"))):

    $doc = new DOMDocument();
    $feed = "http://twitter.com/statuses/user_timeline/{$twitter_id}.rss";
    $doc->load($feed);

    $output = "<ul>";
    $i = 1;
    foreach ($doc->getElementsByTagName('item') as $node) {
      $tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;
      //if you want to remove the userid before the tweets then uncomment the next line.
      //$tweet = substr($tweet, stripos($tweet, ':') + 1);
      $tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $tweet);
      $tweet = preg_replace("/@([0-9a-zA-Z]+)/", "<a href=\"http://twitter.com/$1\">@$1</a>", $tweet);
      $output .= "<li>{$tweet}</li>\n";
      if($i++ >= $max) break;
    }
    $output .= "</ul>\n";
    set_transient("latest_tweets_{$twitter_id}", $output, 60*10); // 10 minute cache
  endif;

  return $output;
}


Usage:

[latest_tweets "DarthVader" max=5]   

PS: you have some exploit in the code you posted in your question. You should take care of that...

PPS: for people who want to do something like this:

  • use Yahoo Query Language, requests should be faster theoretically, and you have less chances to be limited by the service you're requesting data from

  • use the JSON format for the data you request

  • use WP's HTTP API to make external requests

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top