7 Very Handy Shortcodes for WordPress

One of WordPress greatest feature since version 2.5 is shortcodes. All themes comes with a function.php file. You just have to put the shortcodes in your themes function.php and you can start using them.

Shortcodes are handy for features you want to use in your posts but not in all of them. Plug-ins usually are standard applied on all your posts. With shortcodes you get to be selective and it saves some typing.

I listed 7 plus shortcodes that comes in handy in your daily or not so daily posting routine. The last shortcode, StumbleUpon, is provided by me. A very simple shortcode that can be easily modified for use with other social media links.

Display Google Ad Sense anywhere in your posts

function showads() {
  return '<script type="text/javascript"><!--
google_ad_client = "xxxxxxx";
google_ad_slot = "xxxxxxxxx";
google_ad_width = 468;
google_ad_height = 60;
<script type="text/javascript"

add_shortcode('adsense', 'showads');

Replace the Javascript with your own Google Ad Sense Javascript.


Show Members Only Content


add_shortcode( 'visitor', 'visitor_check_shortcode' );

function visitor_check_shortcode( $atts, $content = null ) {
     if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
        return $content;
    return '';


Some content for the people just browsing your site.



add_shortcode( 'member', 'member_check_shortcode' );

function member_check_shortcode( $atts, $content = null ) {
     if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
        return $content;
    return '';


This is members-only content.



Add private notes to your WordPress blog posts

add_shortcode( 'note', 'sc_note' );

function sc_note( $atts, $content = null ) {
     if ( current_user_can( 'publish_posts' ) )
        return '<div class="note">'.$content.'</div>';
    return '';

This is a personal note that only admins can see!

Notes will be displayed in

<div class="note"></div>

making it easier to style them in your stylesheet. Off course you can change the HTML in function sc_note to your own liking.


Protect an email address from spam

*Not 100% foolproof

function munge_mail_shortcode( $atts , $content=null ) {

    for ($i = 0; $i < strlen($content); $i++) $encodedmail .= "&#" . ord($content[$i]) . ';';

  return '<a href="mailto:'.$encodedmail.'">'.$encodedmail.'</a>';

add_shortcode('mailto', 'munge_mail_shortcode');

[mailto][email protected][/mailto]

Mail is shown as &#101;&#109;&#97;&#105;&#108;&#64;&#101;&#109;&#97;&#105;&#108;&#46;&#99;&#111;&#109;
in the source.


Display a Flickr badge in your posts

*Requieres PHP5

function flickr_badge_shortcode($atts){

//Here's our defaults
$query_atts = shortcode_atts(array('count' => '6', 'display' => 'latest', 'source' => 'user', 'size' => 't', 'user' => '', 'layout' => 'x', 'tag' => '', 'group' => '', 'set' => ''), $atts);

    return sprintf('<div class="flickr_badge"><script src="http://www.flickr.com/badge_code_v2.gne?%s" type="text/javascript"></script></div>
', http_build_query($query_atts));

add_shortcode('flickrbadge', 'flickr_badge_shortcode');

[flickrbadge count="4" layout="h" display="latest" size="t" source="all_tag" tag="fish"]


Display your own Flickrbadge

    $flickr = '<div class="flickr_badge"><script type="text/javascript" src="http://www.flickr.com/badge_code_v2.gne?count=6&amp;display=latest&amp;size=s&amp;layout=h&amp;source=user&amp;user=xxxxxx"></script></div>';

   return $flickr;

add_shortcode('latestflickr', 'flickr_badge_shortcode');

Change the parameters in the Javascript; count=6&display=latest&size=s&layout=h&source=user&user=xxxxxx or use the Flickr Badge generator and choose HTML badge.


Dynamic permalinks

The advantage of using a ‘dynamic’ permalink is that you are not using a relative link to other page or posts. This way of you change the title of a post or page later on the links will still work as they are dependent on the id not on the slug.

function permalink_thingy($atts) {
        'id' => 1,
        'text' => ""  // default value if none supplied
  ), $atts));
  if ($text) {
      $url = get_permalink($id);
      return "<a href='$url'>$text</a>";
  } else {
       return get_permalink($id);
add_shortcode('permalink', 'permalink_thingy');

<a href="[permalink id=49]">Using without providing text</a>


Add Stumble or other social media buttons

function stumble_shortcode( $atts ) {

$stumble = '<div class="stumble"><script src="http://www.stumbleupon.com/hostedbadge.php?s=5&amp;r=">// <![CDATA[">// ]]></script></div>';

return $stumble;

add_shortcode('stumble', 'stumble_shortcode');


Using this shortcode example of StumbleUpon you can create your own buttons for different social media such as Reddit, Facebook, Delicious, etc.

This is very handy if you do not want to use a social media plugin that automatically adds buttons to all your posts. If you rather be selective with promoting your content using different social media per post then this shortcode is the solution.

  1. By diaby mohamed posted on August 12, 2010 at 12:21 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    very good and handful list. thanks

  2. Trackback