do_shortcodes_in_html_tags( string $content, bool $ignore_html, array $tagnames ): string
- Since
- 4.2.3
- Source
wp-includes/shortcodes.php:465
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
- Scaling
- Scales with input
- Instructions
- 18–19
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 140.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 18–19 | get_shortcode_regex(), wp_html_split() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 140 | 18–19 | 22 | |
| 8.5 | 140 | 18–19 | 22 | |
| 8.4 | 140 | 18–19 | 22 | 50 fewer instructions than PHP 8.3 |
| 8.3 | 190 | 24–25 | 22 | |
| 8.2 | 190 | 24–25 | 22 | |
| 8.1 | 190 | 24–25 | 22 | |
| 7.4 | 190 | 24–25 | 22 |
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
- get_shortcode_regex()Retrieves the shortcode regular expression for searching.
- wp_html_split()Separates HTML elements and comments from the text.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_kses_attr_parse()Finds all attributes of an HTML element.
- wp_kses_one_attr()Filters one HTML attribute and ensures its value is allowed.
Used by · 2
- do_shortcode()Searches content for shortcodes and filter shortcodes through their hooks.
- strip_shortcodes()Removes all shortcode tags from the given content.
Source code
function do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ) { // Normalize entities in unfiltered HTML before adding placeholders. $trans = array( '[' => '[', ']' => ']', ); $content = strtr( $content, $trans ); $trans = array( '[' => '[', ']' => ']', ); $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.
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.