wp_sprintf() WordPress Function

The wp_sprintf() function is a tool for generating strings with placeholders that can be replaced with variables at runtime. It is similar to the standard PHP sprintf() function, but has a few important differences. One difference is that the wp_sprintf() function supports named placeholders, which makes it easier to keep track of which variable goes where. Another difference is that the wp_sprintf() function automatically encodes and escapes HTML entities, which makes it safer to use with data that comes from untrusted sources. The wp_sprintf() function is useful for generating dynamic content that needs to be safe for display on a web page. For example, you could use it to generate a list of recent posts with links to each one. To use the wp_sprintf() function, you need to specify a format string and an array of variables. The format string can contain any combination of literals and placeholders. Placeholders are denoted by a percent sign (%) followed by a letter or number. The variables in the array will be substituted into the placeholders in the format string. The order of the variables in the array is important, and corresponds to the order of the placeholders in the format string. If you want to use a literal percent sign (%) in the format string, you need to escape it with another percent sign (%). The wp_sprintf() function returns the formatted string.

wp_sprintf( string $pattern, mixed $args ) #

WordPress implementation of PHP sprintf() with filters.


Parameters

$pattern

(string)(Required)The string which formatted args are inserted.

$args

(mixed)(Required)Arguments to be formatted into the $pattern string.


Top ↑

Return

(string) The formatted string.


Top ↑

Source

File: wp-includes/formatting.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;
}


Top ↑

Changelog

Changelog
VersionDescription
5.3.0Formalized the existing and already documented ...$args parameter by adding it to the function signature.
2.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More