do_shortcode( string $content, bool $ignore_html = false ): string
- Since
- 2.5.0
- Source
wp-includes/shortcodes.php:243
Description
If there are no shortcode tags defined, then the content will be returned without any filtering. This might cause issues when plugins are disabled but the shortcode will still show up in the post or content.
Compatibility
- WordPress
- since 2.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$contentstring- Content to search for shortcodes.
$ignore_htmlbooloptional- When true, shortcodes inside HTML elements will be skipped.
Default false.Default:false
Return value
string- Content with shortcodes filtered out.
Performance profile
How much work a call to do_shortcode() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 6–66
- Plugin surface
- None
- Called by
- 8
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 69.
Nothing here hands control to plugin code.
8 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- regexregular expression over the whole input
preg_match_all()called directly
Further down the call graph this can also reach hook, option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost do_shortcode() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–10 | none |
$content && !empty($shortcode_tags) && is_array($shortcode_tags) && empty($tagnames) | 26 | preg_match_all(), array_keys(), array_intersect() |
$content && !empty($shortcode_tags) && is_array($shortcode_tags) && !empty($tagnames) | 57 | preg_match_all(), array_keys(), array_intersect(), has_filter(), do_shortcodes_in_html_tags(), get_shortcode_regex(), preg_replace_callback(), unescape_invalid_shortcodes() |
$content && !empty($shortcode_tags) && is_array($shortcode_tags) && !empty($tagnames) | 61 | preg_match_all(), array_keys(), array_intersect(), has_filter(), do_shortcodes_in_html_tags(), get_shortcode_regex(), preg_replace_callback(), unescape_invalid_shortcodes(), remove_filter() |
$content && !empty($shortcode_tags) && is_array($shortcode_tags) && !empty($tagnames) | 62 | preg_match_all(), array_keys(), array_intersect(), has_filter(), add_filter(), do_shortcodes_in_html_tags(), get_shortcode_regex(), preg_replace_callback(), unescape_invalid_shortcodes() |
$content && !empty($shortcode_tags) && is_array($shortcode_tags) && !empty($tagnames) | 66 | preg_match_all(), array_keys(), array_intersect(), has_filter(), add_filter(), do_shortcodes_in_html_tags(), get_shortcode_regex(), preg_replace_callback(), unescape_invalid_shortcodes(), remove_filter() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 69 | 6–66 | 6 | |
| 8.5 | 69 | 6–66 | 6 | |
| 8.4 | 69 | 6–66 | 6 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 72 | 9–69 | 6 | |
| 8.2 | 72 | 9–69 | 6 | |
| 8.1 | 72 | 9–69 | 6 | |
| 7.4 | 72 | 9–69 | 6 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 7
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- has_filter()Checks if any filter has been registered for a hook.
- add_filter()Adds a callback function to a filter hook.
- do_shortcodes_in_html_tags()Searches only inside HTML elements for shortcodes and process them.
- get_shortcode_regex()Retrieves the shortcode regular expression for searching.
- unescape_invalid_shortcodes()Removes placeholders added by do_shortcodes_in_html_tags().
- remove_filter()Removes a callback function from a filter hook.
Used by · 8
- WP_Embed::run_shortcode()Processes the [embed] shortcode.
- WP_Widget_Text::widget()Outputs the content for the current Text widget instance.
- apply_shortcodes()Searches content for shortcodes and filter shortcodes through their hooks.
- get_the_block_template_html()Returns the markup for the current template.
- img_caption_shortcode()Builds the Caption shortcode output.
- render_block_core_template_part()Renders the `core/template-part` block on the server.
- wp_ajax_parse_embed()Applies [embed] Ajax handlers to a string.
- wp_ajax_parse_media_shortcode()
Source code
function do_shortcode( $content, $ignore_html = false ) { global $shortcode_tags; if ( ! str_contains( $content, '[' ) ) { return $content; } if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) { return $content; } // Find all registered tag names in $content. preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches ); $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] ); if ( empty( $tagnames ) ) { return $content; } // Ensure this context is only added once if shortcodes are nested. $has_filter = has_filter( 'wp_get_attachment_image_context', '_filter_do_shortcode_context' ); $filter_added = false; if ( ! $has_filter ) { $filter_added = add_filter( 'wp_get_attachment_image_context', '_filter_do_shortcode_context' ); } $content = do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ); $pattern = get_shortcode_regex( $tagnames ); $content = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $content ); // Always restore square braces so we don't break things like <!--[if IE ]>. $content = unescape_invalid_shortcodes( $content ); // Only remove the filter if it was added in this scope. if ( $filter_added ) { remove_filter( 'wp_get_attachment_image_context', '_filter_do_shortcode_context' ); } return $content;}Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/shortcodes.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.