Title: wp_sprintf
Published: April 25, 2014
Last modified: February 24, 2026

---

# wp_sprintf( string $pattern, mixed $args ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#wp--skip-link--target)

WordPress’ implementation of PHP sprintf() with filters.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#parameters)󠁿

 `$pattern`stringrequired

The string which formatted args are inserted.

`$args`mixedrequired

Arguments to be formatted into the $pattern string.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#return)󠁿

 string The formatted string.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#source)󠁿

    ```php
    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;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/formatting.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/formatting.php#L5254)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/formatting.php#L5254-L5318)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#hooks)󠁿

 [apply_filters( ‘wp_sprintf’, string $fragment, string $arg )](https://developer.wordpress.org/reference/hooks/wp_sprintf/)

Filters a fragment from the pattern passed to [wp_sprintf()](https://developer.wordpress.org/reference/functions/wp_sprintf/).

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#related)󠁿

| Uses | Description | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

| Used by | Description | 
| [rest_validate_enum()](https://developer.wordpress.org/reference/functions/rest_validate_enum/)`wp-includes/rest-api.php` |

Validates that the given value is a member of the JSON Schema “enum”.

  | 
| [rest_get_combining_operation_error()](https://developer.wordpress.org/reference/functions/rest_get_combining_operation_error/)`wp-includes/rest-api.php` |

Gets the error of combining operation.

  | 
| [rest_find_one_matching_schema()](https://developer.wordpress.org/reference/functions/rest_find_one_matching_schema/)`wp-includes/rest-api.php` |

Finds the matching schema among the “oneOf” schemas.

  | 
| [rest_handle_multi_type_schema()](https://developer.wordpress.org/reference/functions/rest_handle_multi_type_schema/)`wp-includes/rest-api.php` |

Handles getting the best type for a multi-type schema.

  | 
| [wp_credits_section_list()](https://developer.wordpress.org/reference/functions/wp_credits_section_list/)`wp-admin/includes/credits.php` |

Displays a list of contributors for a given group.

  | 
| [rest_sanitize_value_from_schema()](https://developer.wordpress.org/reference/functions/rest_sanitize_value_from_schema/)`wp-includes/rest-api.php` |

Sanitize a value based on a schema.

  | 
| [rest_validate_value_from_schema()](https://developer.wordpress.org/reference/functions/rest_validate_value_from_schema/)`wp-includes/rest-api.php` |

Validate a value based on a schema.

  | 
| [get_the_taxonomies()](https://developer.wordpress.org/reference/functions/get_the_taxonomies/)`wp-includes/taxonomy.php` |

Retrieves all taxonomies associated with a post.

  |

[Show 3 more](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.3.0](https://developer.wordpress.org/reference/since/5.3.0/) | Formalized the existing and already documented `...$args` parameter by adding it to the function signature. | 
| [2.5.0](https://developer.wordpress.org/reference/since/2.5.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/wp_sprintf/?output_format=md#comment-content-105)
 2.   [viper007bond](https://profiles.wordpress.org/viper007bond/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/wp_sprintf/#comment-105)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_sprintf%2F%23comment-105)
    Vote results for this note: 9[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_sprintf%2F%23comment-105)
 4. By default, `wp_sprintf_l()` is hooked into this function and adds the `%l` type
    which turns an array into a comma separated list. For example this code:
 5.     ```php
        wp_sprintf( '%s: %l', __( 'Some cool numbers' ), array( 1, 5, 10, 15 ) );
        ```
    
 6. Turns into this:
 7. > Some cool numbers: 1, 5, 10, and 15
 8.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_sprintf%2F%3Freplytocom%3D105%23feedback-editor-105)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_sprintf%2F)
before being able to contribute a note or feedback.