Building on my WordPress Shortcode How To, here are two more Twitter shortcodes. I’ve also added a new project on google code to track all of my shortcodes.
The first new shortcode is for twitter search. It’s logically enough
[twitter-search phrase='#haikufriday']
Like my last twitter shortcode, it caches the results for two minutes. It also includes some other options. You can specify the number of tweets using the number attribute. There is a default of 20. You can also specify a max and min tweet id using max_id and since_id . Finally, you can specify the language with the lang attribute. This defaults to English.
[sourcecode language=”php”]
function jorbin_firestream_search($atts){
extract(shortcode_atts(array(
‘phrase’ => false,
‘lang’ => ‘en’,
‘max_id’ => false,
‘since_id’ => false,
‘number’ => ’20’
), $atts));
if (‘phrase’ == false){
return false;
}
//*/ Build our search url and transient name
$transient = ‘tweet-‘. esc_sql($phrase) . ‘&l=’ . esc_sql($lang);
$url = ‘http://search.twitter.com/search.json?q=’. urlencode($phrase) . ‘&show_user=true〈=’. urlencode($lang) .’&rpp=’ . $number;
if ($max_id != false){
$url .= ‘&max_id=’ . (int) $max_id;
$transient .= ‘&m=’ . (int) $max_id;
}
if ($since_id != false){
$url .= ‘&since_id=’ . (int) $since_id;
$transient .= ‘&s=’ . (int) $since_id;
}
if ( $tweet_display = get_transient($transient) ){
// It’s allready been brought
}
else {
if ($search = wp_remote_get( $url ) ){
$results = json_decode($search[‘body’]);
ob_start();
$tweets = $results->results;
//*/
foreach ( (array) $tweets as $tweet){
$tweetcontent = $tweet->text;
$newcontent = preg_replace(‘%@([^\s]*)%’, "<a href="http://twitter.com/\\1">@\\1</a>", $tweetcontent);
echo "<div class="twitter_shortcode"><p>
<img class="twitter_shortcode_image" src="".esc_url($tweet->profile_image_url).""><span class="twitter_shotcode_username"><a href="http://twitter.com/".$tweet->from_user."">".$tweet->from_user."</a> — </span>". $newcontent ."</p>
</div>";
}
$tweet_display = ob_get_clean();
set_transient($transient, $tweet_display, 300);
}
else
{
$tweet_display = "I’m sorry, no tweets are availailable at this time";
}
}
return apply_filters(‘jorbin_tweet_content’, $tweet_display) ;
}
add_filter(‘jorbin_tweet_content’, ‘make_clickable’ );
add_shortcode(‘twitter-search’, ‘jorbin_firestream_search’);
[/sourcecode]
Like before, there are some classes for you to work with that should make it easy for you to theme these shortcodes. If you want for me to add more, comment below.
The second shortcode allows you to get and display a list of the most recent trends on twitter using the shortcode:
[twitter-trends]
This one doesn’t have any attribute. The output is in an unordered list with the class of twitter-trends.
[sourcecode language=”php”]
function jorbin_twitter_trends(){
$transient=’twitter-trends’;
$url = ‘http://search.twitter.com/trends.json’;
if ( $tweet_display = get_transient($transient) ){
}
else{
$search = wp_remote_get( $url );
$results = json_decode($search[‘body’]);
$trends = $results->trends;
ob_start();
echo "<ul class="twitter-trends">";
foreach ($trends as $trend){
echo ‘<li><a href="’ . esc_url($trend->url) . ‘"> ‘. esc_html($trend->name) . ‘</a></li>’;
}
echo "</ul>";
$tweet_display = ob_get_clean();
set_transient($transient, $tweet_display, 120);
}
return $tweet_display;
}
add_shortcode(‘twitter-trends’, ‘jorbin_twitter_trends’);
[/sourcecode]
If you use any of these, let me know. If there are any improvements or more you want to see, comment below.