Do you want to add shortcodes in WordPress but don’t know how to get started?
Shortcodes are an easy way to add dynamic content to your WordPress posts, pages, and sidebars. Many WordPress plugins and themes use shortcodes to add specialized content like contact forms, image galleries, sliders, and more.
In this article, we will show you how to easily add a shortcode in WordPress. We will also show you how to create your own custom shortcodes in WordPress.
What Are Shortcodes?
Shortcodes in WordPress are code shortcuts that help you add dynamic content to WordPress posts, pages, and sidebar widgets. They are displayed inside square brackets like this:
[myshortcode]
To better understand shortcodes, let’s take a look at the background of why they were added in the first place.
WordPress filters all content to make sure that no one uses posts and page content to insert malicious code into the database. This means that you can write basic HTML in your posts, but you cannot write PHP code.
But what if you wanted to run some custom code inside your posts to display related posts, banner ads, contact forms, galleries, or something else?
This is where the Shortcode API comes in.
Basically, it allows developers to add their code inside a function and then register that function with WordPress as a shortcode, so users can easily use it without having any coding knowledge.
When WordPress finds the shortcode, it will automatically run the code associated with it.
Let’s see how to easily add shortcodes to your WordPress posts and pages.
Adding a Shortcode in WordPress Posts and Pages
First, you need to edit the post and page where you want to add the shortcode.
After that, you need to click on the add block button ‘+’ to insert a Shortcode block.
After adding the Shortcode block, you can simply enter your shortcode in the block settings.
The shortcode will be provided by various WordPress plugins that you might be using, such as WPForms for contact forms or OptinMonster for email marketing.
To learn more about using blocks, see our Gutenberg tutorial for more details.
You can now save your post or page and preview your changes to see the shortcode in action.
Adding a Shortcode in WordPress Sidebar Widgets
You can also use shortcodes in WordPress sidebar widgets.
Simply visit the Appearance » Widgets page and add a ‘Shortcode’ widget block to a sidebar.
Now, you can paste your shortcode inside the text area of the widget.
Don’t forget to click on the ‘Update’ button to store your widget settings.
After that, you can visit your WordPress website to see the live preview of the shortcode in the sidebar widget.
Adding a Shortcode in Old WordPress Classic Editor
If you are still using the old classic editor in WordPress, then here is how you can add shortcodes to your WordPress posts and pages.
Simply edit the post and page where you want to add the shortcode. You can paste the shortcode anywhere inside the content editor where you want it to be displayed. Just make sure the shortcode is in its own line.
Don’t forget to save your changes. After that, you can preview your post and page to see the shortcode in action.
How to Add a Shortcode in WordPress Theme Files
Shortcodes are meant to be used inside WordPress posts, pages, and widgets. However, sometimes, you may want to use a shortcode inside a WordPress theme file.
WordPress makes it easy to do that, but you will need to edit your WordPress theme files. If you haven’t done this before, then see our guide on how to copy and paste code in WordPress.
Basically, you can add a shortcode to any WordPress theme template by simply adding the following code:
<?php echo do_shortcode("[your_shortcode]"); ?>
WordPress will now look for the shortcode and display its output in your theme template.
How to Create Your Own Custom Shortcode in WordPress
Shortcodes can be really useful when you want to add dynamic content or custom code inside the WordPress post and pages. However, if you want to create a custom shortcode, then it requires some coding experience.
If you are comfortable with writing PHP code, then here is a sample code that you can use as a template:
// function that runs when shortcode is called
function wpb_demo_shortcode() {
// Things that you want to do.
$message = 'Hello world!';
// Output needs to be return
return $message;
}
// register shortcode
add_shortcode('greeting', 'wpb_demo_shortcode');
In this code, we first created a function that runs some code and returns the output. After that, we created a new shortcode called ‘greeting’ and told WordPress to run the function we created.
You can add this code to your theme’s functions.php file manually or use a code snippet plugin like WPCode.
For more details, you can see our guide to adding custom code in WordPress without breaking anything.
Once you have done that, you can add this shortcode to your posts, pages, and widgets using the following code:
[greeting]
It will run the function you created and show the desired output.
Now, let’s take a look at a more practical usage of a shortcode.
In this example, we will display a Google AdSense banner inside a shortcode:
// The shortcode function
function wpb_demo_shortcode_2() {
// Advertisement code pasted inside a variable
$string .= '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
style="display:block; text-align:center;"
data-ad-format="fluid"
data-ad-layout="in-article"
data-ad-client="ca-pub-0123456789101112"
data-ad-slot="9876543210"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>';
// Ad code returned
return $string;
}
// Register shortcode
add_shortcode('my_ad_code', 'wpb_demo_shortcode_2');
Don’t forget to replace the ad code with your own advertisement code.
You can now use the [my_ad_code] shortcode inside your WordPress posts, pages, and sidebar widgets. WordPress will automatically run the function associated with the shortcode and display the advertisement code.
Shortcodes vs. Gutenberg Blocks
We are often asked by users about the differences between shortcodes vs. Gutenberg blocks.
Basically, blocks allow you to do the same thing as shortcodes but in a more user-friendly way.
Instead of requiring users to add a shortcode for displaying dynamic content, blocks allow users to add dynamic content inside posts/pages with a more intuitive user interface. A lot of popular WordPress plugins have switched to using blocks instead of shortcodes because they are more beginner-friendly.
We have put together a list of the most useful Gutenberg block plugins for WordPress that you may want to try.
If you want to create your own custom Gutenberg blocks, you can follow our step-by-step tutorial on how to create custom Gutenberg blocks in WordPress.
We hope this article helped you learn how to add a shortcode in WordPress. You may also want to see our guide on how to create a custom WordPress theme without writing any code and our expert pick of the best drag-and-drop WordPress page builder plugins.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Syed Balkhi says
Hey WPBeginner readers,
Did you know you can win exciting prizes by commenting on WPBeginner?
Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
You can get more details about the contest from here.
Start sharing your thoughts below to stand a chance to win!
Mohammed says
In Example2 How did you define this shortcode ‘my_ad_code’ thought you didn’t define it as a function?
WPBeginner Support says
That is added with the code add_shortcode at the bottom of the example
Admin
Mohammed says
❤️❤️
Appreciate for the best Article
Josh says
One picture says “greatings” instead of “greetings”
WPBeginner Support says
Thank you for pointing that out, it works as a good reminder to make sure you spell your shortcodes correctly
Admin
Maya says
Thanks for sharing such a detailed article keep up the good work!
WPBeginner Support says
You’re welcome, glad you found our guide helpful!
Admin
abuzar says
your guidence is very easy to learn. thanks you
WPBeginner Support says
You’re welcome!
Admin
Hugh says
Thanks for the great article. Works like a charm. However, although it is explained on the page link provided in the article, it may have been helpful to many readers if you had reiterated that to create shortcodes yourself (‘How to Create Your Own Custom Shortcode’ section of the article), you simply add the example code provided or your own code to the theme’s (or child theme’s) ‘functions.php’ file. Thanks again!
WPBeginner Support says
Thank you for that feedback!
Admin
Hafed benchellali says
Thank you for this great article!
WPBeginner Support says
You’re welcome!
Admin
Susan Benfatto says
Thank you for your tutorial, it was very useful
WPBeginner Support says
Glad it was helpful
Admin
Rohmah Azim says
Hi
Can you tell me how I can add shortcode to my header?
WPBeginner Support says
You would need to use the method for the theme files from this article and add it to your theme’s header file.
Admin
David says
-Hi, you know if its possible to add a box fixed for example with a shortcode inside? TNX
WPBeginner Support says
That would depend on the plugin you are using for the shortcode.
Admin
Tihomir says
Hi All
in one of my pages I am using this shortcode [product_category category=’jeans’ number=’1000′ per_page=’1000′] . It shows 1000 products of the chosen category jeans. What I can add to this code to make this 1000 products appear same like on product category page or by with a load more button?
Jose Rodriguez says
English translation –> but something happens when I add the shortcode and then some text, it turns out that this text is inside the html of the shortcode in the frontend, how can I avoid that?
loganathan says
I am creating an own theme, the problem is that theme does no support shortcode. can anyone help me to enable shortcode functionality for my theme.
Ihsan says
thanks for sharing such a nice article..
i want to know how we can use php and html both in short code… waiting for good response thanks…
Melissa Lowe says
I installed a Mailchimp for WordPress shortcode ..pasted it at the end of the text version of my document/article & no form was created & this is what it said…..MailChimp for WordPress error: There is no form with ID 0, perhaps it was deleted?
Ashkan says
What for $atts in that function ?
Helena says
Hi,
I have a widget by the homepage sidebar from a multisite installation and want to have 1 widget (only one) from this sidebar working at the subsites.
It is a text widget with a picture, this picture links to an online ebook.
Do you think I can use this to have it done?
Many thanks!
Vinayak Ganiger says
The wp form shortcode is not working in pages but it works fine in posts. My website is vinstechs.com. In my website page named Projects is their, in that the shortcode is not working.
What’s the solution for this, please reply with solution.
Meryl says
I have a slightly more complex question.
I’m using a theme with a theme options panel.
I want to use a shortcode inside one of those options.
However the output is just the [shortcode] text. Is there specific syntax I can use or does it differ from theme to theme? It’s taking forever to get answers from the theme dev.
Mayank Jain says
Perfect beginner explanation!
Vikash Ahlawat says
Very helpful post to create our own plugin.
hirak das says
Hi i am having a problem with [login_widget] shortcode.when i inserted this code to any page/post on wordpress i only get the text but not the login form…i am using login widget with shortcode plugin…please help…
Armen says
This doesn’t work if you’re trying to place another shortcode within the shortcode you’re creating.
Say I had a shortcode like [jpshare] and I wanted to place it along with other stuff into another shortcode function like in this article to add to the posts, how would you do that?
Mike says
No mention in the article WHERE to add the shortcode. Back to Google for a better tutorial.
Drew says
It clearly stated the code goes into “functions.php”. If you’re not sure how to use the shortcode, it goes anything you add text.
Ihatewordpress says
Hello,
As you can tell from my name I’m very frustrated right now with wordpress.
>>This last part is simple, just add the [adsense] shortcode in the HTML or Visual views of the Post or Page content editor.<<
Not even a tiny clue how to do that – would you be willing to expand on this? Thank you
WPBeginner Support says
WordPress uses shortcodes to add functionality into posts and pages. Like in this tutorial if you wanted to display advertisement code, then you will create a shortcode for it. Once you have created a shortcode you can use it in your posts and pages. Create a new post or edit an existing one and add your shortcode with square brackets in the post content area, like [adsense].
Admin
Brock says
I installed Popup manager plugin for newsletter subscribers. I got the popup to appear by following your suggestion of putting the shortcode in a comment on my main page. This works but only if a person expands my first comment to read the entire thing. Where/how do I paste the shortcode to get the popup to appear when someone visits my page without requiring them to do anything else? I’m trying to find something in the template customization options but nothing is standing out.
Patty says
Please help me understand if this is using a shortcode plugin properly. I need to display the date of the last update to a post or page to stop Google structured errors. I am using default 2014 only with plugins. I found a shortcode plugin named “Last Updated Shortcode” that seems to do what I need. I am wondering if this is what I do.
I would install the plugin, and then activate the plugin. Then on the page/post I would Edit page/post, and at the bottom of the post (or anywhere else if I prefer) add the shortcode. So I would literally type [lastupdated] The last updated date should then appear, correct???
And hopefully, Google will be happy and stop emailing notices of structured errors.
WPBeginner Support says
Yes. We are not sure about how it will affect your SEO though. You may want to take a look at how to display the last updated date of your posts in WordPress.
Admin
Larry Leek says
I’m guessing using the short codes for things like countdown timers is is only for wordpress.org or self hosted wp users correct?
WPBeginner Support says
Yes.
Admin
Anibal says
Hi I’m having trouble with the short code [tpe mylang lang=’es, en’] I know WP has recently changed this shortcode replacing the old [tp mylang=”yes” lang=”es, en” ] for the new one, but I’ve done the replacement and I still get to a 404 Nothing found error page, whenever I click on a link with this code. Any Ideas?
WPBeginner Support says
You are probably using Transposh plugin. Please contact the plugin support, they would be able to help you better.
Admin
Akshat Verma says
can you recommend any good shortcode plugin for wordpress 4.2
Steve says
I am having trouble figuring out how to add code comments to a page with just shortcode in it. I’ve tried HTML & PHP comment tags and when added the shortcode doesn’t work anymore. The reason I would like to comment the code is that I would like to document information about the shortcode in the page I call it from ( I may not be the site admin forever).
WPBeginner Support says
WordPress will strip out PHP or HTML comments in post editor. If you were using Edit Flow, then you could leave a note below the post. Right now you can switch to Text editor and leave HTML comment, but as soon as some user views the post in visual editor it will be stripped out again.
Admin
Steve says
It strips out the comments AND puts in a blank line for each comment. I put the comments below the shortcode and that works fine.
Thanks for helping me understand what was going on.
Michael says
When I add shortcode to posts, whether it be in the middle of an article or at the end, the actual element always appears at the top of the post, any idea why?
Malte says
Same here! How to fix that?!
Malte says
Okay… Use return instead of echo and everything is k!
James Newman says
@MALTE – I have the same issue. Can you clarify which file needs to be edited? I looked at the .php files for the plugin and could not find any echo statements to replace.
Thanks!
WPBeginner Staff says
Simply copy [rx_aeolus_three_cols] and paste it in a WordPress page. Save your page and then preview it. However, you will first have to create your portfolio in the plugin. Otherwise you will not see anything, because there is nothing for the plugin to display.
Usha says
I have downloaded a plug-in ‘Aeolus portfolio’ for the WordPress 3.9.2 running Twenty Eleven theme. I have been given the following shortcodes in the plug-in setting:
[rx_aeolus_three_cols]-Displays all portfolio as three columns.
[rx_aeolus_two_cols]-Displays all portfolio as two columns.
[rx_aeolus_one_col]-Displays all portfolio as one column.
Can anyone help as to how these short code can be incorporated in a page. I am new to WP plugins and badly need help.
Thanks
Usha
Sweetbreeze Tornado says
What if you want to add a button with shortcode?
Dan says
Thanks! This thread helped me a lot.
Just in case someone else lands in here and need to use a more complex HTML/PHP code structure inside a shortcode, this is an example using some aditional options from the WordPress Shortcodes API documentation:
Place this (and modify as needed) inside your functions.php:
http://pastie.org/9454401
Then you just use this shortcode inside your WordPress content
[mycustomshortcode]
Greetings!
Rob says
You’re the man! This is exactly what I needed. Thank you.
WPBeginner Staff says
You can add this code in your theme’s functions.php file.
Aymen Benzarti says
HI
I’m beginner and this is my first time with wordpress
so the first question when i tried this tutorial is in which file i have to write this code ?
Nidesh says
Hi,
Could you please tell me whether i have to put short codes to a particular page if i want any particular plugin to be displayed in that particular page.
E.g. I’ve installed Collabpress plugin. And i don’t see it’s UI in the front end of my site. So do i have to insert any shortcode to a page for it to be displayed?
WPBeginner Support says
Nidesh, yes you will be using shortcodes to do that. Please check out our guide on using Collabpress
Admin
Christina says
I would like to add the subscribe shortcode to my side menu. How do I do this?
Amber T says
I am just a little confused… where am I embedding this shortcode.
I have a columns plugin that provided the shortcode with a click of a buton, not sure if I have to take the code and embed it in my html.
please help.
thanks in advance!
WPBeginner Support says
just put the shortcode in your post content. Click on the preview button to see how it looks before publishing your post.
Admin
Carol Manser says
Really? Easy? Simple? This website is great but it says it is for beginners. I’ve been using WordPress for 2 years and yet I did not understand it at all. It’s quite annoying the way geeks always bang on about how easy something is and then write something totally incomprehensible to ordinary humans – just like every Math teacher I ever had.
OK, write an article that can only be understood by geeks – fine, but please don’t keep telling me how easy it is
output? string? content editor – where is it? define a function? I wish you would because I don’t know what it is.
“This function is pretty straightforward – it just returns my Google Adsense code as a string” –
a string of what? etc etc. Just saying….
WPBeginner Support says
We totally agree with you that sometimes when writing we may feel that we have explained everything clearly for beginners, but a beginner level user may still not be able to understand it. This is why we have comments and we will be happy to explain it to you more
A shortcode is a short+code. WordPress allows you to generate shortcodes for your programing code blocks called
functions
. Since a user can not write programming code in the Post Editor when writing a new post. The shortcodes provide users a way to add those programming codes into a post without writing the full code in post editor.When WordPress finds a shortcode in a post like this:
[my_shortcode]
It then executes the programming code associated with that particular shortcode.
The example given in the article above, creates a function get_adsense. When this function is executed it outputs the adsense code. We hope this explains it for you. Let us know if you have any more questions.
Admin
Stacy says
Or the instructions are 3 lines and your wondering where are the rest of the instructions????
Przemek says
Totally, agree. I struggled to understand most of it, too.
Alan Murray says
Can I use a shortcode on a Standalone HTML page to link to a page on my blog?
If so how do I embed it? Does the file need to be a PHP file?
I can’t seem to find anywhere on the net providing information on using sortcode outside WordPress pages or posts.
WPBeginner Support says
Alan, shortcodes are a WordPress feature the page should be a WordPress page in order to execute the shortcode.
Admin
DavidA says
Do you know, how to add a shortcode on a website from an other website WordPress? (multisite)
Thx
WPBeginner Support says
yes by creating a network wide plugin all blogs on the multisite can share the shortcode.
Admin
Lucy says
Very good one!
John Rose says
Clear and concise. Thank you so much. – John
Luke Ward says
Hello, thank you for this, it’s helped a lot.
However I would like to know how to add more than one adsense in as a shortcode.
eg [adsense1] – displays one advert [adsense2] displays a different advert.
Thanks!
Editorial Staff says
Follow the same process. Change the word adsense with adsense1.
Admin
Luke Ward says
Thank you, sorry for the noob question!
Also I have another problem now, I’m using this code to add adsense which works with responsive designs using javascript. You can see the code I’m using in the link below…
http://www.labnol.org/?p=25252
When this is added in after reuturn’ – nothing is displayed.
Hope you can help with this problem as I cannot seem to find out how anywhere.
Thanks
Editorial Staff says
The issue is with the syntax.
Paul says
Just came across this as I’m looking to add my own shortcodes, I can understand the function part of it, but which document do you include the “hook up”
“add_shortcode(‘adsense’, ‘get_adsense’); line”
that would be helpful to me
Thanks
Editorial Staff says
You add that right after the function part.
Admin
Patrick says
I’m clueless and am trying to create a short code to insert the blog’s owner’s user name. Any ideas?
Editorial Staff says
Write a function that outputs the username. Then put that function in the shortcode output. Unfortunately, we do not write custom codes like those on requests.
Admin
The Dividend Ninja says
You forgot to put the semicolon after the ‘ on line 13, which would cause an error, and show people how to call the function. So it would look like this:
</script>
‘;
}
add_shortcode(‘adsense’, ‘get_adsense’);
But nice job explaining Cheers!
Editorial Staff says
Thanks for letting us know. Its fixed now
Admin
wt says
So how to add a box with a CSS style using shortcode?Where should I put CSS content in?
ayushwhizkid says
I have installed a plugin that uses shortcodes to add styled boxes. I want to add the box to my posts by putting in the single.php. How to insert shortcode content to php file.
p.s. want to use the box for author info!