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
chloeruthwren — RT @KatieRLi: Do you wonder if, Hungary is very sad, That Turkey is far? #HaikuFriday
sexymexy17 — RT @KatieRLi: Do you wonder if, Hungary is very sad, That Turkey is far? #HaikuFriday
anna_cli — RT @KatieRLi: Do you wonder if, Hungary is very sad, That Turkey is far? #HaikuFriday
KatieRLi — Do you wonder if, Hungary is very sad, That Turkey is far? #HaikuFriday
HelloooKevinn — Chillin in Pittsburg. To dinner then Blue Man Group. Peace out, yah bitches. #HaikuFriday #idowhatiwant @tiffypoohbear @JLeach26 @NikkiiNY
EMMAVON2 — Greek food fest, you are very beautiful, irish dancers man #haikufriday
bazmaniandevil — Bustin out down South... pickin, jammin, suds slammin. The road is callin' #americanhaiku #haikufriday #ontheroad
_Talaska_ — RT @youscreamjenna: I am a sea monster they call me big ass fat ben don't make fun of me @_Talaska_ #HaikuFriday
marshajc — No time to haiku Got 24 tons of stone Sitting in driveway #HaikuFriday @bvisconti @diddley_squat
diddley_squat — @bvisconti @marshajc @hitrecordjoe @slimfast965 Found myself alone My iPad all a Twitter Longing for Marsh Mac #HaikuFriday
bvisconti — #HaikuFriday | Lady at work stinks. I don't think she cleans her butt. Sad that my nose smells.
bvisconti — @marshajc @diddley_squat @hitRECordJoe @SethMacFarlane @slimfast965---> I challenge you to #HaikuFriday!
KarenMracek — The week is over/Time for relaxing and fun/Two days is not long #HaikuFriday #Twaiku
twitchy67 — @kesrows @passittobulis this is important. Haiku friday's a good thing. Makes you think harder. #haikufriday
youscreamjenna — I am a sea monster they call me big ass fat ben don't make fun of me @_Talaska_ #HaikuFriday
twitchy67 — @kesrows yeah same with me, dude... just no interest in it. Still too depressed. #haikufriday
djmorris55 — Save #haikufriday/ Compose one right this minute/ Use it or lose it.
JJKesq — Call me old fashioned, But energy drinks are trash. Water is classic. #haikufriday
twitchy67 — it is may 18...big happy birthday manny...malhotra that is... #haikufriday #canucks
PokerVixen — RT @twitchy67: Facebook I P O... douchebags clamouring for pie... zuckerberg giggles. #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.
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');
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:
Warning: Invalid argument supplied for foreach() in /web/sites/ajorb11/aaron.jorb.in/wp-content/plugins/jorbin-shortcodes.php on line 26
This one doesn’t have any attribute. The output is in an unordered list with the class of twitter-trends.
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');
If you use any of these, let me know. If there are any improvements or more you want to see, comment below.













I just read this and found it useful, however, I did want to point out two issues.
One, some of the code is FUBAR as the > became > instead.
Second, when using the Twitter interface via your code, Twitter does not return retweets but they use them against the count. For example, if you request 1 and it is a retweet, you are returned nothing. As for 5, and 4 are retweets, you get one.
Thank you for the code, it was what I needed to get a jumpstart on including tweets.
Thanks Gerry. I know twitter has updated the API more then a bit since I wrote this. I would encourage anyone using this now to check out https://dev.twitter.com/docs/api to see how everything is working.