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 '

Hello World!

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

[helloworld]

Source: https://www.sitepoint.com/wordpress-shortcodes-tutorial/

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 . "
" . $getpost->post_excerpt . "…"; $return .= "
ID) . "">read more →"; 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:

[newestpost]

Source: https://code.tutsplus.com/articles/getting-started-with-wordpress-shortcodes–wp-21197

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 ) {    extract(shortcode_atts(array(  '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 .= '
  • '.wptexturize($r->post_title).'
  • '; } } else { $retval .= '
  • No related posts found
  • '; } $retval .= '
'; return $retval; } return; } 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:

[related_posts]

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

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 '<script>  <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:

[adsense]

Source: http://www.wprecipes.com/how-to-embed-adsense-anywhere-on-your-posts

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]

Source: http://justintadlock.com/archives/2009/05/09/using-shortcodes-to-show-members-only-content

RSS Feed Display

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

  include_once(ABSPATH.WPINC.'/rss.php');    function cwc_readRss($atts) {  extract(shortcode_atts(array(  "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"]

Source: http://www.wprecipes.com/how-to-display-any-rss-feed-on-your-wordpress-blog

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]

Source: https://www.ideasandpixels.com/articles/disable-wordpress-auto-formatting-short-code

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 '';  }  add_shortcode('embedpdf', 'viewpdf');    

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

[embedpdf width="xxxpx" height="xxxpx"]http://www.yoursite.com/your.pdf[/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]

Source: https://konstantin.blog/2011/snippet-a-feed-only-shortcode-for-wordpress/

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!

Tags: