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:

[adsense]

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.

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 Gadbois is the co-owner and technical lead at Domain Superstar, 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.

Comments

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

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.

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.

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:

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

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.

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.

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.

Get 95% off for your first month with Aweber. Start your first month with just $1. View this Deal »

About WPBeginner

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).