What is a shortcode in WordPress?

WordPress has several interesting and unique features that help you create content on your website more easily. One of them is the shortcode, a WordPress feature designed to allow you to quickly and easily create any type of repeatable content. Here’s how shortcodes work in WordPress and what you can do with them.

What is a shortcode?

A shortcode is a simple piece of text that instructs WordPress to replace it with code defined in a theme, plugin, or WordPress itself. It is typically a word or hyphenated words enclosed in straight brackets. Sometimes, shortcodes have additional “arguments,” or words that follow them with additional instructions for the shortcode parser. One example is the built-in WordPress image gallery shortcode, [gallery]. The word “gallery” alone does not give WordPress enough information to create a image gallery. Which photos or images should be used? How big should they be? How many columns of images should be shown at one time? You can list out these attributes as follows: [gallery ids="729,732,731,720" columns="4" size="medium"]

Why are shortcodes useful?

Shortcodes allow you accomplish something that would otherwise require the use of a substantial amount of code with one simple command. Plugin and theme developers can also create their own shortcodes, too, so the possibilities are endless. For example, our Latest Post Redirect plugin creates an additional shortcode, [latest-post], that will automatically redirect a page to the most recent post you published on WordPress. To use it, all you have to do is create a page on your site and type [latest-post], and the shortcode does all the work for you.

How do shortcodes work with Gutenberg?

If it sounds like shortcodes accomplish a similar feat as Gutenberg blocks, that’s because they do. Like shortcodes, Gutenberg blocks allow you to easily create different types of content by simply filling in a few details. In fact, just like we’ve been using an image gallery as an example of a built-in WordPress shortcode, there’s also a built-in WordPress image gallery Gutenberg block. A lot of content that previously would be created with a shortcode is now found in a Gutenberg block instead.

That doesn’t mean shortcodes are antiquated, however. Consider three reasons why shortcodes still have a place in WordPress:

  1. If you want to use a shortcode within a paragraph, it’s easier to simply type it than it is to use a Gutenberg block.
  2. Sometimes, it’s a lot easier to insert a shortcode than it is a Gutenberg block. For example, that’s why we’ve stuck with a shortcode for our Latest Post Redirect plugin. With a shortcode, you can just create a post, type [latest-post], and you’re good to go. If we switched to a Gutenberg block, it would still be a simple process but would likely take longer to set up a page.
  3. There’s a Gutenberg shortcode block that makes it easy to turn any shortcode into a Gutenberg block.

If you’d like to explore the simplicity of shortcodes, our Latest Post Redirect plugin is a great place to start. Simply subscribe to our email list below and a download link will be delivered to your inbox, absolutely free!