More Twitter Shortcodes for WordPress

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

 

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="&quot;.esc_url($tweet-&gt;profile_image_url).&quot;"><span class="twitter_shotcode_username"><a href="http://twitter.com/&quot;.$tweet-&gt;from_user.&quot;">".$tweet-&gt;from_user."</a>&nbsp;—&nbsp;</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:

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-&gt;trends;
                ob_start();
                        echo "<ul class="twitter-trends">";
                        foreach ($trends as $trend){
                                echo '<li><a href="' . esc_url($trend-&gt;url) . '"> '. esc_html($trend-&gt;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.

Published by

Aaron Jorbin

Polyhistoric man of the web. Currently Technical Architect on the Conde Nast Platform Team and a WordPress Core Committer, he works to improve developer happiness and is dedicated to making the internet usable and enjoyable by everyone. He's probably wearing a bow tie right now.

2 thoughts on “More Twitter Shortcodes for WordPress”

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>