What are WordPress Shortcodes?

Among the many things that WordPress has among its features I found a reference to something called “shortcodes”. For those of you who don’t already know it, in this post I will try to answer the question “What are WordPress Shortcodes?”

The short version is that WordPress shortcodes are special markers that you can input inside the text of your posts and WordPress will interpret and expand them into HTML content. So, shortcodes work like a macro.

Let’s take an example. Many times when writing posts you would like to have Google Ads inside the content of the post. The code of the ad will always be the same, but you have to go to Google everytime, grab the snippet code and paste it in your post. This can be a bit of a hassle and also, because the code for Google Ads is a JavaScript snippet, chances are that the WordPress posts editor will just remove that upon save. This happens because WordPress has a mechanism of cleaning up certain tags, like the <script> tag, for security purposes (in case there are multiple users putting content in the system, you never know what kind of script a user might put in).

So, the solution that fits perfect in this case is a shortcode for Google Ads, which can look like this:


This piece of text you will be able to place anywhere inside your post and WordPress will render it as a Google Ad. Of course, your WordPress needs to be instructed how to render these shortcodes.

For a very simple shortcode like the [[googleAd]] above all you have to do is go into your WordPress admin panel, go into Appearance > Editor and locate on the right hand side a link that says “Theme Functions (functions.php)”. Click on that and you will go into editing mode of the “functions.php” file of your theme.

Be careful, as this file contains functions specific for the current theme of your blog, and if you damage any piece of it, your blog might not work properly anymore. However, if you feel pretty confident that you know what you are doing, at the end of this file, just above the ?> you have to add the following lines to make the shortcode work:

//[[googleAd]] shortcode registration function
function googleAd_func( $atts ){
	return ob_get_clean();
add_shortcode( 'googleAd', 'googleAd_func' );

That’s all! If everything is done correctly, you should be able to use the shortcode inside your posts, and it will be rendered as a Google Ad, something like this:


Please note that the shortcode will not work anymore if you switch themes, so you will have to edit the functions.php again.

Hope you found this post useful and please don’t hesitate to ask questions in the comments area.

John Negoita

View posts by John Negoita
I'm a Java programmer, been into programming since 1999 and having tons of fun with it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top