A shortcode is a small piece of code inside square brackets that adds special content to a post or page. For example, when you type [gallery] in a post, WordPress shows an image gallery in its place.
Shortcodes save you from writing long HTML or PHP code by hand. Here is how they work and how you can use them on your own site.
How Does a Shortcode Work?
When WordPress displays your content, it looks for these bracket tags. If a plugin, your theme or WordPress itself has registered that shortcode, the tag is replaced with the real output. Think of it like a shortcut icon on your desktop: a tiny thing that opens something much bigger.
Many shortcodes also accept extra settings called attributes. For example, [gallery columns="4"] shows the gallery in four columns instead of the default three.
WordPress has a few built-in shortcodes, such as [audio], [video], [caption] and [playlist]. Plugins add plenty more for contact forms, sliders, tables and so on.
How to Add a Shortcode in WordPress
- First, copy the shortcode from your plugin's settings page or documentation.
- Now, open your post or page in the block editor.
- Next, click the + block inserter and add the Shortcode block.
- Here, paste the shortcode and then save or publish the post.
Shortcodes work in widget areas too. Our guide on using shortcodes in sidebar widgets explains how.
If you deactivate the plugin that created a shortcode, the raw bracket text will appear on your pages. So, check your content before you remove such a plugin.
Creating Your Own Shortcode
Developers register new shortcodes with the add_shortcode() function. Here is a tiny example that prints the current year:
function wpt_year_shortcode() {
return wp_date( 'Y' );
}
add_shortcode( 'year', 'wpt_year_shortcode' );
Now, typing [year] anywhere shows the current year, which comes handy in footer copyright lines. Always return the output instead of echoing it, or it may appear in the wrong place. You can learn more in our lesson on the WordPress shortcode.
That's all for now. Shortcodes look small, but they make plenty of tasks quicker. If you have any questions, leave them in the comment box below.