wppaste
WordPress

do_shortcodes_in_html_tags( string $content, bool $ignore_html, array $tagnames ): string

Since
4.2.3
Source
wp-includes/shortcodes.php:465
Searches only inside HTML elements for shortcodes and process them.

Description

Any [ or ] characters remaining inside elements will be HTML encoded to prevent interference with shortcodes that are outside the elements.
Assumes $content processed by KSES already. Users with unfiltered_html capability may get unexpected output if angle braces are nested in tags.

Compatibility

WordPress
since 4.2.3
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_htmlbool
When true, all square braces inside elements will be encoded.
$tagnamesarray
List of shortcodes to find.

Return value

string
Content with shortcodes filtered out.

Performance profile

How much work a call to do_shortcodes_in_html_tags() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
18–19

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost do_shortcodes_in_html_tags() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always18–19get_shortcode_regex(), wp_html_split()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev14018–1922
8.514018–1922
8.414018–192250 fewer instructions than PHP 8.3
8.319024–2522
8.219024–2522
8.119024–2522
7.419024–2522

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 · 6

Used by · 2

Source code

function do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ) {	// Normalize entities in unfiltered HTML before adding placeholders.	$trans   = array(		'&#91;' => '&#091;',		'&#93;' => '&#093;',	);	$content = strtr( $content, $trans );	$trans   = array(		'[' => '&#91;',		']' => '&#93;',	); 	$pattern = get_shortcode_regex( $tagnames );	$textarr = wp_html_split( $content ); 	foreach ( $textarr as &$element ) {		if ( '' === $element || '<' !== $element[0] ) {			continue;		} 		$noopen  = ! str_contains( $element, '[' );		$noclose = ! str_contains( $element, ']' );		if ( $noopen || $noclose ) {			// This element does not contain shortcodes.			if ( $noopen xor $noclose ) {				// Need to encode stray '[' or ']' chars.				$element = strtr( $element, $trans );			}			continue;		} 		if ( $ignore_html || str_starts_with( $element, '<!--' ) || str_starts_with( $element, '<![CDATA[' ) ) {			// Encode all '[' and ']' chars.			$element = strtr( $element, $trans );			continue;		} 		$attributes = wp_kses_attr_parse( $element );		if ( false === $attributes ) {			// Some plugins are doing things like [name] <[email]>.			if ( 1 === preg_match( '%^<\s*\[\[?[^\[\]]+\]%', $element ) ) {				$element = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $element );			} 			// Looks like we found some unexpected unfiltered HTML. Skipping it for confidence.			$element = strtr( $element, $trans );			continue;		} 		// Get element name.		$front   = array_shift( $attributes );		$back    = array_pop( $attributes );		$matches = array();		preg_match( '%[a-zA-Z0-9]+%', $front, $matches );		$elname = $matches[0]; 		// Look for shortcodes in each attribute separately.		foreach ( $attributes as &$attr ) {			$open  = strpos( $attr, '[' );			$close = strpos( $attr, ']' );			if ( false === $open || false === $close ) {				continue; // Go to next attribute. Square braces will be escaped at end of loop.			}			$double = strpos( $attr, '"' );			$single = strpos( $attr, "'" );			if ( ( false === $single || $open < $single ) && ( false === $double || $open < $double ) ) {				/*				 * $attr like '[shortcode]' or 'name = [shortcode]' implies unfiltered_html.				 * In this specific situation we assume KSES did not run because the input				 * was written by an administrator, so we should avoid changing the output				 * and we do not need to run KSES here.				 */				$attr = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $attr );			} else {				/*				 * $attr like 'name = "[shortcode]"' or "name = '[shortcode]'".				 * We do not know if $content was unfiltered. Assume KSES ran before shortcodes.				 */				$count    = 0;				$new_attr = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $attr, -1, $count );

Changelog

Introduced in 4.2.3. 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 7.1.0 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.