Run a shortcode in a string or custom field
Use do_shortcode() to render shortcodes stored in custom fields, ACF values or strings, with escaping, security cautions and when to use do_blocks instead.
At a glance
WordPress only processes shortcodes where its shortcode functions are called, so text from a custom field, widget option or your own code stays as plain brackets until processed. Pass the value through do_shortcode() after cleaning it with wp_kses_post(), and only for content written by people you trust. Use do_blocks() for block markup, and avoid running shortcodes on visitor input.
Updated November 29, 2026 · For WordPress theme and plugin developers
- For developers rendering shortcodes from custom fields, ACF values or strings
- Pattern echo do_shortcode( wp_kses_post( $value ) )
- Alternatives do_blocks() for block markup, apply_filters( 'the_content' ) only for full formatting
- Security never pass visitor-supplied text to do_shortcode()
- Safety back up first and add code to a child theme or small plugin
- Works with post meta, options, ACF Text and Wysiwyg fields
- Help WordPress development and plugin development services
On this page
WordPress only processes shortcodes in places that call its shortcode functions, such as the post editor output. A string from a custom field, a widget option or your own code stays as plain [text] until you process it. Pass the value through `do_shortcode()`, after cleaning it with `wp_kses_post()`, and only for content written by people you trust.
What is the basic pattern?
$value = get_post_meta( get_the_ID(), 'intro_text', true );
echo do_shortcode( wp_kses_post( $value ) );get_post_meta()returns the stored text. The third argumenttruereturns a single value instead of an array.wp_kses_post()strips tags and attributes a post author should not add, such as<script>, while keeping links and paragraphs. It leaves shortcode brackets alone.do_shortcode()finds registered shortcodes and replaces each with its output. Unknown shortcodes stay as written.
For a hard-coded string, such as a note in a template, the string is your own code, so cleaning is not needed:
$data = '<div class="intro"><h2>Latest posts</h2>[recent_posts count="5"]</div>';
echo do_shortcode( $data );[recent_posts] stands for any shortcode registered on your site. See creating a recent posts shortcode to make one.
Which function should I use?
| Function | Processes | Use when |
|---|---|---|
do_shortcode() | Classic shortcodes | You only need shortcodes |
apply_shortcodes() | Classic shortcodes | Same behaviour, a clearer newer name |
do_blocks() | Block markup | A field holds <!-- wp:paragraph --> content |
apply_filters( 'the_content', ... ) | Everything: paragraphs, embeds, blocks, plugin additions | You really want full post formatting |
Older tutorials use apply_filters( 'the_content', $data ). It runs every function attached to the_content, which can cause duplicate widgets, extra paragraph tags and, from inside your own the_content callback, an endless loop.
How do I do it with ACF fields and blocks?
ACF's functions respect each field type:
$content = get_field( 'intro_text' );
if ( $content ) {
echo do_shortcode( wp_kses_post( $content ) );
}A Text Area or Text field returns the raw string, so you need do_shortcode(). A Wysiwyg Editor field returns formatted content and many versions already process shortcodes, so a second call is harmless but unnecessary. Test with one shortcode before relying on it. For other plugins' post meta use get_post_meta(), and for options get_option().
If a custom field holds block markup, render it with do_blocks():
echo do_blocks( wp_kses_post( $value ) );do_blocks() does not process classic shortcodes unless they are in a Shortcode block. If a field contains both, run do_blocks() first and then do_shortcode() on the result.
Is it safe to run shortcodes on user input?
do_shortcode() does not ask who wrote the text. If a visitor can submit text that you later print through it, they can use any shortcode your site registers. Some list private posts, show forms, send emails, run queries or print system information. This is a real injection risk.
If you need a limited set, call remove_all_shortcodes(), register only the allowed ones with add_shortcode(), run the call and restore your normal shortcodes afterwards, since core offers no built-in allowlist.
What common problems occur?
| Symptom | Likely cause | Fix |
|---|---|---|
| Shortcode shows as text | Value escaped, for example with esc_html() | Print with do_shortcode( wp_kses_post( ... ) ) |
| Nothing is output | Shortcode not registered in this context | Run later in the load, after init |
| Nested shortcodes break | Enclosing shortcode does not call do_shortcode() on its content | Fix the handler |
| Output in the wrong place | Handler echoes instead of returning | Wrap in ob_start() and ob_get_clean(), or fix the handler |
| Run twice | Content already filtered by the_content | Do not process again |
Need something custom?
Our developers structure the data with sanitising and escaping built in.
Our WordPress development team and plugin developers can do this properly. If you only need a quick price display, see the product price shortcode guide.
How do I execute a shortcode stored in a custom field?
Read the value, sanitise it for output with wp_kses_post(), and pass it to do_shortcode() before printing: echo do_shortcode( wp_kses_post( $value ) ).
Link to this answerWhy use do_shortcode() instead of apply_filters( 'the_content' )?
the_content runs every content filter, including paragraph formatting, embeds and plugin additions. Calling it inside a the_content callback can also loop forever.
Link to this answerIs it safe to run shortcodes from user input?
Not unless you trust the author. Any visitor-supplied text that reaches do_shortcode() can trigger any registered shortcode, including ones that expose data or send mail.
Link to this answerWhat does wp_kses_post() do here?
It strips tags and attributes a post author should not add, such as script, while keeping normal formatting like links and paragraphs. It leaves shortcode brackets alone.
Link to this answerDo I need do_shortcode() with ACF fields?
For a Text or Text Area field, yes, because it returns the raw string. A Wysiwyg Editor field returns formatted content and its the_field() function already processes shortcodes in many versions, so a second call is harmless but unnecessary.
Link to this answerWhat is apply_shortcodes()?
A newer alias of do_shortcode() with the same behaviour. do_shortcode() remains fully supported, so use whichever name you prefer.
Link to this answerHow do I render block markup stored in a field?
Use do_blocks(). It processes blocks but not classic shortcodes unless they are in a Shortcode block. If a field contains both, run do_blocks() first and then do_shortcode() on the result.
Link to this answerWhy does my shortcode show as plain text?
The text is probably escaped, for instance printed with esc_html(), so brackets and output become text. Print with do_shortcode( wp_kses_post( ... ) ) instead.
Link to this answerWhy does a shortcode output nothing?
The shortcode may not be registered in this context. Some plugins register shortcodes only on the front end, on certain hooks or after init. Run your code later in the load.
Link to this answerWhy does the output appear in the wrong place?
Shortcode handlers must return a string. A handler that echoes prints immediately, ahead of surrounding markup. Wrap the call in ob_start() and ob_get_clean() as a workaround, or fix the handler.
Link to this answerHow can I allow only a few shortcodes?
Core has no built-in allowlist. You can remove all shortcodes, register only the allowed ones for the call, then restore the normal set, or handle a single shortcode yourself.
Link to this answerKeep reading
Want an engineer on it?
Tell us what is wrong in plain words. A person reads it, today.