Tag Archives: Shortcodes

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.

WordPress Shortcodes – A how to by example

I love the WordPress shortcode api. I’m willing to stand on a roof and scream it. It makes so many aspects of developing a WordPress site so much easier. It allows you to add dynamic information to any post, page, and with a single line text widget. How easy you ask? Let’s look at some examples so I can show you just how easy it is to use.

A shortcode for this year

Sometimes you just want to include the year inline and there’s no reason you should have to change the blog posts every time you want to do it. Here’s a quick shortcode for the year.

1
2
3
4
function show_current_year(){
	return date('Y');
}
add_shortcode('show_current_year', 'show_current_year');

Just that easy. As you can see there are a few components to a basic short code. The first is a function that returns what we want the shortcode to return and the second is a call to add_shortcode with the first parameters being what we want the shortcode to be and the second is the function we want that shortcode to call.

A custom links list shortcode

So that last one was pretty easy. Maybe we should try something a little harder, like adding an unordered list of links that uses each links image. This time we also add an attribute to allow us to limit it to a specific category.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function show_bookmark_image_list($atts){
	extract(shortcode_atts(array(
	'catagory_name' => false
	), $atts));
	if ( $catagory_name == false )
		$bookmarks = get_bookmarks();
	else
		$bookmarks = get_bookmarks("catagory_name=$catagory_name");
	ob_start();
	echo "<ul class="link-image-list">";
	foreach($bookmarks as $bookmark){
		echo "<li><a href="&quot;.esc_url($bookmark-&gt;link_url) .&quot;"><img src="&quot;.esc_url($bookmark-&gt;link_image).&quot;"></a></li>";
	}
	echo "";
	$list = ob_get_clean();
	return $list;
}
add_shortcode('show_bookmark_image_list', 'show_bookmark_image_list');
</ul>

This time we added an attribute called Catagegory_name that we can then retrieve using the shortcode_atts function that also allows us to set a default incase no attribute is set. We then go through the list of bookmarks that we retrieve and echo out the results. Since we want our shortcode function to return the data, we wrap our echo is an output buffer. I prefer to use output buffers because it allows me to use echo (and makes the code more readable in my opinion), but you can also use string concoction and return the string if you want.

Most Recent Tweet

That one is a little more complex, but still allows us to condense a bit of work (and something that might change) This one is just as simple and came as an idea from Lyza Danger Gardner, who if you don’t follow in twitter you should (and if you haven’t seen lyza.com, go there now) . We are going to get a twitter users most recent tweet (or optionally more tweets), and display it along with there avatar and link to there twitter stream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function twitter_status($atts){
	extract(shortcode_atts(array(
	'screenname' =&gt; '',
	'count' =&gt; 1
	), $atts));
	$transient = "$screenname"."_$count"."_twitter_status";
	$statuses =  get_transient($transient);
	if ($statuses == true  )
	{
		return $statuses;
	}
	elseif ($screenname != false)
	{
		$site = "http://twitter.com/statuses/user_timeline.json?screen_name=$screenname&count=$count";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_URL, $site);
		$result = curl_exec($ch);
		$tweets = json_decode($result);
		ob_start();
		foreach ( (array) $tweets as $tweet){
			$tweetcontent = $tweet-&gt;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;user-&gt;profile_image_url).&quot;"><span class="twitter_shotcode_username"><a href="http://twitter.com/&quot;.$tweet-&gt;user-&gt;screen_name.&quot;">".$tweet-&gt;user-&gt;screen_name."</a>&nbsp;—&nbsp;</span>$newcontent</p>
			</div>";
 
		}
		$tweet_display = ob_get_clean();
		set_transient($transient, $tweet_display, 120);
		return $tweet_display;
	}
	else
	{
		return false;
	}
}
 
add_shortcode('twitter_status', 'twitter_status');

You’ll notice that this function relies upon json_decode, which was introduced in php version 5.2 . What if you’re running a version of php below 5.2? Well WordPress takes care of that thanks to the compat.php library in wp-includes. This uses the Services_JSON class. Our shortcode also makes use of the WordPress transients api to temporarily store the results of our request to the twitter api for two minutes. This way we don’t need to make the request every time the page is loaded.

Using Shortcodes inside Text/html widgets

Text widgets can be simple and are great for creating small blocks of content that you can place any where your theme allows. This simple hack will allow you to use shortcodes inside your text widgets. Just add the following single line to your theme’s functions.php file.

add_filter('widget_text', 'do_shortcode');

If you want to download and use these three simple shortcodes and enable the use of shortcodes in your text widgets, download this file, remove the .txt at the end of the file, upload it to your plugins folder, and activate the simple shortcodes plugin.

Those three simple sample shortcodes for WordPress should give you a bit of a base to create your own. What are your favorite shortcodes? What’s a shortcode that you wish existed? Comment below and let me know. Also let me know if there is any part of my code that you don’t understand, and I’ll try to help you understand it.

UPDATE

I’ve added two more twitter shortcodes. You can also download all of my shortcodes and follow along to the development of them on google code.