
WORKING WITH WORDPRESS SHORTCODES

WordPress shortcodes have been used often in plugins as well as themes as a approach to grasp a single some-more functionality, yet a need to cgange template files. You only type a shortcode word right in to your post. Some plugins as well as themes have have have have have have have make use of of of of of of of of them to supplement eventuality calendars, a tiny for origination announcements, whilst others have have have have have have have make use of of of of of of of of them for inserting contact forms.
Simply, WP shortcodes have been awesome.
However, what if you’re a theme / plugin developer wishing to have have have have have have have make use of of of of of of of of them for your subsequent good product, yet we have no thought where to start? We’re starting to repair which in this tutorial.
1 – Overview – Exactly Where We have been Going
We will begin out by seeking at a elementary ways which we can have have have have have have have make use of of of of of of of of WP shortcodes, afterwards we will pierce onto a tiny some-more modernized tricks. After a have have have make use of of of of sections, I’m starting to uncover we a tiny examples of real-world shortcodes, followed by even some-more ideas / references of what we could do with them.
2 – Getting Into a Basics
The initial we thing we should regularly do when operative with something WordPress, is check a WordPress Codex. It’s a good anxiety as well as starting point.
Shortcodes, identical to only about all else in WordPress, can be combined in a functions.php file, or from inside of your plugin record if you’re building a plugin.
Let’s begin elementary by origination a shortcode which we can have have have have have have have make use of of of of of of of of to supplement a single some-more styles to a tiny text.
1 2 3 4 |
function extra_style_shortcode( $atts, $content = null ) { return '<span style="color: blue; text-decoration: underline;">' . $content . '</span>'; } add_shortcode('extra-style', 'extra_style_shortcode'); |
This is a unequivocally elementary e.g. which serves roughly no purpose, yet simply illustrates a basis of shortcode creation. It allows us to do something identical to this in a WP posts/pages:
[extra-style]Hello World![/extra-style]
and have it outputted identical to this:
Hello World!
So what’s happening? It’s simple, we’re simply revelation WordPress to put all of a calm inside of a [ ] . . . [/ ] in to a non-static called $content, afterwards outputting $content inside of a camber tab which has a tiny elementary inline styling practical to it.
Okay, which was easy, right away let’s get in to something a tiny some-more complex. This time we’ll give a capability to conclude a tone of a calm regulating attributes.
1 2 3 4 5 6 7 |
function extra_style_shortcode( $atts, $content = null ) { extract(shortcode_atts(array( "color" => 'blue', ), $atts)); return '<span style="color: ' . $color . '; text-decoration: underline;">' . $content . '</span>'; } add_shortcode('extra-style', 'extra_style_shortcode'); |
Now we can do this:
[extra-style color=red]Hello World![/extra-style]
and we’ll see: Hello World!
or:
[extra-style color=orange]Hello World![/extra-style]
and we’ll see: Hello World!.
This e.g. uses this bit of code
extract(shortcode_atts(array( "color" => 'blue', ), $atts));
to remove a report inputted by a user for a non-static color. We have additionally tangible a default tone of blue in box no tone is defined.
Again, this e.g. is flattering many useless, yet a morality creates it unequivocally easy to understand.
Let’s get only a tiny bit some-more formidable prior to relocating on.
1 2 3 4 5 6 7 8 9 |
function extra_style_shortcode( $atts, $content = null ) { extract(shortcode_atts(array( "color" => 'blue', "size" => '14px', "padding" => '0px', ), $atts)); return '<span style="color: ' . $color . '; padding: ' . $padding . '; font-size: ' . $size . '; text-decoration: underline;">' . $content . '</span>'; } add_shortcode('extra-style', 'extra_style_shortcode'); |
By regulating a shortcode identical to this inside of a retard of text:
[extra-style color=purple size=18px padding=5px]Hello World![/extra-style]
we could see something identical to this:
Elementum odio? Sed, Hello World! proin pulvinar eros nascetur, massa urna aliquam turpis elit, adipiscing mauris montes ac, vel lacus placerat in adipiscing ridiculus rhoncus.
Well that’s sufficient of a purposeless examples; let’s demeanour at a tiny genuine shortcode examples.
3 – Making an Information Box
This shortcode e.g. will let we embody a good tiny summary box at a tip of your post to assistance locate readers’ eyes.
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 |
function box_shortcode( $atts, $content = null ) { extract( shortcode_atts( array( 'color' => 'yellow', 'size' => 'medium', ), $atts ) ); return ' <style type="text/css"> .shortcode_box { padding: 2px 4px; border: 1px plain #ccc; } .yellow { background: #ffd149; color: #666; } .blue { background: #a0c5ef; color: #333; } .gray { background: #f0f0f0; color: #333; } </style> <div class="shortcode_box ' . $size . ' ' . $color . '">' . $content . '</div>'; } add_shortcode('box', 'box_shortcode'); |
Now we can have have have have have have have make use of of of of of of of of a “box” shortcode to furnish something identical to this
by regulating this shortcode:
[box color=yellow]This is a summary box with critical report we should read.[/box]
By becoming opposite a color variable, we can have opposite colors of boxes:
With a tiny some-more CSS, we could additionally carry out a distance of a box, yet I’ll leave which to you.
4 – Create a Download Button
The routine for formulating a download symbol is unequivocally identical to formulating a summary box: we simplu hang a calm of a symbol inside of a div with a tiny special CSS3 styles practical to it.
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 |
function button_shortcode( $atts, $content = null ) { extract( shortcode_atts( array( 'color' => 'blue', 'size' => 'medium', ), $atts ) ); return ' <style type="text/css"> .shortcode_button { padding: 2px 8px; border: 1px plain #ccc; border-radius: 10px; -webkit-border-radius: 10px; -moz-border-radius: 10px; } .black { background: #ffd149; background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#636363), to(#332F2F)); background: -moz-linear-gradient(19% 75% 90deg,#332F2F, #636363); color: #f0f0f0; border-top-color: #1c1c1c; border-left-color: #1c1c1c; border-right-color: #525252; border-bottom-color: #525252; } .blue { background: #a0c5ef; background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#508BC7), to(#203F75)); background: -moz-linear-gradient(19% 75% 90deg,#203F75, #508BC7); color: #f0f0f0; border-top-color: #023778; border-left-color: #023778; border-right-color: #26609e; border-bottom-color: #26609e; } .large { width: 200px; } .medium { width: 120px; } .small { width: 80px; } </style> <div class="shortcode_button ' . $size . ' ' . $color . '">' . $content . '</div>'; } add_shortcode('button', 'button_shortcode'); |
We can arrangement a symbol by using:
[button color=black size=medium]<a href="#">Download</a>[/button]
In sequence to carry out a distance or tone of a button, all we have to do is emanate a CSS difficulty with a same name as a worth we submit for a size / color variables. For example, if we do size=large in a shortcode, we need a CSS difficulty called large in a stylesheet. You can see we have enclosed a single some-more styles in my on top of e.g. to spell out a tiny probable options.
5 – Buttons as well as Boxes Together
Shortcodes have been good since they additionally let us mix them to a outcome like:
Integer in, odio mattis ac! Nascetur augue odio in risus, arcu nunc, phasellus ultrices lectus velit, et tincidunt tristique. Integer vel pulvinar purus magnis.
by regulating a shortcode like:
[box color=blue]Porta ultricies. Amet odio amet, pellentesque elementum adipiscing sagittis enim, eu, proin placerat sed pid cum? Dictumst turpis integer. Adipiscing, porttitor scelerisque! Lorem turpis porttitor.
Integer in, odio mattis ac! Nascetur augue odio in risus, arcu nunc, phasellus ultrices lectus velit, et tincidunt tristique. Integer vel pulvinar purus magnis.
[button color=black size=small]<a href="#">Download[/button][/box]
There is a single tiny hitch, though. By default, WordPress does not concede we to hide shortcodes inside of alternative shortcodes. In sequence to get around that, we have to supplement a line to a functions.php or categorical plugin file:
add_filter('the_content', 'do_shortcode');
This will have WordPress routine both sets of shortcodes.
6 – Show Related Posts
This is a unequivocally accessible shortcode we can have have have have have have have make use of of of of of of of of to arrangement a list of posts associated to a stream post by comparing tags.
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 |
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 a 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 equate 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 associated posts found</li> '; } $retval .= '</ul> '; return $retval; } return; } add_shortcode('related_posts', 'related_posts_shortcode'); |
We can uncover a associated posts by using:
[related_posts]
Credit goes to Blue Anvil for this shortcode.
7 – Drop Caps With a Shortcode
Drop caps have been flattering as well as have your articles demeanour unequivocally nice. They can unequivocally supplement a tiny good item to post as well as have your site mount out on top of a rest.
1 2 3 4 5 6 |
function dropcap($atts, $content = null) { return ' <div class="dropcap">'.$content.'</div> ;'; } add_shortcode('dropcap', 'dropcap'); |
The CSS:
1 2 3 4 5 6 7 |
.dropcap { display:block; float:left; font-size:50px; line-height:40px; margin:0 5px 0 0; } |
Use it with:
[dropcap]M[/dropcap]auris ut lectus erat. In ...
Credit for this shortcode goes to TutToaster.
8 – Custom Post Type Query
Let’s contend which we have set up a law post sort called News as well as we instruct to arrangement all posts inside which law post sort on a page called Articles. You could do it with a formula below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function news_shortcode() { //The Query query_posts('post_type=news'); //The Loop if ( have_posts() ) : while ( have_posts() ) : the_post(); echo '<h3><a href="'; echo the_permalink(); echo '">'; echo the_title(); echo '</a></h3>'; echo the_excerpt(); endwhile; else: endif; //Reset Query wp_reset_query(); } add_shortcode('news', 'news_shortcode'); |
Then arrangement a headlines posts on your Articles page by regulating this shortcode:
[news]
9 – Display Posts from Category on Page
Very identical to a shortcode e.g. above, we can additionally arrangement singular series of posts from inside of a sold difficulty regulating a shortcode identical to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function category_shortcode( $atts ) { extract(shortcode_atts(array( 'limit' => '5', 'category' => '', ), $atts)); //The Query query_posts('category=' . $id . 'posts_per_page=' . $limit); //The Loop if ( have_posts() ) : while ( have_posts() ) : the_post(); echo '<h3><a href="'; echo the_permalink(); echo '">'; echo the_title(); echo '</a></h3>'; echo the_excerpt(); endwhile; else: endif; //Reset Query wp_reset_query(); } add_shortcode('category', 'category_shortcode'); |
You can list a posts using:
[category id=# limit=5]
Just reinstate # with a ID of a difficulty we instruct to display.
10 – Great Examples of Other Possible Shortcodes
I’ve shown we a couple of examples, both of my own as well as other’s creation, right away let me give we even some-more examples of overwhelming things we could do.
Joen Asmussen of NoScope.com combined a unequivocally nifty tiny shortcode to have a Google PDF Viewer.
Jean Baptiste, of WP-Recipes combined a shortcode to arrangement a “ReTweet” or “TweetMeme” buttons in your posts. Find it here.
Justin Tadlock done a good plugin which turns all (or most) WordPress template tags in to shortcodes so which they can be used inside of a post editor. Download a Plugin.
A couple of weeks ago we wrote a shortcode plugin of my own (sorry, couldn’t leave myself out!). It’s a shortcode application plugin which allows we to insert a accumulation of summary boxes as well as web buttons around shortcodes (similar to a tiny of a examples I’ve used above). Check out WP Utility Shortcodes.
11 – Some More Ideas
- Use shortcodes for displaying hit forms
- use shortcodes for displaying a Google map
- Shortcodes to arrangement writer bios
- A shortcode which outputs all of your amicable network info
- Shortcodes for picture galleries
- Shortcodes for jQuery picture sliders
Have we seen any alternative engaging uses of shortcodes? Do we have have have have have have have have make use of of of of of of of of of them in your site already?
See a strange post:
Working With WordPress Shortcodes








.gif)
.gif)