How to Create Your Own WordPress Shortcode (Easy Tutorial)

Most anyone who has used WordPress is familiar with shortcodes. WordPress shortcodes are little snippets you place inside your content, or even a widget, to do something. They look like this [shortcode] and what the code will do depends on what it’s for. A lot of plugins, and sometimes themes, will give you some that you can use as needed.

So, today I wanted to show you how you can create your own WordPress shortcode. It’s very easy to do, and useful to know if you have something you need to do repeatedly.

Creating a WordPress Shortcode

The first thing you will need to do is register the shortcode you plan to use with WordPress. You can do this by placing the following code in your theme’s functions.php file.

As always, I suggest creating a child theme if you don’t have one already.

add_shortcode('my_code','run_my_code');

There’s two parts to this. The first argument is the shortcode you want used (what you’ll type) to perform the action you need. In the example above, my_code is what I entered, so to use this I would type [my_code].

The second argument is the function that will execute to do whatever that shortcode needs done. In this example it will be a function called run_my_code.

Shortcode Function

Now, we need to create the function that will run when the shortcode is found. As mentioned above, I referenced a function called run_my_code, so that’s what I need to create.

function run_my_code($atts = false, $content = null) {
	return 'Look at me run!';
}

That doesn’t look very special and it’s not. In the example the only thing this will do is return the text Look at me run! That’s fine for demonstration, as I’m only aiming to show you how to setup the code.

Do note that the function uses return and not echo. If you use echo it will work, but you will get unexpected results. Instead, use return to place what you want where you want it.

Function Arguments

You will notice that the function I created run_my_code accepts two arguments. The $atts argument is the shortcode attributes. This would be if you need to pass in variables to your function.

$atts

Let’s say you need to pass in some unique ID. You could create your shortcode to look like [my_code id=”1″]. In turn, the function might look like this:

function run_my_code($atts = false, $content = null) {
	if(isset($atts['id']) && $atts['id'] == 1) return 'We are the best!';
	else return 'Look at me run!';
}

The function will see if the id attribute is set and return one result (We are the best!), or the other result if it’s not set, or doesn’t have a value of 1 (Look at me run!).

Obviously you can use the attribute for any number of things. That’s just one simple example.

$content

The content argument is the content of the shortcode. So, if your shortcode was written like this [my_code]Here’s some content[/my_code] then the $content argument would contain Here’s some content.

So, we could do something like this:

function run_my_code($atts = false, $content = null) {
	if(isset($atts['id']) && $atts['id'] == 1) return 'We are the best! ' . $content;
	else return 'Look at me run!' . $content;
}

Now, the function will append the $content to what it returns. So, if the shortcode were [my_code id=”1″]Rock on![/my_code] then it would return We are the best! Rock on!

Make sense?

Putting it All Together

If I were to put this all together then the code would look like this:

add_shortcode('my_code','run_my_code');

function run_my_code($atts = false, $content = null) {
	if(isset($atts['id']) && $atts['id'] == 1) return 'We are the best! ' . $content;
	else return 'Look at me run!' . $content;
}

As a Plugin

You could of course make this a plugin as well. This would be very useful if it’s something you plan to use site to site, or regardless of the theme you’re using.

To make this a plugin there’s just a few more steps involved. If you’ve never created a plugin before, it’s really easy.

First, create a folder to put your PHP file into. Let’s make a folder called my-shortcode.

Inside that folder you will want to create a PHP file, let’s say my-shortcode.php.

Inside that PHP file, add the following code:

<?php

/*
	Plugin Name: My Shortcode
	Plugin URI: http://blogplayground.com/
	Description: Runs a shortcode I made.
	Author: Thor
	Version: 1.0.0
	Author URI: http://blogplayground.com/
*/

add_shortcode('my_code','run_my_code');

function run_my_code($atts = false, $content = null) {
	if(isset($atts['id']) && $atts['id'] == 1) return 'We are the best! ' . $content;
	else return 'Look at me run!' . $content;
}

?>

Once you’ve got that completed, you need to upload the plugin. There are two ways to do that.

    1. FTP into your web host and upload the folder with the file to wp-content/plugins/
    1. Zip the folder with file, then go to Plugins->Add New->Upload Plugin.

If you do the first option, you’ll need to go to Plugins to activate it. If you do the second option, you will get the option to activate it once uploaded.

In Closing

There is a lot that you can do with shortcodes. Really, the sky is the limit with them and they are super useful. My example was really simple just to show you how easy it is to setup your own WordPress shortcode to use. However, if you would like a bit more depth then definitely check out the WordPress API on shortcodes.

Please Rate this Article

Please take a second to rate this. It helps us deliver to you the stuff you enjoy.

How to Create Your Own WordPress Shortcode (Easy Tutorial)
5 (100%) 1 vote

Author: Thor

I started blogging in 2009 for my wargaming blog. I've been blogging ever since. I even created a niche site about miniature storage of all things.

I have created a free 40K roster builder. I've also set up, maintain, and run my wife's site where she sells the Makeup Eraser.

I'm also a PHP programmer for a local web hosting company where I develop WordPress plugins.

Leave a Reply!

Note: You can comment as a guest by clicking the field indicated Name and checking off “