wppaste
WordPress

do_shortcode( string $content, bool $ignore_html = false ): string

Since
2.5.0
Source
wp-includes/shortcodes.php:243
Searches content for shortcodes and filter shortcodes through their hooks.

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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6–66

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 69.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
8

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

WhenInstructionsCalls it makes
always6–10none
$content && !empty($shortcode_tags) && is_array($shortcode_tags) && empty($tagnames)26preg_match_all(), array_keys(), array_intersect()
$content && !empty($shortcode_tags) && is_array($shortcode_tags) && !empty($tagnames)57preg_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)61preg_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)62preg_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)66preg_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

PHPCompiledExecutedBranchesNotes
8.6-dev696–666
8.5696–666
8.4696–6663 fewer instructions than PHP 8.3
8.3729–696
8.2729–696
8.1729–696
7.4729–696

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

Used by · 8

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.