One of WordPress greatest feature since version 2.5 is shortcodes. All themes comes with a function.php file. You just have to put the shortcodes in your themes function.php and you can start using them.
Shortcodes are handy for features you want to use in your posts but not in all of them. Plug-ins usually are standard applied on all your posts. With shortcodes you get to be selective and it saves some typing.
I listed 7 plus shortcodes that comes in handy in your daily or not so daily posting routine. The last shortcode, StumbleUpon, is provided by me. A very simple shortcode that can be easily modified for use with other social media links.
Display Google Ad Sense anywhere in your posts
[php]
function showads() {
return ‘
‘;
}
add_shortcode(‘adsense’, ‘showads’);
[/php]
[php][adsense][/php]
Replace the Javascript with your own Google Ad Sense Javascript.
Show Members Only Content
Visitors
[php]
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 ”;
}
[/php]
[php]
[visitor]
Some content for the people just browsing your site.
[/visitor]
[/php]
Members
[php]
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 ”;
}
[/php]
[php]
[member]
This is members-only content.
[/member]
[/php]
Add private notes to your WordPress blog posts
[php]
add_shortcode( ‘note’, ‘sc_note’ );
function sc_note( $atts, $content = null ) {
if ( current_user_can( ‘publish_posts’ ) )
return ‘
‘;
return ”;
}
[/php]
[php]
[note]
This is a personal note that only admins can see!
[/note]
[/php]
Notes will be displayed in
[html]
[/html]
making it easier to style them in your stylesheet. Off course you can change the HTML in function sc_note to your own liking.
Protect an email address from spam
*Not 100% foolproof
[php]
function munge_mail_shortcode( $atts , $content=null ) {
for ($i = 0; $i < strlen($content); $i++) $encodedmail .= "&#" . ord($content[$i]) . ';'; return '‘.$encodedmail.’‘;
}
add_shortcode(‘mailto’, ‘munge_mail_shortcode’);
[/php]
[php]
[mailto][email protected][/mailto]
[/php]
Mail is shown as email@email.com
in the source.
Display a Flickr badge in your posts
*Requieres PHP5
[php]
function flickr_badge_shortcode($atts){
//Here’s our defaults
$query_atts = shortcode_atts(array(‘count’ => ‘6’, ‘display’ => ‘latest’, ‘source’ => ‘user’, ‘size’ => ‘t’, ‘user’ => ”, ‘layout’ => ‘x’, ‘tag’ => ”, ‘group’ => ”, ‘set’ => ”), $atts);
return sprintf(‘
‘, http_build_query($query_atts));
}
add_shortcode(‘flickrbadge’, ‘flickr_badge_shortcode’);
[/php]
[php]
[flickrbadge count=”4″ layout=”h” display=”latest” size=”t” source=”all_tag” tag=”fish”]
[/php]
Display your own Flickrbadge
[php]
$flickr = ‘
‘;
return $flickr;
}
add_shortcode(‘latestflickr’, ‘flickr_badge_shortcode’);
[/php]
Change the parameters in the Javascript; count=6&display=latest&size=s&layout=h&source=user&user=xxxxxx
or use the Flickr Badge generator and choose HTML badge.
[php]
[latestflickr]
[/php]
Dynamic permalinks
The advantage of using a ‘dynamic’ permalink is that you are not using a relative link to other page or posts. This way of you change the title of a post or page later on the links will still work as they are dependent on the id not on the slug.
[php]
function permalink_thingy($atts) {
extract(shortcode_atts(array(
‘id’ => 1,
‘text’ => “” // default value if none supplied
), $atts));
if ($text) {
$url = get_permalink($id);
return “$text“;
} else {
return get_permalink($id);
}
}
add_shortcode(‘permalink’, ‘permalink_thingy’);
[/php]
[php]
[/php]
Add Stumble or other social media buttons
[php]
function stumble_shortcode( $atts ) {
$stumble = ‘
‘;
return $stumble;
}
add_shortcode(‘stumble’, ‘stumble_shortcode’);
[/php]
[php]
[stumble]
[/php]
Using this shortcode example of StumbleUpon you can create your own buttons for different social media such as Reddit, Facebook, Delicious, etc.
This is very handy if you do not want to use a social media plugin that automatically adds buttons to all your posts. If you rather be selective with promoting your content using different social media per post then this shortcode is the solution.