get_shortcode_regex( array $tagnames = null ): string
- Since
- 2.5.0, 4.4.0
- Source
wp-includes/shortcodes.php:324
Description
The regular expression combines the shortcode tags in the regular expression in a regex class.
The regular expression contains 6 different sub matches to help with parsing.
1 - An extra [ to allow for escaping shortcodes with double [[]] 2 - The shortcode name 3 - The shortcode argument list 4 - The self closing / 5 - The content of a shortcode when it wraps some content.
6 - An extra ] to allow for escaping shortcodes with double [[]]
Compatibility
- WordPress
- since 4.4.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
$tagnamesarrayoptional- List of shortcodes to find. Defaults to all registered shortcodes.Default:
null
Return value
string- The shortcode search regular expression.
Performance profile
How much work a call to get_shortcode_regex() 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
- 15–19
- Plugin surface
- None
- Called by
- 9
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 19.
Nothing here hands control to plugin code.
9 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_shortcode_regex() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($tagnames) | 15 | array_map() |
empty($tagnames) | 19 | array_keys(), array_map() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 19 | 15–19 | 1 | |
| 8.5 | 19 | 15–19 | 1 | |
| 8.4 | 19 | 15–19 | 1 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 23 | 19–23 | 1 | |
| 8.2 | 23 | 19–23 | 1 | |
| 8.1 | 23 | 19–23 | 1 | |
| 7.4 | 23 | 19–23 | 1 |
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.
Used by · 9
- do_shortcode()Searches content for shortcodes and filter shortcodes through their hooks.
- do_shortcodes_in_html_tags()Searches only inside HTML elements for shortcodes and process them.
- get_post_galleries()Retrieves galleries from the passed post's content.
- get_shortcode_tags_in_content()Returns a list of registered shortcode names found in the given content.
- has_shortcode()Determines whether the passed content contains the specified shortcode.
- strip_shortcodes()Removes all shortcode tags from the given content.
- twentyeleven_get_gallery_images()Retrieves the IDs for images in a gallery.
- twentyten_get_gallery_images()Retrieves the IDs for images in a gallery.
- wp_ajax_parse_embed()Applies [embed] Ajax handlers to a string.
Source code
function get_shortcode_regex( $tagnames = null ) { global $shortcode_tags; if ( empty( $tagnames ) ) { $tagnames = array_keys( $shortcode_tags ); } $tagregexp = implode( '|', array_map( 'preg_quote', $tagnames ) ); /* * WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag(). * Also, see shortcode_unautop() and shortcode.js. */ // phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation return '\\[' // Opening bracket. . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]. . "($tagregexp)" // 2: Shortcode name. . '(?![\\w-])' // Not followed by word character or hyphen. . '(' // 3: Unroll the loop: Inside the opening shortcode tag. . '[^\\]\\/]*' // Not a closing bracket or forward slash. . '(?:' . '\\/(?!\\])' // A forward slash not followed by a closing bracket. . '[^\\]\\/]*' // Not a closing bracket or forward slash. . ')*?' . ')' . '(?:' . '(\\/)' // 4: Self closing tag... . '\\]' // ...and closing bracket. . '|' . '\\]' // Closing bracket. . '(?:' . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags. . '[^\\[]*+' // Not an opening bracket. . '(?:' . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag. . '[^\\[]*+' // Not an opening bracket. . ')*+' . ')' . '\\[\\/\\2\\]' // Closing shortcode tag. . ')?' . ')' . '(\\]?)'; // 6: Optional second closing bracket for escaping shortcodes: [[tag]]. // phpcs:enable}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.
$tagnames parameter.from the docblockAbout 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.