wppaste
WordPress

get_shortcode_regex( array $tagnames = null ): string

Since
2.5.0, 4.4.0
Source
wp-includes/shortcodes.php:324
Retrieves the shortcode regular expression for searching.

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

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
15–19

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
9

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.

WhenInstructionsCalls it makes
!empty($tagnames)15array_map()
empty($tagnames)19array_keys(), array_map()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1915–191
8.51915–191
8.41915–1914 fewer instructions than PHP 8.3
8.32319–231
8.22319–231
8.12319–231
7.42319–231

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

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.

  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.

4.4.0
Added the $tagnames parameter.from the docblock
2.5.0
Introduced.from the docblock

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.