10 Awesome Shortcodes For Your WordPress Blog

10 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 these shortcodes has been tested and works with WordPress 3.3.1.

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.

1
2
3
4
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:

1
[helloworld]

Source: http://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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:

1
[newestpost]

Source: http://wp.tutsplus.com/tutorials/getting-started-with-wordpress-shortcodes/

Related Posts

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function related_posts_shortcode( $atts ) {

extract(shortcode_atts(array(
'limit' => '5',
), $atts));

global $wpdb, $post, $table_prefix;

if ($post->ID) {

$retval = '
<ul>';

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

1
[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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function showads() {
return '<script type="text/javascript"><!--
google_ad_client = "xxx";
google_ad_slot = "xxx";
google_ad_width = xxx;
google_ad_height = xxx;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</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:

1
[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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
add_shortcode( 'visitor', 'visitor_check_shortcode' );

function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() &amp;&amp; !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() &amp;&amp; !is_null( $content ) &amp;&amp; !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:

1
2
3
[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.

1
2
3
4
5
6
7
8
9
10
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:

1
[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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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:

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

Source: http://ideasandpixels.com/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.

1
2
3
4
function 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', 'viewpdf');

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

1
<pre>[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:

1
2
3
4
5
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:

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

Source: http://kovshenin.com/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. 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!

Creative Commons photo courtesy of pvera