# Run a shortcode in a string or custom field

URL: https://wp24x7.com/execute-shortcode-string-wordpress-custom-field/
Updated: 2026-11-29
Type: blog post
Audience: WordPress theme and plugin developers

> 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.

## Key facts

- 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

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.**

> **Quick answer**
`echo do_shortcode( wp_kses_post( $value ) );` for trusted text. Use `do_blocks()` for block markup. Never pass raw visitor input to `do_shortcode()`. Back up first and add code to a child theme or small plugin.

## What is the basic pattern?

```php
$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 argument `true` returns 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:

```php
$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](/how-to-create-and-add-a-short-code-to-display-recent-posts) 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?

### Text fields
ACF's functions respect each field type:

```php
$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()`.
### Block markup
If a custom field holds block markup, render it with `do_blocks()`:

```php
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.

> **Rules to follow**
Only process text entered by editors or administrators, or users you already trust with that power. Never pass raw `$_GET`, `$_POST` or form submissions to `do_shortcode()`. Sanitise on output with `wp_kses_post` and on input when you save custom data.

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?

**Building custom templates or ACF-driven layouts?**
Our developers structure the data with sanitising and escaping built in.
[Post a project](/post-project)
[WordPress development](/wordpress-development)

Our [WordPress development team](/wordpress-development) and [plugin developers](/wordpress-plugin-development) can do this properly. If you only need a quick price display, see the [product price shortcode guide](/how-to-display-product-price-using-shortcode).

## Frequently asked questions

### 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 ) ).

### Why 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.

### Is 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.

### What 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.

### Do 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.

### What is apply_shortcodes()?

A newer alias of do_shortcode() with the same behaviour. do_shortcode() remains fully supported, so use whichever name you prefer.

### How 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.

### Why 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.

### Why 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.

### Why 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.

### How 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.
