Awesome Shortcodes For Your WordPress Blog

Awesome Shortcodes For Your WordPress Blog

10 Awesome Shortcodes For Your WordPress BlogLast week I featured three tips for effectively utilizing shortcodes. Now what’s the point in having all of that knowledge if you can’t use it? It’s time to get down and dirty with some shortcodes that you can use on your WordPress blog right now.

Your level (or lack) of expertise doesn’t matter – shortcodes can be successfully implemented by anyone who knows how to copy and paste!

Every single one of the shortcodes below has been tested and works with WordPress:

Hello World!

Don’t worry – I am not trying to suggest that a really helpful shortcode is one that allows you to display “Hello World!” But this super simple shortcode is included because it demonstrates what you can do with just a little HTML knowledge.

The potential applications are endless – you could use a shortcode like this any time that you wish to display something on more than one page (such as social sharing buttons, a newsletter signup form, or a simple call to action). Rather than typing out the same code every time, just reference the shortcode.

function HelloWorldShortcode() {
return '<p>Hello World!</p>';
add_shortcode('helloworld', 'HelloWorldShortcode');

As you can see, it is very simply to change what message you want to display, and you can include full-blown HTML and CSS.

Whenever you want to include the content stored in the shortcode, just enter this into your WordPress editor:



Most Recent Post

Having the ability to display your most recent post is one of those “stock” features that can be useful in a multitude of applications. For instance, I recently built a website for a business who wanted their most recent blog post to be displayed in the footer of their website. I used this shortcode to make it happen.

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

$getpost = get_posts( array('number' => 1) );

$getpost = $getpost[0];

$return = $getpost->post_title . "<br />" . $getpost->post_excerpt . "…";

$return .= "<br /><a href='" . get_permalink($getpost->ID) . "'><em>read more →</em></a>";

return $return;

add_shortcode('newestpost', 'wptuts_recentpost');

As you can no doubt see this shortcode includes the post title, the excerpt, and a “read more” link as standard. Just enter this shortcode whenever you want to display your newest post:



Having a related posts section on each and every blog post page is a great way of increasing user engagement. If someone has enjoyed what you read, they may will be willing to read more, but you need to put it in front of them.

Whilst there are plenty of related posts plugins, this will do the job just as well:

function related_posts_shortcode( $atts ) {

'limit' => '5',
), $atts));

global $wpdb, $post, $table_prefix;

if ($post->ID) {

$retval = '

// Get tags
$tags = wp_get_post_tags($post->ID);
$tagsarray = array();
foreach ($tags as $tag) {
$tagsarray[] = $tag->term_id;
$tagslist = implode(',', $tagsarray);

// Do the query
$q = "
SELECT p.*, count(tr.object_id) as count
FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p
WHERE tt.taxonomy ='post_tag'
AND tt.term_taxonomy_id = tr.term_taxonomy_id
AND tr.object_id = p.ID
AND tt.term_id IN ($tagslist)
AND p.ID != $post->ID
AND p.post_status = 'publish'
AND p.post_date_gmt < NOW()
GROUP BY tr.object_id
ORDER BY count DESC, p.post_date_gmt DESC
LIMIT $limit;";

$related = $wpdb->get_results($q);

if ( $related ) {
foreach($related as $r) {
$retval .= '
<li><a title="'.wptexturize($r->post_title).'" href="'.get_permalink($r->ID).'">'.wptexturize($r->post_title).'</a></li>
} else {
$retval .= '
<li>No related posts found</li>
$retval .= '</ul>
return $retval;
add_shortcode('related_posts', 'related_posts_shortcode');

This shortcode will display the posts in a simple list, but with a bit of CSS trickery you can turn it into something a little more interesting.

Whether or not a post is related depends upon your use of post tags. Whenever you want to display related posts, just enter this:



Display AdSense

This is another feature that is offered in plugins, but can be easily replicated with a simple shortcode.

Displaying AdSense on your site can be a pain – you certainly don’t want to be inserting the ads manually on every post page. So use this shortcode instead:

function showads() {
return '<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%3E%3C!--%0Agoogle_ad_client%20%3D%20%22xxx%22%3B%0Agoogle_ad_slot%20%3D%20%22xxx%22%3B%0Agoogle_ad_width%20%3D%20xxx%3B%0Agoogle_ad_height%20%3D%20xxx%3B%0A%2F%2F--%3E%0A%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />

add_shortcode('adsense', 'showads');

Obviously you will need to place your own AdSense account and ad details into the relevant spots, but once you have done so, just enter this where you want the ad to appear:


Our best pro WP tools in one bundle

Try free for 7 days
30-day money-back


Content for Registered Visitors Only

If you have a membership site or simply only want to display a certain piece of content to those who are registered as a user on your site, this simple shortcode will allow you tailor what is shown to whom.

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 '';

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 actually two shortcodes in one. The first will display content to those who are not registered, and the second will display content to those who are registered. Just place these shortcodes where it is appropriate:

[visitor]Displayed to visitors[/visitors]

[member]Displayed to members[/member]


RSS Feed Display

You can use this shortcode to display the RSS feed of any blog of your liking.


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

return wp_rss($feed, $num);

As you may notice, there are a couple of variables that we need to define in order for this shortcode to work. Just enter the following where you would like the feed to appear:

[rss feed="feed-url" num="number-of-posts-to-display"]


Disable WordPress Auto Formatting

For anyone who has suffered from WordPress getting all up in their junk (that’s the technical term) with its auto formatting, this could well be the answer to your prayers:

function my_formatter($content) {
$new_content = '';
$pattern_full = '{(\[raw\].*?\[/raw\])}is';
$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

foreach ($pieces as $piece) {
if (preg_match($pattern_contents, $piece, $matches)) {
$new_content .= $matches[1];
} else {
$new_content .= wptexturize(wpautop($piece));

return $new_content;

remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');

add_filter('the_content', 'my_formatter', 99);

Just wrap the bit of text you want to remain untouched in the following shortcode, and you’re golden:

[raw]This is gonna be raw, yo'[/raw]


Embed a PDF

Often you will link to PDFs from your site, but wouldn’t it be great if you could include them within the content of your page or post itself? Then the user doesn’t have to leave your site to read it.

function viewpdf($attr, $url) {
return '<iframe src="' . $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', 'viewpdf');

You’ll need to define the source of the PDF in addition to its dimensions:

[embedpdf width="xxxpx" height="xxxpx"][/embedpdf]

Feed Only Content

Getting more RSS subscribers is a great way of increasing repeat visits to your blog, and one way of increasing said subscribers is to offer exclusive content (or even perhaps an exclusive competition) via your RSS feed content only.

With this shortcode, you can easily display RSS feed only content:

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

When you want to include text for your RSS readers only, just use the following shortcode:

[feedonly]Only my most loyal readers can see this text.[/feedonly]


And Finally – YOUR Shortcodes!

The eagle-eyed amongst you may have spotted that I have only included 9 shortcodes. If there is one thing I found out from trawling the web in search of shortcodes, it is that there is no shortage of ideas out there.

Which is why I now turn to you, humble WPMU reader, expert in all things WordPress that you are.

Creative Commons photo courtesy of pvera

Do you use any shortcodes on your blog, or do you have any cool shortcode ideas? Share them in the comments section, so that we can all reap the benefits of your genius!

Tom Ewer

Tom Ewer Tom is a professional blogger and WordPress enthusiast. He created plugins and got into WordPress after launching a website in 2011. He also started working as a freelance blogger that year and now owns WordCandy – a content marketing agency for WordPress businesses.