Polyhistoric Man Of The Web

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.

function show_current_year(){<br />
	return date(&#8216;Y&#8217;);<br />
}<br />
add_shortcode(&#8216;show_current_year&#8217;, &#8216;show_current_year&#8217;);

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.

<br />
function show_bookmark_image_list($atts){<br />
    extract(shortcode_atts(array(<br />
    &#8216;catagory_name&#8217; =&gt; false<br />
    ), $atts));<br />
    if ( $catagory_name == false )<br />
        $bookmarks = get_bookmarks();<br />
    else<br />
        $bookmarks = get_bookmarks(&quot;catagory_name=$catagory_name&quot;);<br />
    ob_start();<br />
    echo &quot;&lt;ul class=&#8217;link-image-list&#8217;&gt;&quot;;<br />
    foreach($bookmarks as $bookmark){<br />
        echo &quot;&lt;li&gt;&lt;a href=&#8217;&quot;.esc_url($bookmark-&gt;link_url) .&quot;&#8217;&gt;&lt;img src=&#8217;&quot;.esc_url($bookmark-&gt;link_image).&quot;&#8217; /&gt;&lt;/a&gt;&lt;/li&gt;&quot;;<br />
    }<br />
    echo &quot;&lt;/ul&gt;&quot;;<br />
    $list = ob_get_clean();<br />
    return $list;<br />
}<br />
add_shortcode(&#8216;show_bookmark_image_list&#8217;, &#8216;show_bookmark_image_list&#8217;);<br />

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

This was removed in August 2013 since it relied upon a non deprecated twitter api

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(&#8216;widget_text&#8217;, &#8216;do_shortcode&#8217;);

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.


If you have a reaction, you should get a blog and send me a webmention or pingback.


*/