14+ years building on WordPress / Replies in under 5 hours
WordPress 8 min read · Updated July 2026

How to Use Shortcodes in WordPress: Complete Guide

Photo of Ajay Khandal
Ajay Khandal
WordPress Developer
TL;DR

A WordPress shortcode is a bracketed tag — [shortcode] or [shortcode attr="value"] — that WordPress expands into content or HTML when a page renders. Where to use them: in the block editor, add a Shortcode block (don't type into a Paragraph block — it renders as text); in the classic editor, paste directly into content; in page builders, use the dedicated Shortcode/Code widget, not a text element; in PHP templates, use echo do_shortcode('[shortcode]'). Shortcodes support attributes ([gallery columns="3"]) and enclosing format ([button]Click[/button]). To register a custom shortcode: add_shortcode('name', 'callback_function') in functions.php — the callback returns (not echoes) the HTML string. Shortcodes are still fully supported in 2026 but blocks are the preferred pattern for new functionality.

A shortcode is a small tag in square brackets — like [contact-form] or — that WordPress expands into content or HTML when a page renders. Shortcodes let you drop complex functionality into posts, pages, or templates without writing code each time. This guide covers every context where you can use shortcodes, how to pass attributes, how to build your own, and how shortcodes fit into WordPress in 2026 alongside the block editor.

Shortcode Formats

There are three formats a shortcode can take:

  • Self-closing: [shortcode_name] — the most common. No wrapping content required.
  • With attributes: [shortcode_name attribute="value" another="123"] — passes parameters to control the output.
  • Enclosing: [shortcode_name]Content goes here[/shortcode_name] — the shortcode wraps and processes the content between the tags.

Plugins that provide shortcodes document which format they use and what attributes they accept. Common examples: WooCommerce uses [products] with category/tag attributes; contact form plugins output their forms via [contact-form-7 id="123"]; gallery functionality via .

Using Shortcodes in the Block Editor

The block editor (Gutenberg) has a dedicated Shortcode block for this purpose. To add one:

  1. Click the + button to add a new block (or type / on a new line)
  2. Search for Shortcode and select the Shortcode block
  3. Paste your shortcode into the field — including the square brackets
  4. The block displays the shortcode text in the editor, but renders the output on the front end when the page is published or previewed

You won’t see the shortcode output live in the editor — the Shortcode block is one of the few in WordPress that requires a preview or front-end view to see the rendered result. Click Preview (top right) to check the output before publishing.

Shortcodes inside paragraph blocks don’t work. If you type a shortcode directly into a Paragraph block, WordPress will render it as literal text. Always use the dedicated Shortcode block. The block editor expects shortcodes to be in their own block, not mixed into text content.

Using Shortcodes in the Classic Editor

In the TinyMCE classic editor, shortcodes work directly in the content area in both Visual and Text mode. Type or paste the shortcode anywhere in the post body — WordPress processes it automatically when the post renders.

In Visual mode, the shortcode will appear as the bracketed text. In Text (HTML) mode, you can see and edit it alongside HTML. Either mode works — the rendering happens server-side when the page is served, not in the editor itself.

Classic editor shortcodes also work in the Excerpt field, though fewer plugins process shortcodes in excerpts by default. If you need a shortcode to render in an excerpt, a small functions.php snippet can add the filter.

Using Shortcodes in Page Builders

All major page builders include a dedicated element for shortcodes:

  • Elementor: Add the Shortcode widget (search “Shortcode” in the widget panel). Paste your shortcode in the Enter Your Shortcode field.
  • Divi: Add a Code module. Paste the shortcode inside it.
  • Beaver Builder: Add an HTML module. Paste the shortcode into the HTML content field.
  • WPBakery: Add a Shortcodes element, or use any text element — WPBakery processes shortcodes inside text content.

When a shortcode appears broken or renders as text in a page builder, the most common cause is the page builder encoding square brackets as HTML entities ([ / ] or [ / ]). This happens when content is pasted into a rich text field rather than a code/shortcode-specific field. Use the dedicated shortcode element, not a text or heading element, to avoid this.

Using Shortcodes in Widget Areas

By default, WordPress does not process shortcodes in text widgets. To enable shortcodes in the classic Text widget:

add_filter( 'widget_text', 'do_shortcode' );

Add this to your theme’s functions.php or a site-specific plugin. After adding the filter, any shortcode in a Text widget will render its output rather than display as literal text.

If you’re using the block editor’s widget area (Widgets screen in wp-admin), use a Shortcode block there — the same as you would in a post. The filter above is only needed for the legacy Text widget.

Using Shortcodes in PHP Templates with do_shortcode()

To execute a shortcode inside a theme template file, PHP file, or anywhere outside the WordPress content pipeline, use the do_shortcode() function:

<?php echo do_shortcode( '[your_shortcode_here]' ); ?>

The function takes the shortcode string and returns the rendered HTML output. You must echo it — do_shortcode() returns a string, it doesn’t output anything on its own.

With attributes:

<?php echo do_shortcode( '' ); ?>

With a variable shortcode name or dynamic attributes:

<?php
$post_id = get_the_ID();
echo do_shortcode( '[my_shortcode post="' . intval( $post_id ) . '"]' );
?>

Note the intval() call when inserting dynamic values into shortcode attributes — this sanitises the value before it becomes part of a string. This isn’t required for static shortcodes but is good practice when the attribute value comes from a variable. For more on using PHP in WordPress templates, see how to get the title in a WordPress template page.

Shortcode Attributes

Attributes let users pass parameters to a shortcode. Plugin documentation will list available attributes and their defaults. Common pattern:

[product_showcase category="featured" limit="6" columns="3"]

When you’re building your own shortcode (see next section), you define which attributes your shortcode accepts and what their default values are. When using a plugin’s shortcode, check the plugin’s documentation — the wp-admin settings page for the plugin often lists available shortcodes and their attribute options.

Enclosing Shortcodes

Some shortcodes wrap content — the output is built around the text or HTML between opening and closing tags:

[button url="https://example.com" style="primary"]Get Started[/button]

The content between the tags (Get Started in this case) is passed to the shortcode function as a parameter. WordPress’s built-in shortcode is an enclosing shortcode — it wraps an image with a caption element. The plugin’s documentation will indicate whether a shortcode is self-closing or enclosing.

Creating a Custom Shortcode

If no plugin provides the shortcode you need, you can register your own with add_shortcode(). Add this to your theme’s functions.php or a site-specific plugin:

/**
 * Outputs the current year — useful for keeping copyright notices current.
 */
function current_year_shortcode() {
    return date( 'Y' );
}
add_shortcode( 'current_year', 'current_year_shortcode' );

After adding this, the shortcode [current_year] will output the current year wherever it’s placed.

A shortcode with attributes:

function highlight_box_shortcode( $atts, $content = null ) {
    $atts = shortcode_atts(
        array(
            'color' => 'yellow',
        ),
        $atts,
        'highlight_box'
    );

    return '<div class="highlight-' . esc_attr( $atts['color'] ) . '">'
        . do_shortcode( $content )
        . '</div>';
}
add_shortcode( 'highlight_box', 'highlight_box_shortcode' );

This registers an enclosing shortcode [highlight_box color="blue"]Content[/highlight_box] that wraps content in a styled div. Key points:

  • shortcode_atts() merges provided attributes with defaults — any attribute not passed by the user gets its default value.
  • Always esc_attr() attribute values before inserting them into HTML output — shortcode attributes come from post content and could contain unexpected characters.
  • Call do_shortcode( $content ) on the enclosed content to allow nested shortcodes to render.
  • The callback returns the HTML string — it does not echo it. WordPress handles the output.

If the functionality you need is more complex than a few lines, consider whether a custom block might be a better fit. For the comparison between shortcode-based extensions and custom code approaches, see page builders vs. custom code in WordPress.

Shortcodes vs. Blocks in 2026

Shortcodes were introduced in WordPress 2.5 (2008) as a way to embed functionality in post content without writing HTML. The block editor, introduced in WordPress 5.0 (2018), provides a visual, modular alternative to shortcodes for most of the same use cases.

Shortcodes are still fully supported and will not be removed from WordPress — too many sites depend on them. But for new functionality, blocks are the preferred pattern:

  • Blocks are preferable when: you’re building new functionality, you want editors to see the output in the editor rather than a placeholder, you want drag-and-drop reordering, or you’re starting a project where you control the editor experience.
  • Shortcodes are appropriate when: you’re using a plugin that provides shortcodes and hasn’t shipped a block equivalent yet, you need to output content in a PHP template via do_shortcode(), you’re working in a classic editor environment, or you need to embed functionality in locations that don’t support blocks (widget areas on older themes, certain theme option fields).

The two can coexist on the same site without conflict. Many widely-used plugins (WooCommerce, contact form plugins, membership plugins) ship both shortcodes and blocks, letting you choose the appropriate format for each context.

Troubleshooting: Shortcode Showing as Text

If a shortcode appears on the front end as literal text (like [contact-form-7 id="123"] instead of the rendered form), the usual causes are:

  • The plugin that registers the shortcode is not active. WordPress only processes shortcodes that have been registered via add_shortcode(). If the plugin is deactivated, the shortcode is unknown and renders as text. Check wp-admin → Plugins and verify the relevant plugin is active.
  • Wrong location in the block editor. Typing a shortcode into a Paragraph block rather than a Shortcode block will render it as text. Replace the paragraph block with a Shortcode block.
  • Square brackets encoded by a page builder or editor. Some rich text editors convert [ to HTML entities. Use the dedicated shortcode element in your page builder rather than a text element.
  • The shortcode tag is misspelled. Shortcode names are registered exactly — a typo (extra space, wrong underscore) means WordPress doesn’t recognise it.

For persistent issues tracing plugin interactions, see how to identify and fix WordPress plugin conflicts.

Frequently asked questions

A shortcode is a bracketed tag — like [gallery] or [contact-form-7 id="123"] — that WordPress replaces with rendered HTML or content when a page is served. Shortcodes were introduced in WordPress 2.5 as a way to embed complex functionality in posts and pages without writing HTML directly. They come in three forms: self-closing ([shortcode]), with attributes ([shortcode size="large"]), and enclosing ([shortcode]content[/shortcode]). Plugins register their shortcodes using the add_shortcode() function; if the plugin providing a shortcode is deactivated, the shortcode renders as literal text.

Three common causes: (1) The plugin that registers the shortcode is not active — WordPress only renders shortcodes that have been registered via add_shortcode(). Check wp-admin → Plugins. (2) You typed the shortcode into a Paragraph block in the block editor — shortcodes must go in a dedicated Shortcode block, not a Paragraph block. (3) A page builder encoded the square brackets as HTML entities — use the page builder's dedicated Shortcode or Code element, not a rich text/heading element. A fourth less common cause: the shortcode tag is misspelled — names are case-sensitive and an extra space or wrong character means WordPress won't recognise it.

Add a Shortcode block — click the + block inserter, search 'Shortcode', and select the Shortcode block. Paste your shortcode (including square brackets) into the block's text field. The block editor shows the shortcode tag in the editor view, not the rendered output — click Preview to see what it looks like on the front end. Do not type shortcodes into Paragraph blocks; they will render as literal text. The Shortcode block is specifically designed to handle this and processes shortcodes correctly.

Use the do_shortcode() function and echo its return value: echo do_shortcode('[your_shortcode_here]'); The function returns the rendered HTML as a string — it doesn't output anything on its own, so you must echo it. You can pass attributes inside the string: echo do_shortcode('[gallery ids="1,2,3" columns="2"]'); When including dynamic values as attributes, sanitise them first: echo do_shortcode('[my_shortcode id="' . intval($id) . '"]'); do_shortcode() works anywhere in a PHP file — theme templates, functions.php, plugins.

Use add_shortcode('tag_name', 'callback_function') in your theme's functions.php or a site-specific plugin. The callback receives an $atts array (the shortcode's attributes) and optionally $content (for enclosing shortcodes). Use shortcode_atts() to merge provided attributes with your defaults. The callback must return (not echo) the HTML string. Example: function my_year_shortcode() { return date('Y'); } add_shortcode('year', 'my_year_shortcode'); After adding this, [year] in any post or page outputs the current year. Always esc_attr() attribute values before inserting them into HTML output.

For new functionality you're building: blocks. The block editor is WordPress's current and future direction — blocks render visually in the editor, support drag-and-drop, and integrate with Full Site Editing. For existing plugins that provide shortcodes: use the shortcode if the plugin hasn't shipped a block yet. Many widely-used plugins (WooCommerce, contact form plugins) now provide both. For PHP template usage: shortcodes via do_shortcode() are still the right tool — blocks don't have an equivalent do_block() for arbitrary template insertion. Shortcodes are fully supported and won't be removed from WordPress — the choice is about which fits the context, not which is 'correct.'

Photo of Ajay Khandal

Written by Ajay Khandal

I'm a freelance WordPress developer with 14+ years of experience building, fixing, and speeding up sites for businesses, agencies, and store owners across the US, UK, Europe, and Australia. I specialize in custom themes, WooCommerce, and performance — the kind of work that shows up as faster load times and fewer support tickets. No account managers, no outsourced tickets — you work directly with me, with replies typically inside 5 hours.

Work with me →