Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Add A Shortcode in WordPress?

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Add A Shortcode in WordPress?

Have you ever had a time where you wanted to add some specialized content to your WordPress post or page, but weren’t sure how? Maybe you wanted to embed a Twitter widget or some content called back from some website or API. How can you easily add this type of content to your WordPress post? Fortunately, WordPress provides something called a shortcode to make this kind of task extremely easy. This tutorial will take you through the process of building, installing, and using a shortcode in your WordPress installation. Let’s start by seeing what a shortcode is.

What is a WordPress Shortcode?

Briefly, a shortcode is a special tag that you can enter into a post that gets replaced with different content when actually viewing the post on the website. If you have ever embedded a WordPress gallery on your blog, then you’ve already seen the built in short code .

When you load a blog page with the [gallery] shortcode, WordPress replaces the [gallery] shortcode with all of the code that actually displays a gallery of your images.

As you can see from the above example, a shortcode looks similar to an HTML tag, but is enclosed with square brackets instead of angle brackets. This code gets replaced with some other code when the page is actually loaded in a web browser. The really cool thing is that WordPress allows you to create your own custom shortcodes to display pretty much anything! You could use it to output a Youtube video, show your latest tweets, or even customize it however you like.

In case that doesn’t make sense, let’s look at an example. Let’s say I want to output an AdSense ad within my post. I could go into the HTML mode of the WordPress content editor and copy and paste the Adsense code block into it, but this would be tedious and potentially distracting with all the extra markup in my post. In addition, if I wanted to change the ad block, I would have to go back to each and every post to change it to the new one. An easier way and more reliable way to add the Adsense block wherever I wanted would be to use an adsense shortcode. The shortcode could look like this:


When actually looking at the post on your website, the shortcode would be replaced with the Adsense ad block. So how do you create this shortcode? Obviously, you need to tell WordPress what to replace the shortcode with somehow. Let’s look at that next.

How do I Create A Shortcode?

Fortunately, WordPress makes it pretty simple to make your own shortcodes, so let’s actually go about implementing the [adsense] shortcode. The first thing we need to do is define a function that outputs the actual Adsense code. All of the following code will go in functions.php in your theme (it could also go in a standalone plugin file). Got it? Ok, so let’s look at that function.

function get_adsense($atts) {
	return '<script type="text/javascript"><!--
google_ad_client = "pub-546321545321589";
/* 468x60, created 9/13/10 */
google_ad_slot = "54321565498";
google_ad_width = 468;
google_ad_height = 60;
<script type="text/javascript"

This function is pretty straightforward – it just returns my Google Adsense code as a string. Whatever this function returns is what my shortcode will be replaced with, so I could potentially have returned the html for a Twitter widget, or a list of the child posts of this one, or anything else.

Now that we have a function that returns what we want, how do we hook that up to a shortcode? Now this is where the WordPress API comes in. Again let’s look at how we do it and then explain what’s going on. Here’s the call to set up the adsense shortcode.

add_shortcode('adsense', 'get_adsense');

That’s it! The first parameter passed in is the name of the shortcode, so in our case, ‘adsense’ tells WordPress to create the [adsense] shortcode. The second parameter designates the function that will be called when the new shortcode is encountered. Again, in our case, ‘get_adsense’ tells WordPress to replace [adsense] with the results of our get_adsense method.

Not too bad is it? Now this is a very simple shortcode, WordPress allows you to do much more with your shortcodes, including adding parameters (maybe you want to choose between adsense blocks?). The full API can be seen at the WordPress Codex.

How do I Use my Shortcode?

This last part is simple, just add the [adsense] shortcode in the HTML or Visual views of the Post or Page content editor. That’s it! You’ve created your first shortcode.

John GadboisJohn Gadbois is the co-owner and technical lead at , a domain name tools site. He also runs CalculatorPro, a cool calculator sites with all sorts of financial calculators. He enjoys learning about and coding with Ruby on Rails, jQuery, WordPress, and PHP.

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. Vikash Ahlawat says:

    Very helpful post to create our own plugin.

  2. 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…

  3. 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?

  4. Mike says:

    No mention in the article WHERE to add the shortcode. Back to Google for a better tutorial.

  5. Ihatewordpress says:

    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].

  6. 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.

  7. Larry Leek says:

    I’m guessing using the short codes for things like countdown timers is is only for or self hosted wp users correct?

  8. 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.

  9. Akshat Verma says:

    can you recommend any good shortcode plugin for wordpress 4.2

  10. 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.

      • 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.

  11. 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?

  12. 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.

  13. 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.


  14. Sweetbreeze Tornado says:

    What if you want to add a button with shortcode?

  15. 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:

    Then you just use this shortcode inside your WordPress content :)



  16. WPBeginner Staff says:

    You can add this code in your theme’s functions.php file.

  17. Aymen Benzarti says:

    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 ?

  18. Nidesh says:


    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?

  19. Christina says:

    I would like to add the subscribe shortcode to my side menu. How do I do this?

  20. 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.

  21. 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:


      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.

    • Stacy says:

      Or the instructions are 3 lines and your wondering where are the rest of the instructions????

  22. 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.

  23. 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.

  24. Lucy says:

    Very good one!

  25. John Rose says:

    Clear and concise. Thank you so much. – John

  26. 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.


    • Editorial Staff says:

      Follow the same process. Change the word adsense with adsense1.

      • 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…

        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.

  27. 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


  28. 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.

  29. 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:
    add_shortcode(‘adsense’, ‘get_adsense’);
    But nice job explaining :) Cheers!

  30. wt says:

    So how to add a box with a CSS style using shortcode?Where should I put CSS content in?

  31. 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!

  32. KishaanSha says:

    I added a shortcode:

    add_shortcode(‘post_title’, ‘get_the_title’);

    which returns post title.. But that post title doen’t displays in rss feed. What may the problem? Where that shortcode [post_title] shoul appear, there is blank space only. Can anyone help me to fix it.. Thank you.

  33. BlogAid says:

    I can see a lot of custom uses for this. Thanks for the post. ~ MaAnna

  34. denisa says:

    how to make a short code for the archive page, whether this be?

  35. Crysislevel says:

    thanx for this…i added many shortcodes to my site with the help of plugins and implemented them in my single.php file!

  36. Groho says:

    I think it’s the very basic tutorial to add shortcode in wordpress

  37. graphicbeacon says:

    This is pretty basic. The links below will provide a detailed use of wordpress shortcodes.

    A flexible shortcode is one that will allow you to edit certain attributes. Couldn’t the google adsense code be tweaked to allow the user to set attributes such as the width/height and even ad client?

  38. Glenn says:

    wow nice, so I need to add my shortcode in my functions.php just in case I changed my wordpress theme?

    • Editorial Staff says:

      Yes, it is crucial that you update the functions to the next functions.php file if you change themes.

  39. Bill says:

    Straightforward enough, except where do I put the

    add_shortcode(‘adsense’, ‘get_adsense’);


    • Editorial Staff says:

      You put this right below the function that is mentioned in the article in your functions.php file.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.