# wp_sprintf()

URL: https://chugunov.pro/api-wordpress/functions/wp_sprintf/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 2.5.0.

## Сигнатура

```php
wp_sprintf( string $pattern, mixed $args ): string
```

## Описание

Реализация PHP-функции sprintf() в WordPress с фильтрами.

## Параметры

- `$pattern` `string` — обязательный. Строка, в которую вставляются форматированные аргументы.
- `$args` `mixed` — обязательный. Аргументы для форматирования в строку $pattern.

## Возвращаемое значение

`string`

## Исходный код

Файл: `wp-includes/formatting.php:5245`

```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      = $args[ $index ] ?? '';
				$fragment = str_replace( "%{$matches[1]}$", '%', $fragment );
			} else {
				$arg = $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;
}
```

## История изменений

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

## Связанные

Использует: [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/).
Используется в: [`rest_validate_enum`](https://chugunov.pro/api-wordpress/functions/rest_validate_enum/), [`rest_get_combining_operation_error`](https://chugunov.pro/api-wordpress/functions/rest_get_combining_operation_error/), [`rest_find_one_matching_schema`](https://chugunov.pro/api-wordpress/functions/rest_find_one_matching_schema/), [`rest_handle_multi_type_schema`](https://chugunov.pro/api-wordpress/functions/rest_handle_multi_type_schema/), [`wp_credits_section_list`](https://chugunov.pro/api-wordpress/functions/wp_credits_section_list/), [`rest_sanitize_value_from_schema`](https://chugunov.pro/api-wordpress/functions/rest_sanitize_value_from_schema/), [`rest_validate_value_from_schema`](https://chugunov.pro/api-wordpress/functions/rest_validate_value_from_schema/), [`get_the_taxonomies`](https://chugunov.pro/api-wordpress/functions/get_the_taxonomies/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/wp_sprintf/
