What Is a WordPress Shortcode? Complete Developer Guide | CrestVox Studio

A WordPress shortcode is a small piece of code enclosed in square brackets, such as [contact-form] or , that can be inserted into WordPress content to embed complex functionality or dynamic content generated by a plugin or theme. Shortcodes act as placeholders: when WordPress renders a page, it replaces each shortcode with the HTML output […]

A WordPress shortcode is a small piece of code enclosed in square brackets, such as [contact-form] or , that can be inserted into WordPress content to embed complex functionality or dynamic content generated by a plugin or theme. Shortcodes act as placeholders: when WordPress renders a page, it replaces each shortcode with the HTML output produced by the function registered to handle that shortcode. They allow non-technical users to add dynamic features like contact forms, sliders, pricing tables, and media galleries to their content without writing any HTML or PHP code.

How WordPress Shortcodes Work

Shortcodes are registered by plugins and themes using the WordPress add_shortcode() function. The function takes two parameters: the shortcode tag (the text that appears inside the square brackets) and a callback function that generates and returns the HTML output. When WordPress encounters a shortcode in post content, it calls the registered callback function, passes any shortcode attributes as arguments, and replaces the shortcode with the function’s return value before displaying the content.

Shortcodes can be self-closing ([shortcode_tag attribute="value"]) or enclosing ([shortcode_tag]content here[/shortcode_tag]). Enclosing shortcodes process the content between their opening and closing tags, which is useful for formatting shortcodes that apply styling or processing to wrapped content.

Examples of Common WordPress Shortcodes

  • — WordPress’s built-in gallery shortcode displays a grid of images attached to the current post.
  • [contact-form-7 id="1234"] — Contact Form 7 uses shortcodes to embed forms created in the admin into any page or post.
  • [woocommerce_cart] — WooCommerce uses shortcodes to place the cart, checkout, and My Account pages on WordPress pages.
  • — WordPress’s built-in caption shortcode wraps images with aligned captions.
  • — Embeds external content from URLs using WordPress’s oEmbed functionality.

Shortcodes vs. Gutenberg Blocks

Shortcodes were the standard way to embed dynamic content in WordPress before the Gutenberg block editor was introduced in WordPress 5.0. Gutenberg blocks now represent the modern, preferred approach for the same functionality, offering visual previews in the editor, better accessibility, and cleaner page output. Most plugins that previously relied on shortcodes have added equivalent Gutenberg blocks as their primary interface, with shortcodes maintained for backward compatibility.

While shortcodes still work in the Gutenberg editor (using the Shortcode block), they display as text codes in the editor rather than visual previews of the content they produce. For new development, creating Gutenberg blocks is the recommended approach. Shortcodes remain relevant in existing content, in contexts where the Classic Editor is still used, and in theme templates where PHP shortcode processing is needed.

Shortcodes in WordPress Themes and Templates

Shortcodes can also be executed in PHP template files using the do_shortcode() function: <?php echo do_shortcode('[contact-form-7 id="1234"]'); ?>. This is useful when a shortcode needs to appear in a theme’s header, footer, or sidebar template where it cannot be inserted through the post editor.

Custom shortcode development is a straightforward way to create reusable content components in WordPress. A well-designed shortcode with sensible default attributes and clear documentation can serve as a simple, content-editor-friendly alternative to a Gutenberg block for teams that prefer the Classic Editor or need to support older WordPress environments.

At CrestVox Studio, we develop custom shortcodes and Gutenberg blocks as part of our WordPress development service, building reusable content components that match each client’s specific content management requirements. Explore our guides on WordPress plugins, Gutenberg, and action hooks for a broader view of how WordPress’s extensibility system works.