Introduction to WordPress Shortcodes
Shortcodes are one of WordPress’s most powerful features, allowing you to add custom content and functionality without writing lengthy code every time. In this guide, we’ll take you through creating and using your own shortcodes, including self-closing and enclosing types, plus tips for using parameters and attributes effectively.WordPress development agency London
If you’re not familiar with coding, a shortcode plugin might be easier to start with. However, for those wanting full control, creating custom shortcodes offers limitless possibilities.
At our WordPress development agency London, we frequently use custom shortcodes to streamline client workflows, enhance site features, and make content management far more efficient.
A Brief History of Shortcodes
Introduced in WordPress 2.5, shortcodes were originally designed to simplify content insertion and reduce repetitive coding. Over time, they’ve evolved into versatile tools that support advanced layouts, dynamic content, and plugin integrations.
Shortcodes are essentially macros: when inserted, they are replaced with a snippet of code or HTML output. WordPress itself includes several built-in shortcodes, such as gallery and caption. Many plugins and themes also provide their own.
Creating your own shortcodes enables you to:
- Add dynamic content like buttons, images, and columns
- Simplify plugin or theme functionality
- Standardize recurring design elements across your website
Preparing WordPress for Custom Shortcodes
Before you start, it’s a good idea to organize your shortcodes in a separate file. Here’s a common approach:
- Create a new file called
custom-shortcodes.phpinside your theme folder. - Include it in your
functions.phpfile:
include('custom-shortcodes.php');
This keeps your shortcodes organized and easy to maintain.
Creating Basic Shortcodes
Here’s a simple example to create a shortcode that displays an avatar image:
function dotiavatar_function() {
return '<img src="http://dayoftheindie.com/wp-content/uploads/avatar-simple.png" alt="doti-avatar" width="96" height="96" class="left-align" />';
}add_shortcode('dotiavatar', 'dotiavatar_function');
To use it in a post or page, simply insert:
[dotiavatar]
Adding Parameters to Shortcodes
Shortcodes become more flexible when you use parameters (attributes). For example, a rating image shortcode:
function dotirating_function($atts = array()) {
extract(shortcode_atts(array(
'rating' => '5'
), $atts));
return "<img src='http://dayoftheindie.com/wp-content/uploads/{$rating}-star.png' alt='doti-rating' width='130' height='188' class='left-align' />";
}add_shortcode('dotirating', 'dotirating_function');
Usage examples:
[dotirating rating=3]
[dotirating]

Working with Enclosing Shortcodes
Enclosing shortcodes wrap content, similar to BBCode:
function dotifollow_function($atts, $content = null) {
return '<a href="https://twitter.com/DayOfTheIndie" target="blank" class="doti-follow">' . $content . '</a>';
}add_shortcode('dotifollow', 'dotifollow_function');
Example usage:
[dotifollow]Follow us on Twitter![/dotifollow]
You can also combine attributes with enclosing shortcodes for more flexibility:
function dotibutton_function($atts = array(), $content = null) {
extract(shortcode_atts(array(
'link' => '#'
), $atts));
return '<a href="'. $link .'" target="blank" class="doti-button">' . $content . '</a>';
}add_shortcode('dotibutton', 'dotibutton_function');
Usage:
[dotibutton link="/shop/"]Shop Now![/dotibutton]
Using Shortcodes in Widgets
By default, WordPress only supports shortcodes in posts, pages, or custom post types. To enable them in widgets, add this to functions.php:
add_filter('widget_text', 'shortcode_unautop');
add_filter('widget_text', 'do_shortcode');
Now, shortcodes can be used in sidebars just like in post content.
Debugging Your Shortcodes
If a shortcode doesn’t work as expected, check the following:
- Correct spelling of the shortcode tag and attributes
- Shortcode function is properly included and registered
- HTML output is correctly formatted with no missing closing tags
- Use WordPress debug mode or plugins for deeper troubleshooting
Conclusion
Custom shortcodes allow you to add dynamic content, reusable components, and advanced features to your WordPress site with minimal effort. Whether it’s self-closing or enclosing shortcodes, mastering them can save time and streamline your workflow.
For businesses or developers looking for expert guidance, a professional WordPress development agency London can help implement, optimize, and manage custom shortcodes across your site — ensuring both efficiency and functionality.