10 super useful WordPress shortcodes

Shortcodes are real time-savers for all WordPress users. They allow you to do complex tasks easily, just by inserting the shortcode in your post editor. Today, I'm glad to show you 10 new and super useful WordPress shortcodes to boost your productivity!

Display a snapshot of any website

Want to be able to take a snapshot of any website, and display it on your blog? This cool shortcode allows you to do so. Just paste the following code into your functions.php file:

function wpr_snap($atts, $content = null) {
			"snap" => 'http://s.wordpress.com/mshots/v1/',
			"url" => 'http://www.catswhocode.com',
			"alt" => 'My image',
			"w" => '400', // width
			"h" => '300' // height
        ), $atts));
	$img = '<img src="' . $snap . '' . urlencode($url) . '?w=' . $w . '&h=' . $h . '" alt="' . $alt . '"/>';
        return $img;
add_shortcode("snap", "wpr_snap");

Once done, you can use the snap shortcode, as shown in the following example. It will display a CatsWhoCode.com snapshot on your own blog!

[snap url="http://www.catswhocode.com" alt="My description" w="400" h="300"]

Source: http://www.geekeries.fr/snippet/creer-automatiquement-miniatures-sites-wordpress/

Add a Paypal donation link easily

Many bloggers are asking support from their readers in the form of a Paypal donation. The following code creates a shortcode which will display a Paypal “donate” button on your site. Just paste the code below into your functions.php file:

function cwc_donate_shortcode( $atts ) {
        'text' => 'Make a donation',
        'account' => 'REPLACE ME',
        'for' => '',
    ), $atts));
    global $post;
    if (!$for) $for = str_replace(" ","+",$post->post_title);
    return '<a class="donateLink" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$account.'&item_name=Donation+for+'.$for.'">'.$text.'</a>';
add_shortcode('donate', 'cwc_donate_shortcode');

Source: http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/

Obfuscate email addresses

As most of you know, spam bots are constantly scanning the internet in order to find emails to spam. Of course, you don’t want to receive spam, but what if you need to display your email (or someone else) on your blog? This code will create a shortcode which will obfuscate email adresses. As usual, let’s start by creating the shortcode: paste the code into your functions.php file.

function cwc_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', 'cwc_mail_shortcode');

Then you can use the shortcode, which is pretty easy:


Source: http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/

Create private content

If you want to create some private content that only registered users are able to see, the following shortcode is the solution to your problem. Paste the code below into your functions.php file in order to create the shortcode:

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

add_shortcode( 'member', 'cwc_member_check_shortcode' );

Then, proceed as shown below to create some member-only content:

[member]This text will be only displayed to registered users.[/member]

Source: http://snipplr.com/view.php?codeview&id=46936

Embed a PDF in an iframe

This is definitely the easiest way to display a PDF file on your website: The PDF is loaded through Google docs and then displayed in an iframe, on your own site.
To use this shortcode, first paste the code below into your functions.php file:

function cwc_viewpdf($attr, $url) {
    return '<iframe src="http://docs.google.com/viewer?url=' . $url . '&embedded=true" style="width:' .$attr['width']. '; height:' .$attr['height']. ';" frameborder="0">Your browser should support iFrame to view this PDF document</iframe>';
add_shortcode('embedpdf', 'cwc_viewpdf');

Then, use the following syntax to display a PDF. As you can see, it is possible to define width and height, which means that this shortcode will fit great on your blog, nevermind its layout.

[embedpdf width="600px" height="500px"]http://infolab.stanford.edu/pub/papers/google.pdf[/embedpdf]

Source: http://snipplr.com/view.php?codeview&id=35682

“Feed only” content shortcode

This shortcode create some content that will only be displayed on your RSS feed. Quite good to display some RSS-only ads, or an important message for your feed readers. The function have to be pasted in your functions.php. What, you guessed that? 😉

function cwc_feedonly_shortcode( $atts, $content = null) {
	if (!is_feed()) return "";
	return $content;
add_shortcode('feedonly', 'cwc_feedonly_shortcode');

Then, you can use the shortcode as shown below:

[feedonly]Dear RSS readers, please visit <a href="http://yourwebsite.com">my website</a> and click on a few ads[/feedonly]

Source: http://kovshenin.com/archives/snippet-a-feed-only-shortcode-for-wordpress/

“Retweet” shortcode using Tweetmeme

Tweeter is definitely a great source of trafic for bloggers. This is why this “Retweet” shortcode can be very useful. Paste the following code in your functions.php file in order to create the shortcode:

function tweetmeme(){
	return '<div class="tweetmeme"><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script></div>';
add_shortcode('tweet', 'tweetmeme');

Once done, you can display the Tweetmeme “retweet” button anywhere on your posts. In WordPress editor, make sure you are in HTML mode and insert the following:


Source: http://www.wprecipes.com/wordpress-tip-create-a-tweetmeme-retweeet-shortcode

Display the last image attached to post

Instead of dealing with image url, a simple shortcode can retrieve and display the last image attached to post. Paste the code below into your functions.php file in order to create the shortcode:

function cwc_postimage($atts, $content = null) {
		"size" => 'thumbnail',
		"float" => 'none'
	), $atts));
	$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . get_the_id() );
	foreach( $images as $imageID => $imagePost )
	$fullimage = wp_get_attachment_image($imageID, $size, false);
	$imagedata = wp_get_attachment_image_src($imageID, $size, false);
	$width = ($imagedata[1]+2);
	$height = ($imagedata[2]+2);
	return '<div class="postimage" style="width: '.$width.'px; height: '.$height.'px; float: '.$float.';">'.$fullimage.'</div>';
add_shortcode("postimage", "cwc_postimage");

Once done, you can display the last image by using the shortcode:


Source: http://www.wprecipes.com/wordpress-shortcode-easily-display-the-last-image-attached-to-post

Embed Youtube videos

If you often post videos from Youtube on your blog, this shortcode will make you save a lot of time. Let’s start by creating it by pasting the code below into your functions.php file:

function cwc_youtube($atts) {
		"value" => 'http://',
		"width" => '475',
		"height" => '350',
		"name"=> 'movie',
		"allowFullScreen" => 'true',
	), $atts));
	return '<object style="height: '.$height.'px; width: '.$width.'px"><param name="'.$name.'" value="'.$value.'"><param name="allowFullScreen" value="'.$allowFullScreen.'"></param><param name="allowScriptAccess" value="'.$allowScriptAccess.'"></param><embed src="'.$value.'" type="application/x-shockwave-flash" allowfullscreen="'.$allowFullScreen.'" allowScriptAccess="'.$allowScriptAccess.'" width="'.$width.'" height="'.$height.'"></embed></object>';
add_shortcode("youtube", "cwc_youtube");

Using the shortcode is pretty easy:

[youtube value="http://www.youtube.com/watch?v=1aBSPn2P9bg"]

Source: http://wpsnipp.com/index.php/functions-php/display-youtube-video-with-embed-shortcode/

Embed a RSS feed

This shortcode allows you to embed any RSS feed on your blog posts. Definitely cool for showcasing other blogs on your site! Just paste the code below into your functions.php file:


function cwc_readRss($atts) {
	"feed" => 'http://',
      "num" => '1',
    ), $atts));

    return wp_rss($feed, $num);

add_shortcode('rss', 'cwc_readRss');

Then, you can use the shortcode as shown below:

[rss feed="http://feeds.feedburner.com/catswhocode" num="5"]  

Source: http://coding.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/

  • Isn’t pasting the Youtube address, and removing the link anchor from it, automatically making it embeded ? I think that was a feature from WordPress.com, that was later implimented in the standard version, correct me if I’m wrong.

    • No, you’re correct. Just paste the URL on it’s own line and let WordPress do the rest.

      Although since this post is about shortcodes, you can use one if you want using the built-in optional shortcode:


  • Thank you, this is so nice!
    I was using nyroModal for youtube embedding (works great as well) but this is a nice solution as well 🙂
    Will try the snapshot feature this week.


  • Well. I came through delicious as one of the top saved article. I thought what would be here. But after reaching here I understood what functions.php can do. Thanks 🙂

  • You shouldn’t need the YouTube shortcode when using oembed, which is enabled by default on the most recent versions of WordPress.

  • What diference does it make to embed pdf as shown instead of creating an iframe directly in the post?


  • I like the Obfuscate email addresses. We post plenty of contact info on our site, and that one should come in handy frequently. Thank you!

  • Was looking for a more jazzy version of the current latest post shortcode I use and instead I stumbled on this li’l gold mine. I didn’t find a better latest post shorcode but instead i found a ton of other cool shorcodes. My wordpress installs now have site snapshots and feed only content enabled by default 🙂

  • Very nice collection of shortcodes. I will consider to use part of them on my blog. Especially this one with retweet seems nice and useful.

  • Thanks for this great shortcode compilation. I was looking for the RSS and PDF shortcode desperately.

  • Well nice post.very nice collection of shortcodes.It seems that it is very useful.Thanks.

  • Great list of useful WordPress shortcodes. I especially like the snapshot snippet!

  • Thanks for this great shortcode list. It goes directly to me delicious. Many of them would be useful to me.

    It’ll be further better if you can show imagery examples… how those shortcuts for Paypal, Twitter or RSS will look like after using these shortcodes.

  • Excellent. Is it possible to use the email obfuscator in a widget field?

  • This is a fantastic selection! Thanks for putting this together, Jean-Baptiste!

  • Sarangan

    I have this shortcode in functions.php:

    add_shortcode(‘post_title’, ‘get_the_title’);

    which returns post title in content where I add [post_title]. The problem is that output doesn’t displays in rss feeds. What’s wrong with that?

    Can anyone help me to fix this? Thanks in advance.

  • Nice collection.

    May I ask how to display thumbnails in template instead of shortcode? That would be super cool.


  • Thanks for this Excellent post. its really help me to did a project today.

  • wow these shortcode really are super useful :). I just implemented the first one in to one of my fuctions. now if there is no image to show i just show a snapshot ot the website. works just great!
    i didn’t know about the google pdf viewer either but i’m sure about to work with it too.

    thank you, you really made my day!

    btw. the last SOURCE link is broken 404

  • Thank you for the email obfuscate code!! This is the first I’ve found that will prevent spam, doesn’t involve JavaScript, and will still allow my visitors to “click” to e-mail someone! So excited!!

  • Rashid Iqbal

    Assalam U Alaikum All!!!!

    Please visit below mention site.