Mastering Wordpress Shortcodes: Jean-Baptiste Jung
Mastering Wordpress Shortcodes: Jean-Baptiste Jung
WordPress
118 Comments
Advertisement
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 1/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
WordPress shortcodes can do this and more and will definitely make your
blogging life easier. In this article, we’ll show you how to create and use
shortcodes, as well as provide killer ready-to-use WordPress shortcodes
that will enhance your blogging experience.
Using shortcodes is very easy. To use one, create a new post (or edit an
existing one), switch the editor to HTML mode and type a shortcode in
brackets, such as:
[showcase]
[showcase id="5"]
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 2/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
1. Open the functions.php file in your theme. If the file doesn’t exists,
create it.
2. First, we have to create a function to return the “Hello World” string.
Paste this in your functions.php file:
function hello() {
return 'Hello, World!';
}
add_shortcode('hw', 'hello');
The first parameter is the shortcode name, and the second is the
function to be called.
4. Now that the shortcode is created, we can use it in blog posts and on
pages. To use it, simply switch the editor to HTML mode and type the
following:
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 3/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
[hw]
add_shortcode("url", "myUrl");
3. The shortcode is now created. You can use it on your posts and
pages:
When you save a post, the shortcode will display a link titled
“WordPress recipes” and pointing to http://www.wprecipes.com.
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 4/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
Now that we know how to create and use shortcodes, let’s look at some
killer ready-to-use shortcodes!
The problem. Seems that a lot of you enjoyed the “Send to Twitter” hack
from my latest article on Smashing Magazine. I also really enjoyed that hack,
but it has a drawback: if you paste the code to your single.php file, the
“Send to Twitter” link will be visible on every post, which you may not want.
It would be better to control this hack and be able to specify when to add it
to a post. The solution is simple: a shortcode!
The solution. This shortcode is simple to create. Basically, we just get the
code from the “Send to Twitter” hack and turn it into a PHP function. Paste
the following code in the functions.php file in your theme:
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 5/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
function twitt() {
return '<div id="twitit"><a href="http://twitter.com/h
ome?status=Currently reading '.get_permalink($post->ID).
'" title="Click to send this page to Twitter!" target="_
blank">Share on Twitter</a></div>';
}
add_shortcode('twitter', 'twitt');
To use this shortcode, simply switch the editor to HTML mode and then
type:
[twitter]
and a “Send to Twitter” link will appear where you placed the shortcode.
S O U R CE AN D R E LATE D PLU G -I N S :
The problem. You already know that a very good way to gain RSS
subscribers is to display a nice-looking box that says something like
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 6/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
“Subscribe to the RSS feed.” But once again, we don’t really want to hard-
code something into our theme and lose control of the way it appears. In
this hack, we’ll create a “Subscribe to RSS” shortcode. Display it in some
places and not others, in posts or on pages, above or below the main
content, it’s all up to you.
function subscribeRss() {
return '<div class="rss-box"><a href="http://feeds.f
eedburner.com/wprecipes">Enjoyed this post? Subscribe to
my RSS feeds!</a></div>';
}
add_shortcode('subscribe', 'subscribeRss');
Styling the box. You probably noticed the rss-box class that was added to
the div element containing the link. This allows you to style the box the way
you like. Here’s an example of some CSS styles you can apply to your
“Subscribe to RSS” box. Simply paste it into the style.css file in your theme:
.rss-box{
background:#F2F8F2;
border:2px #D5E9D5 solid;
font-weight:bold;
padding:10px;
}
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 7/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
The problem. Most bloggers use Google AdSense. It is very easy to include
AdSense code in a theme file such as sidebar.php. But successful online
marketers know that people click more on ads that are embedded in the
content itself.
1. Open the functions.php file in your theme and paste the following
code. Don’t forget to modify the JavaScript code with your own
AdSense code!
function showads() {
return '<div id="adsense"><script type="text/j
avascript"><!--
google_ad_client = "pub-XXXXXXXXXXXXXX";
google_ad_slot = "4668915978";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/s
how_ads.js">
</script></div>';
}
add_shortcode('adsense', 'showads');
2. Once you have saved functions.php, you can use the following
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 8/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
[adsense]
Note that our AdSense code is wrapped with an adsense div element,
we can style it the way we want in our style.css file.
Code explanation. The above code is used simply to display AdSense ads.
When the shortcode is inserted in a post, it returns an AdSense ad. It is
pretty easy but also, you’ll agree, a real time-saver!
S O U R CE S :
The problem. Many readers also seemed to enjoy the “8 RSS Hacks for
WordPress” post published on Smashing Magazine recently. Now, let’s use
our knowledge of both RSS and shortcodes to embed an RSS reader right in
our posts and pages.
The solution. As usual, to apply this hack, simply paste the following code in
your theme’s function.php file.
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 9/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
function readRss($atts) {
extract(shortcode_atts(array(
"feed" => 'http://',
"num" => '1',
), $atts));
add_shortcode('rss', 'readRss');
The feed attribute is the feed URL to embed, and num is the number of
items to display.
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 10/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
To use it, simply paste the following in the WordPress editor, after switching
to HTML mode:
This will display a list of three posts from the category with an ID of 1. If you
don’t know how to get the ID of a specific category, an easy way is
explained here.
Code explanation. After it has extracted the arguments and created the
global variable $posts, the sc_liste() function uses the get_posts()
function with the numberposts, order, orderby and category parameters to
get the X most recent posts from category Y. Once done, posts are
embedded in an unordered HTML list and returned to you.
S O U R CE :
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 11/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
The solution. Open the functions.php file and paste the following code:
To use the shortcode, simply type the following in the editor, when in HTML
mode:
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 12/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
S O U R CE S :
The problem. Even if you enjoyed this article, you may have felt a bit
frustrated because, by default, WordPress doesn’t allow shortcode to be
inserted into sidebar widgets. Thankfully, here’s a little trick to enhance
WordPress functionality and allow shortcodes to be used in sidebar widgets.
The solution. One more piece of code to paste in your functions.php file:
add_filter('widget_text', 'do_shortcode');
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 13/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
S O U R CE S :
Shortcode API
The WordPress Codex page related to the shortcode API.
WordPress 2.5 shortcodes
Excellent shortcodes tutorial.
WordPress shortcode generator
The page is in French but provides a very useful and easy-to-use app
to create shortcodes online.
Using WordPress shortcode to create beautiful download boxes
Another great use of shortcodes: creating fancy “Download” boxes
for your blog.
Easy way to advertise in WordPress using shortcodes
Great resource to manage and insert advertising on your blog,
brought to you by WpEngineer.
Create a signature using WordPress shortcodes
Really simple but really cool: a shortcode to display a graphic
signature on your blog.
How to: Use WordPress shortcodes with attributes
Concise tutorial on using shortcode attributes.
(al)
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 14/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
Jean-Baptiste Jung
This guest post was written by Jean-Baptiste Jung, a 28-year-
old blogger from Belgium, who blogs about Web Development
on Cats Who Code, about WordPress at WpRecipes and about
blogging on Cats Who Blog . You can stay in touch with Jean
by following him on Twitter.
Related Articles
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 15/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
Japh
February 2nd, 2009 8:30 pm
Great post, thanks! I’m really only just starting to realise the power of
1
WordPress :)
0 Reply
DKumar M.
February 2nd, 2009 8:31 pm
Some Of them I knew Already Some of them are not… Thanks for the info
Jean!!
2
DKumar M.
0 Reply
Ejaz
February 2nd, 2009 8:32 pm
Aravind
February 2nd, 2009 9:37 pm
0 Reply
African Boy
February 2nd, 2009 9:47 pm
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 16/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
This is an example of articles we need, not just lists and lists. Not that the
lists are bad, but information is better.
5
Then again, we all have different requirements.
0 Reply
Don Campbell
February 2nd, 2009 9:59 pm
Wow this is a great tip that I didn’t know about – thanks!! (Dugg.) 6
+1 Reply
silentgirl
February 2nd, 2009 10:33 pm
Youri
February 2nd, 2009 10:35 pm
Agree with African Boy, less lists please and more articles like this. I
actually learned stuff I didn’t know!
8
+1 Reply
new
February 2nd, 2009 11:03 pm
Thank you!!! 9
+2 Reply
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 17/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
sandeep
February 2nd, 2009 11:06 pm
Yogie
February 2nd, 2009 11:35 pm
Great !!! 11
+1 Reply
Sarah
February 2nd, 2009 11:50 pm
0 Reply
Dany
February 2nd, 2009 11:57 pm
Marvin
February 2nd, 2009 11:58 pm
0 Reply
Wp Addict
February 3rd, 2009 12:00 am
Great tutorial. The one on inserting the adsense will definitely be of great
use to me.
15
0 Reply
Simon Day
February 3rd, 2009 12:59 am
tom hermans
February 3rd, 2009 2:10 am
OnWebDev
February 3rd, 2009 2:58 am
Wow, that’s great! Didn’t knew that feature existed. I really like the Embed
Adsense Code in posts shortcode. It sure saves a lot of time… Thanks for
18
sharing!
0 Reply
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 19/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
tom hermans
February 3rd, 2009 2:58 am
19
I think I found a minor error.
In the 5th tip, where a post is retrieved directly from the database, the
shortcode reads
0 Reply
Tam Mai
May 14th, 2012 11:21 pm
Alessandro
February 3rd, 2009 3:00 am
Manu
February 3rd, 2009 3:12 am
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 20/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
Manu
manublog.org
0 Reply
airwolf
February 3rd, 2009 3:22 am
Awesome ! 23
WordPress is very suitable for light-scale web development for those
people that not much in pure coding techniques like me >_<
0 Reply
b00m
February 3rd, 2009 3:37 am
JoSe
February 3rd, 2009 4:18 am
mathiz
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 21/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
0 Reply
Laura
February 3rd, 2009 5:33 am
Awesome post. Please give us more of these helpful tutorials that show us
more of what WordPress can do (and more of Jean-Baptiste’s genius!)
27
I agree with African Boy and Youri – list posts have nothing on posts as
useful and informational this one :)
0 Reply
unicatcher
February 3rd, 2009 6:35 am
0 Reply
Jorge Bordás
February 3rd, 2009 6:42 am
Dainis Graveris
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 22/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
wow, this post is extremely useful for everybody who uses WordPress,
30
thanks!! :)
0 Reply
Wow, we’ve been using WordPress for years and never knew the process
for doing this. Thanks for the great tip!
31
0 Reply
Cédric GIRARD
February 3rd, 2009 8:49 am
0 Reply
Ariyo
February 3rd, 2009 9:53 am
AWESOMENESS! bookmarked! 33
0 Reply
Jean-Baptiste Jung
February 3rd, 2009 10:05 am
I’m glad to see all theses nice comments, great to see you enjoyed the
article! Thanks for your support!
34
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 23/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
0 Reply
Mario Awad
February 3rd, 2009 11:39 am
I was lately thinking about how to easily add images with lighboxes to
posts… you just hit the jackpot… thanks for the amazing article :)
35
0 Reply
Jeff L
February 3rd, 2009 12:32 pm
Good stuff here – I just converted my site to use WordPress, and this
functionality seems to offer a great amount of flexibility. I wasn’t aware
36
that this existed at all, thanks!
0 Reply
Chris Robinson
February 3rd, 2009 12:56 pm
Martin Greenwood
February 3rd, 2009 3:10 pm
runner
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 24/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
good~!article
39
0 Reply
Leion
February 3rd, 2009 9:20 pm
Yang Yang
February 3rd, 2009 10:37 pm
This would come much more handy with large chunks of random snippet
insertions.
41
+1 Reply
kimee
February 3rd, 2009 11:43 pm
AWESOMENESS! 42
That’s I’ve been studying and trying to summarize.
Thanks for the tips. and hoping more informaiton for other shortcodes.
0 Reply
Charliend
February 4th, 2009 5:06 am
charlie-blog.com/media
0 Reply
Digiscott
February 4th, 2009 7:44 am
Nicholi
February 4th, 2009 8:51 am
This is a great article. I didn’t know that WordPress had such a thing built
in. This is definitely useful. Thanks for the tips!
45
0 Reply
Sid W
February 4th, 2009 1:05 pm
王韬
February 4th, 2009 11:02 pm
假如有中文就更完美了 47
0 Reply
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 26/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
Tinna
February 5th, 2009 2:27 am
Oh My ~楼上那个王韬~我好像认识你~
48
0 Reply
Burclar
February 5th, 2009 1:16 pm
very helpful article for webmaster who are using wordpress . Very Very
thank you.
49
+1 Reply
insic
February 6th, 2009 12:10 am
normalfx
February 7th, 2009 7:24 pm
so powerful tips. 51
0 Reply
E. Houston
February 8th, 2009 9:49 am
Great post. I like a lot of functionality but tons of plugins slow down
loading. This one tip replaces several plugins and gives me a great
52
direction to go in.
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 27/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
0 Reply
lyon
February 8th, 2009 4:53 pm
Cassiano
February 10th, 2009 6:34 pm
hi all 54
on “step” 6. why it takes the last image?! how can I do to take the first
image?!?!
BR,
Cassiano
0 Reply
Gary
February 11th, 2009 10:39 pm
Hi, 56
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 28/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
One question though. In the 3rd heading you’ve put the following in the
code box:
<script type=”text/javascript”
src=”http://media.smashingmagazine.com/images/mastering-wordpress-
shortcodes/http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script>
Is this an AdSense hack I’m not aware of or you just deliberately wanted to
mess up the AdSense code which is against the AdSense ToS?
-1 Reply
George Serradinho
February 12th, 2009 5:00 am
0 Reply
Emiliano Jordan
February 19th, 2009 7:47 am
Great article, thanks. Also does anyone know what plugin is being used to
display the code here? I’d really like to get that rolling.
58
0 Reply
chris
March 31st, 2009 3:20 pm
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 29/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
59
Thanks for a great article. I wrote a plugin called ‘MapPress’ that lets you
insert Google Maps (as shortcodes), directly from the WordPress editor.
0 Reply
jmezes
May 1st, 2009 2:20 pm
Great article but I was a bit frutrated with “Embed an RSS reader”. It really
dont works for me (WordPress 2.7.0).
60
For each try I made, it’s every time the same result in my page: “an error
has occured the feed is probably down, try again later”.
An idea ?
0 Reply
Luis Eduardo
May 6th, 2009 10:04 am
Great post, I’m really impressed about such things you can do with a bit of
code
61
0 Reply
+1 Reply
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 30/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
bitlimakina
June 22nd, 2009 1:21 pm
liberta cara
July 12th, 2009 10:54 pm
piouPiouM
July 17th, 2009 1:33 am
0 Reply
Keith Collantine
August 31st, 2009 9:45 am
There’s a problem with the “Get posts from WordPress Database with a
Shortcode” example.
66
It causes all comments added to the final post in the list to appear in the
article you added the list to.
0 Reply
segovia
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 31/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
I too noticed this, it’s definitely a bug because it seems to display the
67
comment box and any related comments for the last post shown. This
happens on all post types (posts, pages and CTP’s). I would also know
how to fix this.
0 Reply
Aakash Chakravarthy
September 2nd, 2009 5:07 am
Rafans Manado
October 12th, 2009 4:40 pm
Roozbeh
October 27th, 2009 9:48 pm
Robert@PNG
November 16th, 2009 4:08 am
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 32/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
0 Reply
TATEZO-
December 1st, 2009 4:26 am
Haitham Al Humsi
December 19th, 2009 6:21 am
for example if i create an adsense short code and embed it in my blog i get
3 adsense blocks… but if i use a short code defined in one of the plugins
i’m using it works great (only 1 instance of the results)…
0 Reply
Judd Dunagan
January 18th, 2010 4:25 am
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 33/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
Al
75
February 23rd, 2010 9:49 am
Good article!
Has any had a problem with Shortcode duplicating on pages? I’ve recently
used a few shortcodes for different wordpress plugins but when I look at
the page i create the code repeats it’s self and I wind up having the 3 of
the same thing on my page. How do I stop that from happening?
0 Reply
cameraadvice
March 3rd, 2010 7:29 am
there is a mistake in your post pulling shortcode. it should read list and not
liste.
76
i have used this code on my website
0 Reply
CSSReX
March 23rd, 2010 9:24 am
Bob
April 5th, 2010 12:52 pm
does not perform the replacement and the shortcode is visible on the
page.
0 Reply
Moochyschwag
April 25th, 2010 9:45 am
0 Reply
Xcellence IT
June 17th, 2010 9:49 pm
[div_halfcol] will create a and something that will close this div…
0 Reply
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 35/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
Deepesh Divakaran
June 18th, 2010 11:46 am
0 Reply
Char-Lou Benedict
August 12th, 2010 12:33 pm
Enjoyed the post – but when I tried the rss shortcode on my arras theme
with a yahoo.pipe, nothing shows up.
83
The pipe does show with the rss sidebar widget. Any suggestions or noted
conflicts?
0 Reply
Andreas Ostheimer
September 7th, 2010 7:58 am
Great stuff – please correct the typo in [liste num="3" cat="1"] – it should be
“list” not “liste”. Took me like 25-30ms to check this ;-)
84
Thanks, Andreas
0 Reply
Osu
September 12th, 2010 11:23 pm
Nice article – will definitely use these shortcodes. This is off-topic, but is
there a way to allow wp registered users to upload their own avatar
85
without using the Gravatar website? Is this website doing that? Thanks
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 36/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
0 Reply
sagive
September 18th, 2010 8:57 am
10x a lot :)
0 Reply
vee ray
September 30th, 2010 8:22 pm
Hi this is a great post. one thing I noticed is that in item# 5 the shortcode
has a typo which renders the example invalid.
87
it says:
it should say:
0 Reply
Mati
October 12th, 2010 8:56 am
great post! 88
I wonder how these shortcodes affect performance?
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 37/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
I guess that WP is scanning the post content for any inline shortcodes
before echoing it, how bad is that for the website’s performance?
0 Reply
Brett Widmann
October 23rd, 2010 2:24 pm
Niraj
November 1st, 2010 8:42 pm
http://i56.tinypic.com/2vctjlf.jpg
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 38/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
function mfields_taxonomy_terms_list( $c ) {
global $post;
$o = ”;
$terms = array();
$lists = array();
$custom_taxonomy_names = array();
$custom_taxonomies = mfields_get_custom_taxonomies();
$custom_taxonomy_names[] = $config->name;
if( is_single() )
return $c . $o;
return $c;
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 39/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
function mfields_get_custom_taxonomies( ) {
global $wp_taxonomies;
$custom_taxonomies = array();
$custom_taxonomies[$slug] = $config;
return $custom_taxonomies;
?>
0 Reply
Eric
November 6th, 2010 11:40 pm
Thanks! 91
0 Reply
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 40/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
Eric
November 6th, 2010 11:40 pm
92
Thanks!
+1 Reply
Chris
November 24th, 2010 1:40 am
These are all great but what if I want to display a shortcode in the visual
editor in a specific way instead of just having the shortcode text?
93
+1 Reply
vinod
December 27th, 2010 11:24 pm
Hey, 94
These are all great ,
Thanks Dear!
+1 Reply
Paulo
January 3rd, 2011 9:30 am
Wonderful! 95
The best article I’ve ever seen on this content!
Congratulations.
Very good!
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 41/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
+1 Reply
Rohan @techlunatic
January 10th, 2011 11:28 pm
hello i am using the elegant themes framework and i pasted the code in
the functions.php as you said but it has error messages. plz help!!
96
+1 Reply
Chris @cquinndesign
February 15th, 2011 1:33 pm
Thanks
+1 Reply
oem store
March 2nd, 2011 1:56 pm
I am totally delighte with strong you blog greatly that warned me God
bless yo
98
+1 Reply
dhanapal
March 11th, 2011 4:48 am
Hai friend, 99
Its very easy to understand and thanks for this post. keep updating our
knowledge through this types of posts.
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 42/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
+2 Reply
Kevin Leary
March 11th, 2011 12:08 pm
I’ve created a modified version of the RSS feed shortcode that uses
fetch_feed() instead of wp_rss(). It allows you to pull in a little more
100
information including the date posted and a description/excerpt and
provides some basic CSS to get you started also. I hope it can help
someone out there!
+1 Reply
ARS
April 2nd, 2011 2:26 pm
Ron Finnerty
May 5th, 2011 8:39 am
How could I either detect the other shortcode definition or create some
mechanism to avoid collissions completely.
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 43/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
MANY people will create shortcodes that are named the same. [hw], [big],
[name] ect seem to come to mind as examples.
+1 Reply
Donna Bushek
September 14th, 2011 7:10 pm
Splendid Angst
September 25th, 2011 4:21 pm
How hard would it be to create a short code that requires you to use a 2
part short code. Example: I would like to avoid the copy/paste method that
104
I’m using to create a 2 column table set. If I could do [side1]…[/side1] [side2]
…[.side2] and have them combine each other into 1 table that would be
awesome, but it would only work if they are both used.
http://inertubetv.splendidangst.com/archives/79
That’s the table layouts I have manually created in HTML in the post.
+1 Reply
Arvind Bhardwaj
September 26th, 2011 11:00 pm
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 44/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
prit
December 23rd, 2011 4:43 am
function sc_postimage($postID)
106
{
$args = array(
‘order’=> ‘ASC’,
);
//print_r($attachments);
if ($attachments) {
$i=0;
?>
?>
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 45/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
<a href="”>
$i++; ?>
?>
<?php
if($i==3)
echo "”;
$i=0;
echo “”;
add_shortcode(“postimage”, “sc_postimage”);
+1 Reply
Manuel S.
October 10th, 2011 10:37 am
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 46/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
107
About example 5), there is an important fix:
Otherwise you mess with the values (date, categories etc) of the main
post, they will be overriden by the posts from the shortcode. Depending
on your specific theme, this can have unwanted results, wrong values in
sidebars, footers, etc.
You probably want to go back to the main loop, and that’s exactly what
“wp_reset_query();” does.
+1 Reply
brian.s
November 10th, 2011 1:18 pm
Rad. I dislike WordPress slightly less after reading this. We (WP + me) have
a love/hate relationship.
108
+1 Reply
Prashant
December 21st, 2011 3:51 am
thanks a lot.. I was breaking my head from last few hours.. and here I found
the exact solution.
109
+1 Reply
vivek
February 6th, 2012 5:59 am
Amazing post. I love it. Thanks for sharing with us. I hope to see more 110
+1 Reply
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 47/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
Shawn
February 17th, 2012 10:23 pm
Thank you. What a nice explanation of Shortcodes and how they work. I 111
had no idea they were so simple to create and implement. You have
inspired me to experiment. Thanks!
0 Reply
Surendra
March 25th, 2012 11:23 am
0 Reply
Caps
July 15th, 2012 12:18 pm
Is there a shortcode for username that you can place on a page? This
would be for a Welcome page.
113
Caps
0 Reply
SID SUZUKI
July 26th, 2012 2:06 pm
extract(shortcode_atts(array(
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 48/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
), $atts));
query_posts( ‘page_id=’.$id);
$retour=”.get_the_title().”;
$retour.=’‘;
$retour.=’ ‘;
endwhile;
endif;
wp_reset_query();
return $retour;
add_shortcode(“page”, “pagedata”);
0 Reply
Steve
August 14th, 2012 5:46 am
The adsense shortcode worked like a charm…Thanks Great Article BTW! 115
0 Reply
max
August 28th, 2012 1:44 pm
Hi there, 116
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 49/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
Im wanting to create a photo blog theme, i want to use short codes for
inserting images into posts rather than the user just adding images straight
to the editor.
What I want to do also is wrap the image inside some custom html, this is
just some divs and spans.
Then the user can just keep adding this shortcode to the editor over and
over:
0 Reply
chazzer
January 1st, 2013 2:10 pm
I found this very difficult. I coded the “twitter” example and it worked well.
However when I removed the “twitter” code from the functions file, the
117
“twitter” example hung around like a bad smell. Where does the code
reside when this happens. The reverse also occurred. I coded the “Hello
world” example but made some sort of mistake. However even when I
corrected the mistake it could not be made to work. I then coded
remove_shortcode(‘hw’) and a corrected version of “Hello world”. It then
worked.
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 50/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
0 Reply
Kamal
January 13th, 2013 7:44 am
Leave a Comment
Yay! You've decided to leave a comment. That's fantastic! Please keep in mind that
comments are moderated and rel="nofollow" is in use. So, please do not use a
spammy keyword or a domain as your name, or it will be deleted. Let's have a
personal and meaningful conversation instead. Thanks for dropping by!
Your name *
Your email *
Your message *
Submit comment
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 51/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
Submit comment
↑ Back to top
BEST OF DESIGN
Responsive Design
Photoshop
Typography
Mobile Design
Usability and UX
Web Design Showcases
BEST OF CODING
Smashing Magazine
The Smashing eBook Library
Smashing Books
Smashing eBooks
Smashing Email Newsletter
Smashing Shop
Smashing Job Board
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 52/53
3/31/13 Mastering WordPress Shortcodes | Smashing WordPress
OTHER RESOURCES
wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ 53/53