1. Open your functions.php file.
2. Add the following code to the file:
function my_shortcode_function() {
// Your code here
}
add_shortcode(‘my_shortcode’, ‘my_shortcode_function’);
3. Replace “my_shortcode” with the name of your shortcode.
4. Replace “my_shortcode_function” with the name of the function that will run when the shortcode is called.
5. Add the code that you want to run when the shortcode is called inside the function.
6. Save the functions.php file.
7. Use the shortcode in your posts or pages by typing [my_shortcode].
Learning how to create a shortcode in WordPress can be an efficient way to customize your posts and pages. However, if you’re new to the process, you may struggle to understand how to use such a feature on your site.
That’s why we’ve put together a guide to help you get started. By looking at how shortcodes work and how to apply them effectively, you can begin customizing your content to your liking without the need for additional plugins.
In this post, we’ll discuss what WordPress shortcodes are and why you might consider using them. We’ll then show you how to create your own. Let’s get started!
How to Create a Shortcode in WordPress Video Tutorial
Learn step-by-step how to create a WordPress shortcode.
Subscribe For more educational videos!
Hostinger Academy
What Are WordPress Shortcodes
WordPress shortcodes act as shortcuts that allow you to embed elements into a post or page quickly. These usually consist of a single line of code within square brackets, such as [exampleshortcode]
, for example. This code will display a predetermined feature on the front end of your site.
WordPress first introduced shortcodes with the release of the Shortcode API. This enables users to easily add engaging elements to their posts and pages, such as Google Maps or Facebook “Like” button.
There are six default shortcodes in WordPress:
- caption: wraps captions around content
- gallery: shows image galleries
- audio: embeds and plays audio files
- video: embeds and plays video files
- playlist: displays a collection of audio or video files
- embed: wraps embedded items
You will also come across two basic types of shortcode formatting – self-closing and enclosing. Self-closing shortcodes can stand on their own and do not need a closing tag, such as the gallery
or video
shortcodes above.
Meanwhile, enclosing shortcodes wrap around the content you want to modify and require you to close the tag manually. For example, you can embed a YouTube video by wrapping the URL in embed
and /embed
tags:
For example, this would create the following result:
Some of the best WordPress plugins come with their own shortcodes. For instance, WP Forms and Contact Form 7 have shortcodes that enable you to embed a WordPress contact form into a post or page quickly. You can also use a plugin like MaxButtons to add a WordPress button shortcode wherever you want on your site.
Why Should You Consider Using WordPress Shortcodes
There are many reasons you may wish to use WordPress shortcodes. For instance, it’s easier and quicker than learning and writing a long piece of code in HTML. Moreover, they help you keep your content clean and accessible.
Shortcodes can be used to automate some features that you use repeatedly. For instance, if you use a call to action (CTA) button on every one of your posts, having a dedicated shortcode ready can be a speedy and practical solution.
It is worth mentioning that the Gutenberg Editor does operate similarly, relying on the use of shortcodes. It enables WordPress users to add several interactive features through the use of blocks.
Such a method is much more beginner-friendly, as you can add content directly onto the user interface. However, the WordPress Block Editor is still limited in what it offers. Luckily, it comes with a Shortcode block, enabling you to add custom content to your pages.
How to Create a Shortcode in WordPress (In 7 Steps)
If you already have some coding knowledge, you may want to create your own custom shortcodes. This gives you complete control over the appearance and functionality of your site.
Let’s look at how to create a custom WordPress shortcode. In this tutorial, we’ll be adding social media links to a post as our example.
Step 1 – Create a New Theme File
Before you start, it’s a good idea to fully backup your WordPress site. You’ll also need to create a separate file for your custom shortcode outside your theme’s functions.php file. This will provide a fallback in case something goes wrong.
You can use a File Transfer Protocol (FTP) client such as FileZilla to access your site’s theme files. Once connected to your site, navigate to wp-content > themes, and locate the folder for your current theme. In our example, this will be rosa-lite:
Open your theme’s folder, right-click it, and hit the Create new file button.
Name your new file custom-shortcodes.php and click OK. You can then edit it by right-clicking on it and selecting the View/Edit option:
This will open the file in your default text editor. Then, you’ll simply need to add the following block of code:
<?php ?>
This ensures that your new file will be interpreted as PHP, which is the scripting language that WordPress is built on.
You can then save your changes and close the file. Make sure to check the following box to ensure that it is updated on the server and applied to your website:
Next, open the functions.php file in the same theme folder, and add the following line of code at the bottom of the document:
include('custom-shortcodes.php');
This will tell the system to include any changes you make to the custom-shortcodes.php file within functions.php while allowing you to keep them separate. When you’re ready, save your changes and close the file.
Step 2 – Create the Shortcode Function
Next, you’ll need to create the shortcode’s function, commanding it what to do. Select the View/Edit option again for your custom-shortcodes.php file. Use the following code snippet to add an action to hook your function to:
function subscribe_link(){ return 'Follow us on <a rel="nofollow" href="https://twitter.com/Hostinger?s=20">Twitter</a>'; }
Next, you’ll need to add a callback function, which will run when the hook action is activated. Adding the following line of code directly after the one mentioned above will tell WordPress that your function is a shortcode:
add_shortcode('subscribe', 'subscribe_link');
When you create a shortcode using the add_shortcode function, you assign a shortcode tag “($tag)” and a corresponding function “($func)” hook that will run whenever the shortcut is used.
In other words, if the shortcode tag is [subscribe]
, it makes the hook ‘subscribe_link’ direct the visitor to the URL provided.
Therefore, the entire code you’ll be using in your custom-shortcodes.php file will look something like this:
It should be noted that when you’re naming tags, you should only use lowercase letters, although underscores can be used. It’s also crucial to avoid using hyphens, as this can interfere with other shortcodes.
Step 3 – Add the Self-Closing Shortcode to the Website
You can now test your initial code as a self-closing shortcode on your WordPress site. Using the WordPress Block Editor, you can insert the [subscribe]
tag directly into the post:
This will display the following content to your website visitors:
If you’re happy with this shortcode, you don’t need to do anything else. However, if you wish to customize it, you can move on to the next step.
Step 4 – Add Parameters to the Shortcode
You can adapt the ‘subscribe’ shortcode for additional functionality to display other social media links. You can do this by adding a parameter to change the URL.
To add handling attributes, you’ll need to open the custom-shortcodes.php file and add in the following code:
function subscribe_link_att($atts) { $default = array( 'link' => '#', ); $a = shortcode_atts($default, $atts); return 'Follow us on '.$a['link']; } add_shortcode('subscribe', 'subscribe_link_att');
This will enable you to customize the links within your shortcode tag to add them to the Gutenberg Editor. You can paste it over the previous code in the custom-shortcodes.php file. It should look something like this:
The addition of the shortcode_atts() function will combine user attributes with any known attributes, and any missing data will be changed to their default values. When you’re ready, save your changes and close the file.
Step 5 – Test the Parameters
You can now test the updated shortcode within the WordPress Block Editor. In our example, we’re testing our Twitter and Facebook links with the following shortcodes:
[subscribe link='https://www.facebook.com/Hostinger/'] [subscribe link='https://twitter.com/Hostinger?s=20/']
This will produce the following result on the front end:
This self-closing shortcode displays the direct URLs for your social profiles to visitors. However, you may want this feature to look a bit polished.
For instance, you can create an enclosed version that enables you to fully customize the anchor text displayed for users when they’re about to click it. We’ll show you how to do that in the following step.
Step 6 – Create an Enclosing Shortcode
The enclosing shortcode will be formatted similarly to an earlier self-closing example. However, it will include one additional parameter for the function.
First, you’ll need to add $content = null, which identifies this function as an enclosing shortcode. You can then add the WordPress do_shortcode, which will search the content for shortcodes.
Within the custom-shortcodes.php file, add the new enclosing shortcode:
function subscribe_link_att($atts, $content = null) { $default = array( 'link' => '#', ); $a = shortcode_atts($default, $atts); $content = do_shortcode($content); return 'Follow us on <a href="'.($a['link']).'" style="color: blue">'.$content.'</a>'; } add_shortcode('subscribe', 'subscribe_link_att');
When you’re ready, your custom-shortcode.php file should look like this:
The previous code also has an additional “style” attribute, which will change the anchor text to a blue color. Don’t forget to save your changes when you’re done!
Step 7 – Add an Enclosing Shortcode to the Website
You can now insert your enclosing shortcode into the WordPress Block Editor to see the final result:
As you noticed, you can easily change the URLs of your social media pages and the anchor text displayed to the visitor using this enclosing shortcode. In this case, we’ve chosen “Facebook” and “Twitter”:
That’s it! You have now created a customized shortcode for subscription links in your pages and posts. Take note that all of the beforementioned steps can be tweaked to make all kinds of different elements using the WordPress Shortcodes function.
Conclusion
Adding extra functionality to your WordPress website is a lot simpler with shortcodes. You can use them to customize your existing content and add interactive features, such as contact forms, image galleries, or subscription links.
In this post, we learned how to create your very own shortcode in WordPress in seven easy steps:
- Create a new theme file
- Create the shortcode function
- Add the self-closing shortcode to the website
- Add parameters to the shortcode
- Test the parameters
- Create an enclosing shortcode
- Add the enclosing shortcode to the website
Have more questions regarding how to create your very own WordPress shortcodes or planning to optimize your site performance with WordPress hosting? Let us know in the comments section below!
Create a Shortcode in WordPress FAQ
Now that you know how to create a shortcode, here are some of the frequently asked questions about the topic.
How Do I Add a Shortcode to My WordPress Website?
The simplest way to add a shortcode is by using the block in your editor. First, you need to edit the post and page where you want to add the shortcode. Then, click on the Add block button to insert a Shortcode block.
How Do I Create a Dynamic Shortcode in WordPress?
Creating a dynamic shortcode in WordPress can be simplified using the Shortcodes Ultimate plugin, which provides an easy-to-use interface to create custom shortcodes. This plugin can help you add unique features and extra functionality to posts and pages throughout your website.
Does WordPress Have a Shortcode Template?
The WordPress do_shortcode function makes it easy to include shortcodes directly in your theme’s template files. All you need to do is include the shortcode inside of the do_shortcode function. Then, use the function in the template location where you want the shortcode to appear.
How to Create a Shortcode in WordPress (In 7 Steps)
Shortcodes are a great way to add dynamic content to your WordPress site. They allow you to easily add complex content, such as buttons, tables, and other elements, without having to write any code. In this tutorial, we’ll show you how to create a shortcode in WordPress in 7 easy steps.
Step 1: Create a Function for Your Shortcode
The first step is to create a function for your shortcode. This function will contain the code that will be executed when the shortcode is used. To create the function, open your theme’s functions.php file and add the following code:
function my_shortcode_function() {
// Your code goes here
}
add_shortcode('my_shortcode', 'my_shortcode_function');
In the code above, we’ve created a function called my_shortcode_function and added it to the WordPress shortcode API using the add_shortcode() function. The first parameter is the name of the shortcode (in this case, “my_shortcode”), and the second parameter is the name of the function.
Step 2: Add Your Content to the Function
Now that you’ve created the function, you can add the content that will be displayed when the shortcode is used. For example, if you want to display a button, you can add the following code to the function:
function my_shortcode_function() {
return '<button>Click Here</button>';
}
add_shortcode('my_shortcode', 'my_shortcode_function');
Step 3: Add Parameters to Your Shortcode
You can also add parameters to your shortcode, which will allow you to customize the content that is displayed. For example, if you want to add a parameter to your button shortcode, you can use the following code:
function my_shortcode_function($atts) {
$atts = shortcode_atts(
array(
'text' => 'Click Here',
),
$atts
);
return '<button>'.$atts['text'].'</button>';
}
add_shortcode('my_shortcode', 'my_shortcode_function');
In the code above, we’ve added a parameter called “text” to the shortcode. This parameter will allow you to customize the text that is displayed on the button.
Step 4: Use the Shortcode in Your Posts or Pages
Once you’ve created your shortcode, you can use it in your posts or pages. To do this, simply add the shortcode to the content editor. For example, if you want to use the button shortcode, you can add the following code:
[my_shortcode text="Click Here"]
Step 5: Add Attributes to Your Shortcode
You can also add attributes to your shortcode, which will allow you to customize the output of the shortcode. For example, if you want to add a class attribute to your button shortcode, you can use the following code:
function my_shortcode_function($atts) {
$atts = shortcode_atts(
array(
'text' => 'Click Here',
'class' => '',
),
$atts
);
return '<button class="'.$atts['class'].'">'.$atts['text'].'</button>';
}
add_shortcode('my_shortcode', 'my_shortcode_function');
In the code above, we’ve added a class attribute to the shortcode. This attribute will allow you to add a custom class to the button.
Step 6: Add a Default Value to Your Shortcode
You can also add a default value to your shortcode, which will be used if the parameter is not specified. For example, if you want to add a default value to the class attribute, you can use the following code:
function my_shortcode_function($atts) {
$atts = shortcode_atts(
array(
'text' => 'Click Here',
'class' => 'button',
),
$atts
);
return '<button class="'.$atts['class'].'">'.$atts['text'].'</button>';
}
add_shortcode('my_shortcode', 'my_shortcode_function');
In the code above, we’ve added a default value of “button” to the class attribute. This means that if the class attribute is not specified, the button will have the class “button”.
Step 7: Test Your Shortcode
Once you’ve created your shortcode, you can test it by adding it to a post or page. If everything is working correctly, you should see the content that you added to the function displayed on the page.
And that’s it! You’ve successfully created a shortcode in WordPress. Shortcodes are a great way to add dynamic content to your site, and they’re easy to create. We hope this tutorial has helped you learn how to create a shortcode in WordPress.