Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

10 super useful WordPress shortcodes

DZone's Guide to

10 super useful WordPress shortcodes

· Web Dev Zone
Free Resource

Never build auth again! Okta makes it simple to implement authentication, authorization, MFA and more in minutes. Try the free developer API today! 

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) {
        extract(shortcode_atts(array(
			"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 ) {
    extract(shortcode_atts(array(
        '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:

[mailto]email@yourdomain.com[/mailto]

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:

[tweet]

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) {
	extract(shortcode_atts(array(
		"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:

[postimage]

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) {
	extract(shortcode_atts(array(
		"value" => 'http://',
		"width" => '475',
		"height" => '350',
		"name"=> 'movie',
		"allowFullScreen" => 'true',
		"allowScriptAccess"=>'always',
	), $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:

include_once(ABSPATH.WPINC.'/rss.php');

function cwc_readRss($atts) {
    extract(shortcode_atts(array(
	"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/



Launch your application faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:

Published at DZone with permission of Jean-Baptiste Jung, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}