Back to WordPress Glossary

What Is a Shortcode in WordPress?

A shortcode is a small tag in square brackets, like , that WordPress replaces with dynamic content when a page loads. WordPress doesn’t execute PHP placed in post content by default, so shortcodes, introduced in WordPress 2.5, let you add galleries, audio, video, and plugin features by calling code a developer has already registered.

More About Shortcodes

WordPress includes six built-in shortcodes:

  • [audio] embeds and plays audio files
  • [caption] wraps captions around content, usually images
  • [embed] wraps embedded items
  • [gallery] shows image galleries
  • [playlist] displays a collection of audio or video files
  • [video] embeds and plays video files

Anything beyond those six, such as a contact form or a slideshow, comes from plugins. Many plugins register their own shortcodes as the way you place their output: drop the tag where the output should appear, and the plugin fills it in.

How shortcodes work

When WordPress renders a post, it scans the content for registered bracketed tags and replaces each one with the output of the PHP function mapped to that tag. Your visitors see the gallery or the player; the tag itself never appears on the published page.

The feature exists because WordPress doesn't execute PHP placed in post content by default. The WordPress Plugin Handbook describes that restriction as a security precaution. Shortcodes, introduced in WordPress 2.5, let content authors trigger registered code without writing PHP themselves. The callback behind a shortcode is still PHP, though, so it's only as safe as the developer made it: the handbook's best practices tell shortcode developers to sanitize the input and escape the output.

Adding a shortcode to a post or page

To add a shortcode to a post or page, open it in the block editor, click the Add Block button, and insert the Shortcode block; you'll find it in the Widgets section of the block inserter. You can also type /shortcode and press Enter. Then type the tag into the block.

Shortcodes are written between square brackets. For example, to add a gallery, type:

[gallery ids="1,2,3,4,5,6" columns="3" size="large"]

This displays the images whose attachment IDs are 1 through 6, in 3 columns, at the registered 'large' image size (the default is 'thumbnail'). Attachment IDs are shared with posts and pages, so your images almost never carry IDs 1 through 6. To find an image's real ID, open it in the Media Library.

Shortcodes vs. blocks

You'll rarely type the built-in shortcodes by hand anymore. Since the block editor became WordPress's default, galleries, audio, video, and embeds all have dedicated blocks. Shortcodes remain the standard way many plugins place their output.

  • Use a block when one exists for the job. The Gallery, Audio, Video, and Embed blocks cover the same ground as the matching shortcodes and show you the result as you edit.
  • Use a shortcode when a plugin provides one, or when you need the same dynamic output repeated across many pages.

Creating custom shortcodes

Developers register a shortcode with the add_shortcode() function, which maps a bracketed tag to a PHP callback in a plugin or a theme's functions.php file. Whatever the callback returns replaces the tag on the page.

For more examples, including a walkthrough of building your own, see our guide An Introduction to WordPress Shortcodes (With Examples).

Frequently Asked Questions

WordPress only replaces tags that are registered. If the plugin or theme that registered a shortcode is deactivated or deleted, the raw bracketed text prints on the page for visitors; a mistyped tag fails the same way. Reactivate or replace the plugin, or delete the leftover tags.
Yes in posts, pages, and the Text widget, which processes shortcodes automatically. Theme template files don't: a shortcode there only runs when passed through the do_shortcode() function, for example echo do_shortcode('[gallery]').
The one loaded last takes precedence. WordPress keeps one callback per shortcode tag, so when two plugins register the same name, one overrides the other. That's why WordPress tells developers to prefix shortcode names. If a shortcode's output changes unexpectedly, check for a naming collision.
Special Offer

Powerful WordPress Hosting

Reliable, lightning-fast hosting solutions specifically optimized for WordPress. Find the perfect plan for you by clicking below.

WordPress Hosting Plans