shortcode_parse_atts( string $text ): array
- Since
- 2.5.0, 6.5.0
- Source
wp-includes/shortcodes.php:613
Description
The attributes list has the attribute name as the key and the value of the attribute as the value in the key/value pair. This allows for easier retrieval of the attributes, since all attributes have to be known.
Compatibility
- WordPress
- since 6.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
$textstring- Shortcode arguments list.
Return value
array- Array of attribute values keyed by attribute name.
Returns empty array if there are no attributes or if the original arguments string cannot be parsed.
Performance profile
How much work a call to shortcode_parse_atts() 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
- 16–22
- Plugin surface
- None
- Called by
- 6
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 101.
Nothing here hands control to plugin code.
6 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 input
preg_match_all()called directly
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 shortcode_parse_atts() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 16–22 | get_shortcode_atts_regex(), preg_match_all() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 101 | 16–22 | 15 | |
| 8.5 | 101 | 16–22 | 15 | |
| 8.4 | 101 | 16–22 | 15 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 110 | 19–25 | 15 | |
| 8.2 | 110 | 19–25 | 15 | |
| 8.1 | 110 | 19–25 | 15 | |
| 7.4 | 110 | 19–25 | 15 |
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 · 2
- get_shortcode_atts_regex()Retrieves the shortcode attributes regex.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
Used by · 6
- WP_oEmbed::discover()Attempts to discover link tags at the given URL for an oEmbed provider.
- do_shortcode_tag()Regular Expression callable for do_shortcode() for calling shortcode hook.
- get_post_galleries()Retrieves galleries from the passed post's 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 shortcode_parse_atts( $text ) { $atts = array(); $pattern = get_shortcode_atts_regex(); $text = preg_replace( "/[\x{00a0}\x{200b}]+/u", ' ', $text ); if ( preg_match_all( $pattern, $text, $match, PREG_SET_ORDER ) ) { foreach ( $match as $m ) { if ( ! empty( $m[1] ) ) { $atts[ strtolower( $m[1] ) ] = stripcslashes( $m[2] ); } elseif ( ! empty( $m[3] ) ) { $atts[ strtolower( $m[3] ) ] = stripcslashes( $m[4] ); } elseif ( ! empty( $m[5] ) ) { $atts[ strtolower( $m[5] ) ] = stripcslashes( $m[6] ); } elseif ( isset( $m[7] ) && strlen( $m[7] ) ) { $atts[] = stripcslashes( $m[7] ); } elseif ( isset( $m[8] ) && strlen( $m[8] ) ) { $atts[] = stripcslashes( $m[8] ); } elseif ( isset( $m[9] ) ) { $atts[] = stripcslashes( $m[9] ); } } // Reject any unclosed HTML elements. foreach ( $atts as &$value ) { if ( str_contains( $value, '<' ) ) { if ( 1 !== preg_match( '/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value ) ) { $value = ''; } } } } return $atts;}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.
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.