Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');
share|improve this question
    
This has been discussed in a chat room, you can find inspiration for an answer there. –  Jan Fabry Dec 20 '10 at 12:19
add comment

1 Answer

up vote 4 down vote accepted

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

share|improve this answer
    
You should name the exploit and point out why you choosed esc_attr as it might be questionable as well. –  hakre Dec 20 '10 at 17:30
    
the document.write(unescape(... part. esc_attr sanitizes the twitter_id in this case. –  onetrickpony Dec 20 '10 at 17:33
    
That part was "explained" in the chat. It is not written in the question, but from the chat I understood that ntechi intended to use both the shortcode and the regular version: if no shortcode is found the regular (post meta or blog option) Twitter account should be displayed. –  Jan Fabry Dec 20 '10 at 21:54
    
ok, but still you have to agree tha using document.write is evil, and encoding js like that makes it worse. –  onetrickpony Dec 21 '10 at 0:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.