wp_sprintf( string $pattern, mixed $args ): string
- Since
- 2.5.0, 5.3.0
- Source
wp-includes/formatting.php:5254
Compatibility
- WordPress
- since 5.3.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
$patternstring- The string which formatted args are inserted.
$argsmixed- Arguments to be formatted into the $pattern string.
Return value
string- The formatted string.
Performance profile
How much work a call to wp_sprintf() 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
- 10–16
- Plugin surface
- 1 hook
- Called by
- 8
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 84.
Third-party callbacks on 'wp_sprintf' run inside this call, and their cost is not bounded by anything here.
8 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()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 wp_sprintf() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10–16 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 84 | 10–16 | 9 | |
| 8.5 | 84 | 10–16 | 9 | |
| 8.4 | 84 | 10–16 | 9 | 15 fewer instructions than PHP 8.3 |
| 8.3 | 99 | 10–19 | 9 | |
| 8.2 | 99 | 10–19 | 9 | |
| 8.1 | 99 | 10–19 | 9 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 101 | 10–19 | 9 |
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.
Hooks and filters fired · 1
One hook fires while wp_sprintf() runs, in this order:
- apply_filters( wp_sprintf )filterline 5303 (+49 into the body)
Filters a fragment from the pattern passed to wp_sprintf().
Uses · 1
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 8
- get_the_taxonomies()Retrieves all taxonomies associated with a post.
- rest_find_one_matching_schema()Finds the matching schema among the "oneOf" schemas.
- rest_get_combining_operation_error()Gets the error of combining operation.
- rest_handle_multi_type_schema()Handles getting the best type for a multi-type schema.
- rest_sanitize_value_from_schema()Sanitize a value based on a schema.
- rest_validate_enum()Validates that the given value is a member of the JSON Schema "enum".
- rest_validate_value_from_schema()Validate a value based on a schema.
- wp_credits_section_list()Displays a list of contributors for a given group.
Source code
function wp_sprintf( $pattern, ...$args ) { $len = strlen( $pattern ); $start = 0; $result = ''; $arg_index = 0; while ( $len > $start ) { // Last character: append and break. if ( strlen( $pattern ) - 1 === $start ) { $result .= substr( $pattern, -1 ); break; } // Literal %: append and continue. if ( '%%' === substr( $pattern, $start, 2 ) ) { $start += 2; $result .= '%'; continue; } // Get fragment before next %. $end = strpos( $pattern, '%', $start + 1 ); if ( false === $end ) { $end = $len; } $fragment = substr( $pattern, $start, $end - $start ); // Fragment has a specifier. if ( '%' === $pattern[ $start ] ) { // Find numbered arguments or take the next one in order. if ( preg_match( '/^%(\d+)\$/', $fragment, $matches ) ) { $index = $matches[1] - 1; // 0-based array vs 1-based sprintf() arguments. $arg = isset( $args[ $index ] ) ? $args[ $index ] : ''; $fragment = str_replace( "%{$matches[1]}$", '%', $fragment ); } else { $arg = isset( $args[ $arg_index ] ) ? $args[ $arg_index ] : ''; ++$arg_index; } /** * Filters a fragment from the pattern passed to wp_sprintf(). * * If the fragment is unchanged, then sprintf() will be run on the fragment. * * @since 2.5.0 * * @param string $fragment A fragment from the pattern. * @param string $arg The argument. */ $_fragment = apply_filters( 'wp_sprintf', $fragment, $arg ); if ( $_fragment !== $fragment ) { $fragment = $_fragment; } else { $fragment = sprintf( $fragment, (string) $arg ); } } // Append to result and move to next fragment. $result .= $fragment; $start = $end; } return $result;}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.
...$args parameter by adding it to the function signature.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/formatting.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.