In this article, we’re going to learn how to create Shortcode in WordPress.
A WordPress Shortcode is simply a small piece of PHP code that allows you to quickly add elements to your WordPress Website’s page and post.
In other words, shortcodes save you time when customizing your website so you wouldn’t need additional plugins for that.
WordPress provides 6 default shortcodes which are:
- [ audio ] – used to play audio files.
- [ playlist ] – display a collection of audio and video files.
- [ gallery ] – for showing image gallery.
- [ video ] – used to embed and play video files.
- [ embed ] – encloses embedded items.
- [ caption ] – wraps caption around your content.
Do note that there are no spaces in between the word and the square brackets ( [shortcode] ), I only added them so this article wouldn’t display my WordPress Files.Ebuka Val
But with some coding knowledge, you can also create your own custom WordPress Shortcode.
For example, if you use a Call to Action button such as ‘Subscribe’ on every post, having a dedicated Shortcode ready will help you publish your articles faster and more efficiently.
We will create a custom Shortcode to add Social Media links as an example but before we start, I do recommend Backing up your WordPress Website.
Create a New Theme File
The first step we’ll take is to create a new theme file using the FTP client, note that you can also do this using the cPanel for your hosting company.
Recommend Hosting: If you want to host your website without issues and easily create shortcodes, you should host your site on Bluehost.
Once FTP Connection is set, navigate to WP Content and then click on Themes. You should open your current theme’s directory and in the theme directory, create a new file and name it ‘myshortcodes.php’.
Open the myshortcodes.php file, do this by right-clicking on the file and clicking on view/edit, this will then open the file with a text editor.
Once the file is open add the block code below and save the file, you may need to confirm changes in some hosting servers like Hostinger.
The next step is to open the functions.php file right in the same theme’s directory (your active theme) and add the code below at the bottom of the document:
include ( 'myshortcodes.php' )
After adding the code to your functions.php file, click on Save Changes and proceed to the next step which is…
Create a Shortcode Function
The next step we need to take now is to create a shortcode function, all you have to do is open the myshortcodes.php file again by right-clicking and then selecting view/edit.
You should now insert your desired action (function), then add a callback function under the previous function so that WordPress will know that the action is a shortcode.
The myshortcodes.php file should look like the image below…
Save the myshortcodes.php file and your custom Shortcode is ready, the next step is to…
Test your Shortcode
To test your Shortcode, you only need to create a new post or page on your WordPress Website and add the Shortcode [Subscribe].
Once you Preview the post, you should see your Shortcode working.
You can add more custom parameters to your Shortcode by editing the file later.
So far so good, I taught you how to create a self-closing Shortcode, self-closing shortcodes are a sort of standalone shortcodes that don’t need a closing tag.
For example, , ,, and are all self-closing shortcodes. Enclosing shortcodes have a start and end tag allowing you to wrap around content.
and are examples of enclosing shortcodes. Let’s now learn how to create custom enclosing shortcodes in WordPress.
Creating an Enclosing Shortcode
Go back to your Active Theme’s directory and open the myshortcodes.php file and then you should add new lines of code, the final lines should look like the ones in the image below.
The Next step is just to preview your Shortcode by simply adding it to a WordPress Post or Page.
So now you know how to create your own custom WordPress Shortcode, and as you know, it’s incredibly fast.
Want a more visual tutorial on How to create a shortcode in WordPress, Watch This.
If you have any questions regarding WordPress Shortcodes, do not hesitate to ask me in the comments.