Final week I featured three suggestions for successfully using shortcodes. Now what’s the purpose in having all of that information if you happen to can’t use it? It’s time to get down and soiled with some shortcodes that you should use in your WordPress weblog proper now.
Your degree (or lack) of experience doesn’t matter – shortcodes might be efficiently applied by anybody who is aware of copy and paste!
Each single one of many shortcodes beneath has been examined and works with WordPress:
Hi there World!
Most Latest Put up
Associated Posts
Show AdSense
Content material for Registered Guests Solely
RSS Feed Show
Disable WordPress Auto Formatting
Embed a PDF
Feed Solely Content material
Hi there World!
Don’t fear – I’m not attempting to counsel {that a} actually useful shortcode is one which lets you show “Hi there World!” However this tremendous easy shortcode is included as a result of it demonstrates what you are able to do with just a bit HTML information.
The potential purposes are countless – you would use a shortcode like this any time that you just want to show one thing on multiple web page (corresponding to social sharing buttons, a e-newsletter signup kind, or a easy name to motion). Moderately than typing out the identical code each time, simply reference the shortcode.
operate HelloWorldShortcode() {
return ‘<p>Hi there World!</p>’;
}
add_shortcode(‘helloworld’, ‘HelloWorldShortcode’);
As you possibly can see, it is extremely merely to vary what message you wish to show, and you’ll embody full-blown HTML and CSS.
Everytime you wish to embody the content material saved within the shortcode, simply enter this into your WordPress editor:
[helloworld]Supply: https://www.sitepoint.com/wordpress-shortcodes-tutorial/
Most Latest Put up
Being able to show your most up-to-date submit is a kind of “inventory” options that may be helpful in a large number of purposes. As an illustration, I lately constructed a web site for a enterprise who wished their most up-to-date weblog submit to be displayed within the footer of their web site. I used this shortcode to make it occur.
operate wptuts_recentpost($atts, $content material=null){
$getpost = get_posts( array(‘quantity’ => 1) );
$getpost = $getpost[0];
$return = $getpost->post_title . “<br />” . $getpost->post_excerpt . “…”;
$return .= “<br /><a href='” . get_permalink($getpost->ID) . “‘><em>learn extra →</em></a>”;
return $return;
}
add_shortcode(‘newestpost’, ‘wptuts_recentpost’);
As you possibly can little question see this shortcode contains the submit title, the excerpt, and a “learn extra” hyperlink as customary. Simply enter this shortcode everytime you wish to show your latest submit:
[newestpost]Supply: https://code.tutsplus.com/articles/getting-started-with-wordpress-shortcodes–wp-21197
Associated Posts
Having a associated posts part on every weblog submit web page is a good way of accelerating consumer engagement. If somebody has loved what you learn, they could will probably be prepared to learn extra, however you might want to put it in entrance of them.
While there are many associated posts plugins, it will do the job simply as nicely:
operate related_posts_shortcode( $atts ) {
extract(shortcode_atts(array(
‘restrict’ => ‘5’,
), $atts));
world $wpdb, $submit, $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 question
$q = “
SELECT p.*, depend(tr.object_id) as depend
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 depend DESC, p.post_date_gmt DESC
LIMIT $restrict;”;
$associated = $wpdb->get_results($q);
if ( $associated ) {
foreach($associated 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 associated posts discovered</li>
‘;
}
$retval .= ‘</ul>
‘;
return $retval;
}
return;
}
add_shortcode(‘related_posts’, ‘related_posts_shortcode’);
This shortcode will show the posts in a easy record, however with a little bit of CSS trickery you possibly can flip it into one thing somewhat extra attention-grabbing.
Whether or not or not a submit is said relies upon upon your use of submit tags. Everytime you wish to show associated posts, simply enter this:
[related_posts]Supply: http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/
Show AdSense
That is one other characteristic that’s provided in plugins, however might be simply replicated with a easy shortcode.
Displaying AdSense in your web site generally is a ache – you actually don’t wish to be inserting the advertisements manually on each submit web page. So use this shortcode as a substitute:
operate showads() {
return ‘<img src=”knowledge:picture/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7″ data-wp-preserve=”%3Cscriptpercent20typepercent3Dpercent22textpercent2Fjavascriptpercent22percent3Epercent3C!–%0Agoogle_ad_clientpercent20percent3Dpercent20percent22xxxpercent22percent3Bpercent0Agoogle_ad_slotpercent20percent3Dpercent20percent22xxxpercent22percent3Bpercent0Agoogle_ad_widthpercent20percent3Dpercent20xxxpercent3Bpercent0Agoogle_ad_heightpercent20percent3Dpercent20xxxpercent3Bpercent0Apercent2Fpercent2F–%3Epercent0Apercent3Cpercent2Fscriptpercent3E” data-mce-resize=”false” data-mce-placeholder=”1″ class=”mce-object” width=”20″ peak=”20″ alt=”<script>” title=”<script>” />
<img src=”knowledge:picture/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7″ data-wp-preserve=”%3Cscriptpercent20typepercent3Dpercent22textpercent2Fjavascriptpercent22percent0Asrcpercent3Dpercent22httppercent3Apercent2Fpercent2Fpagead2.googlesyndication.compercent2Fpageadpercent2Fshow_ads.jspercent22percent3Epercent0Apercent3Cpercent2Fscriptpercent3E” data-mce-resize=”false” data-mce-placeholder=”1″ class=”mce-object” width=”20″ peak=”20″ alt=”<script>” title=”<script>” />
‘;
}
add_shortcode(‘adsense’, ‘showads’);
Clearly you’ll need to position your personal AdSense account and ad particulars into the related spots, however after getting performed so, simply enter this the place you need the ad to look:
[adsense]Supply: http://www.wprecipes.com/how-to-embed-adsense-anywhere-on-your-posts
Content material for Registered Guests Solely
If in case you have a membership web site or just solely wish to show a sure piece of content material to those that are registered as a consumer in your web site, this easy shortcode will permit you tailor what’s proven to whom.
add_shortcode( ‘customer’, ‘visitor_check_shortcode’ );
operate visitor_check_shortcode( $atts, $content material = null )
add_shortcode( ‘member’, ‘member_check_shortcode’ );
operate member_check_shortcode( $atts, $content material = null ) {
if ( is_user_logged_in() && !is_null( $content material ) && !is_feed() )
return $content material;
return ”;
}
That is really two shortcodes in a single. The primary will show content material to those that are not registered, and the second will show content material to those that are registered. Simply place these shortcodes the place it’s applicable:
[visitor]Exhibited to guests[/visitors] [member]Exhibited to members[/member]Supply: http://justintadlock.com/archives/2009/05/09/using-shortcodes-to-show-members-only-content
RSS Feed Show
You should use this shortcode to show the RSS feed of any weblog of your liking.
include_once(ABSPATH.WPINC.’/rss.php’);
operate cwc_readRss($atts) {
extract(shortcode_atts(array(
“feed” => ‘http://’,
“num” => ‘1’,
), $atts));
return wp_rss($feed, $num);
}
As you could discover, there are a few variables that we have to outline to ensure that this shortcode to work. Simply enter the next the place you desire to the feed to look:
[rss feed=”feed-url” num=”number-of-posts-to-display”]Supply: http://www.wprecipes.com/how-to-display-any-rss-feed-on-your-wordpress-blog
Disable WordPress Auto Formatting
For anybody who has suffered from WordPress getting all up of their junk (that’s the technical time period) with its auto formatting, this might nicely be the reply to your prayers:
operate my_formatter($content material) {
$new_content = ”;
$pattern_full = ‘{([raw].*?[/raw])}is’;
$pattern_contents = ‘{[raw](.*?)[/raw]}is’;
$items = preg_split($pattern_full, $content material, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($items 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);
Simply wrap the little bit of textual content you wish to stay untouched within the following shortcode, and also you’re golden:
[raw]That is gonna be uncooked, yo'[/raw]Supply: https://www.ideasandpixels.com/articles/disable-wordpress-auto-formatting-short-code
Embed a PDF
Typically you’ll hyperlink to PDFs out of your web site, however wouldn’t or not it’s nice if you happen to might embody them throughout the content material of your web page or submit itself? Then the consumer doesn’t have to depart your web site to learn it.
operate viewpdf($attr, $url) {
return ‘<iframe src=”http://docs.google.com/viewer?url=’ . $url . ‘&embedded=true” type=”width:’ .$attr[‘width’]. ‘; peak:’ .$attr[‘height’]. ‘;” frameborder=”0″>Your browser ought to help iFrame to view this PDF doc</iframe>’;
}
add_shortcode(’embedpdf’, ‘viewpdf’);
You’ll have to outline the supply of the PDF along with its dimensions:
[embedpdf width=”xxxpx” height=”xxxpx”]http://www.yoursite.com/your.pdf[/embedpdf]Feed Solely Content material
Getting extra RSS subscribers is a good way of accelerating repeat visits to your weblog, and a method of accelerating mentioned subscribers is to supply unique content material (and even maybe an unique competitors) through your RSS feed content material solely.
With this shortcode, you possibly can simply show RSS feed solely content material:
operate feedonly_shortcode( $atts, $content material = null) {
if (!is_feed()) return “”;
return $content material;
}
add_shortcode(‘feedonly’, ‘feedonly_shortcode’);
Whenever you wish to embody textual content on your RSS readers solely, simply use the next shortcode:
[feedonly]Solely my most loyal readers can see this textual content.[/feedonly]Supply: https://konstantin.weblog/2011/snippet-a-feed-only-shortcode-for-wordpress/
And Lastly – YOUR Shortcodes!
The eagle-eyed amongst you will have noticed that I’ve solely included 9 shortcodes. If there’s one factor I discovered from trawling the net searching for shortcodes, it’s that there isn’t a scarcity of concepts on the market.
Which is why I now flip to you, humble WPMU reader, knowledgeable in all issues WordPress that you’re.
Inventive Commons photograph courtesy of pvera
Subscribe to MarketingSolution.
Receive web development discounts & web design tutorials.
Now! Lets GROW Together!