wp_sprintf

The timeline below displays how wordpress function wp_sprintf has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.4

/**
 * WordPress' implementation of PHP sprintf() with filters.
 *
 * @since 2.5.0
 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
 *              by adding it to the function signature.
 *
 * @link https://www.php.net/sprintf
 *
 * @param string $pattern The string which formatted args are inserted.
 * @param mixed  ...$args Arguments to be formatted into the $pattern string.
 * @return string The formatted string.
 */
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;
}

WordPress Version: 6.3

/**
 * WordPress' implementation of PHP sprintf() with filters.
 *
 * @since 2.5.0
 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
 *              by adding it to the function signature.
 *
 * @link https://www.php.net/sprintf
 *
 * @param string $pattern The string which formatted args are inserted.
 * @param mixed  ...$args Arguments to be formatted into the $pattern string.
 * @return string The formatted string.
 */
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;
}

WordPress Version: 5.6

/**
 * WordPress implementation of PHP sprintf() with filters.
 *
 * @since 2.5.0
 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
 *              by adding it to the function signature.
 *
 * @link https://www.php.net/sprintf
 *
 * @param string $pattern The string which formatted args are inserted.
 * @param mixed  ...$args Arguments to be formatted into the $pattern string.
 * @return string The formatted string.
 */
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;
}

WordPress Version: 5.5

/**
 * WordPress implementation of PHP sprintf() with filters.
 *
 * @since 2.5.0
 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
 *              by adding it to the function signature.
 *
 * @link https://www.php.net/sprintf
 *
 * @param string $pattern The string which formatted args are inserted.
 * @param mixed  ...$args Arguments to be formatted into the $pattern string.
 * @return string The formatted string.
 */
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, strval($arg));
            }
        }
        // Append to result and move to next fragment.
        $result .= $fragment;
        $start = $end;
    }
    return $result;
}

WordPress Version: 5.4

/**
 * WordPress implementation of PHP sprintf() with filters.
 *
 * @since 2.5.0
 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
 *              by adding it to the function signature.
 *
 * @link https://www.php.net/sprintf
 *
 * @param string $pattern The string which formatted args are inserted.
 * @param mixed  ...$args Arguments to be formatted into the $pattern string.
 * @return string The formatted string.
 */
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, strval($arg));
            }
        }
        // Append to result and move to next fragment.
        $result .= $fragment;
        $start = $end;
    }
    return $result;
}

WordPress Version: 5.3

/**
 * WordPress implementation of PHP sprintf() with filters.
 *
 * @since 2.5.0
 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
 *              by adding it to the function signature.
 *
 * @link https://secure.php.net/sprintf
 *
 * @param string $pattern The string which formatted args are inserted.
 * @param mixed  ...$args Arguments to be formatted into the $pattern string.
 * @return string The formatted string.
 */
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, strval($arg));
            }
        }
        // Append to result and move to next fragment
        $result .= $fragment;
        $start = $end;
    }
    return $result;
}

WordPress Version: 4.6

/**
 * WordPress implementation of PHP sprintf() with filters.
 *
 * @since 2.5.0
 * @link https://secure.php.net/sprintf
 *
 * @param string $pattern   The string which formatted args are inserted.
 * @param mixed  $args ,... Arguments to be formatted into the $pattern string.
 * @return string The formatted string.
 */
function wp_sprintf($pattern)
{
    $args = func_get_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)) {
                $arg = isset($args[$matches[1]]) ? $args[$matches[1]] : '';
                $fragment = str_replace("%{$matches[1]}\$", '%', $fragment);
            } else {
                ++$arg_index;
                $arg = isset($args[$arg_index]) ? $args[$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, strval($arg));
            }
        }
        // Append to result and move to next fragment
        $result .= $fragment;
        $start = $end;
    }
    return $result;
}

WordPress Version: 4.3

/**
 * WordPress implementation of PHP sprintf() with filters.
 *
 * @since 2.5.0
 * @link http://www.php.net/sprintf
 *
 * @param string $pattern   The string which formatted args are inserted.
 * @param mixed  $args ,... Arguments to be formatted into the $pattern string.
 * @return string The formatted string.
 */
function wp_sprintf($pattern)
{
    $args = func_get_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)) {
                $arg = isset($args[$matches[1]]) ? $args[$matches[1]] : '';
                $fragment = str_replace("%{$matches[1]}\$", '%', $fragment);
            } else {
                ++$arg_index;
                $arg = isset($args[$arg_index]) ? $args[$arg_index] : '';
            }
            /**
             * Filter 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, strval($arg));
            }
        }
        // Append to result and move to next fragment
        $result .= $fragment;
        $start = $end;
    }
    return $result;
}

WordPress Version: 4.2

/**
 * WordPress implementation of PHP sprintf() with filters.
 *
 * @since 2.5.0
 * @link http://www.php.net/sprintf
 *
 * @param string $pattern The string which formatted args are inserted.
 * @param mixed  $args ,... Arguments to be formatted into the $pattern string.
 * @return string The formatted string.
 */
function wp_sprintf($pattern)
{
    $args = func_get_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)) {
                $arg = isset($args[$matches[1]]) ? $args[$matches[1]] : '';
                $fragment = str_replace("%{$matches[1]}\$", '%', $fragment);
            } else {
                ++$arg_index;
                $arg = isset($args[$arg_index]) ? $args[$arg_index] : '';
            }
            /**
             * Filter 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, strval($arg));
            }
        }
        // Append to result and move to next fragment
        $result .= $fragment;
        $start = $end;
    }
    return $result;
}

WordPress Version: 3.8

/**
 * WordPress implementation of PHP sprintf() with filters.
 *
 * @since 2.5.0
 * @link http://www.php.net/sprintf
 *
 * @param string $pattern The string which formatted args are inserted.
 * @param mixed $args,... Arguments to be formatted into the $pattern string.
 * @return string The formatted string.
 */
function wp_sprintf($pattern)
{
    $args = func_get_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)) {
                $arg = isset($args[$matches[1]]) ? $args[$matches[1]] : '';
                $fragment = str_replace("%{$matches[1]}\$", '%', $fragment);
            } else {
                ++$arg_index;
                $arg = isset($args[$arg_index]) ? $args[$arg_index] : '';
            }
            /**
             * Filter 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, strval($arg));
            }
        }
        // Append to result and move to next fragment
        $result .= $fragment;
        $start = $end;
    }
    return $result;
}

WordPress Version: 3.7

/**
 * WordPress implementation of PHP sprintf() with filters.
 *
 * @since 2.5.0
 * @link http://www.php.net/sprintf
 *
 * @param string $pattern The string which formatted args are inserted.
 * @param mixed $args,... Arguments to be formatted into the $pattern string.
 * @return string The formatted string.
 */
function wp_sprintf($pattern)
{
    $args = func_get_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)) {
                $arg = isset($args[$matches[1]]) ? $args[$matches[1]] : '';
                $fragment = str_replace("%{$matches[1]}\$", '%', $fragment);
            } else {
                ++$arg_index;
                $arg = isset($args[$arg_index]) ? $args[$arg_index] : '';
            }
            // Apply filters OR sprintf
            $_fragment = apply_filters('wp_sprintf', $fragment, $arg);
            if ($_fragment != $fragment) {
                $fragment = $_fragment;
            } else {
                $fragment = sprintf($fragment, strval($arg));
            }
        }
        // Append to result and move to next fragment
        $result .= $fragment;
        $start = $end;
    }
    return $result;
}