_wp_json_convert_string

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

WordPress Version: 6.2

/**
 * Converts a string to UTF-8, so that it can be safely encoded to JSON.
 *
 * @ignore
 * @since 4.1.0
 * @access private
 *
 * @see _wp_json_sanity_check()
 *
 * @param string $input_string The string which is to be converted.
 * @return string The checked string.
 */
function _wp_json_convert_string($input_string)
{
    static $use_mb = null;
    if (is_null($use_mb)) {
        $use_mb = function_exists('mb_convert_encoding');
    }
    if ($use_mb) {
        $encoding = mb_detect_encoding($input_string, mb_detect_order(), true);
        if ($encoding) {
            return mb_convert_encoding($input_string, 'UTF-8', $encoding);
        } else {
            return mb_convert_encoding($input_string, 'UTF-8', 'UTF-8');
        }
    } else {
        return wp_check_invalid_utf8($input_string, true);
    }
}

WordPress Version: 6.1

/**
 * Converts a string to UTF-8, so that it can be safely encoded to JSON.
 *
 * @ignore
 * @since 4.1.0
 * @access private
 *
 * @see _wp_json_sanity_check()
 *
 * @param string $string The string which is to be converted.
 * @return string The checked string.
 */
function _wp_json_convert_string($string)
{
    static $use_mb = null;
    if (is_null($use_mb)) {
        $use_mb = function_exists('mb_convert_encoding');
    }
    if ($use_mb) {
        $encoding = mb_detect_encoding($string, mb_detect_order(), true);
        if ($encoding) {
            return mb_convert_encoding($string, 'UTF-8', $encoding);
        } else {
            return mb_convert_encoding($string, 'UTF-8', 'UTF-8');
        }
    } else {
        return wp_check_invalid_utf8($string, true);
    }
}

WordPress Version: 5.5

/**
 * Convert a string to UTF-8, so that it can be safely encoded to JSON.
 *
 * @ignore
 * @since 4.1.0
 * @access private
 *
 * @see _wp_json_sanity_check()
 *
 * @param string $string The string which is to be converted.
 * @return string The checked string.
 */
function _wp_json_convert_string($string)
{
    static $use_mb = null;
    if (is_null($use_mb)) {
        $use_mb = function_exists('mb_convert_encoding');
    }
    if ($use_mb) {
        $encoding = mb_detect_encoding($string, mb_detect_order(), true);
        if ($encoding) {
            return mb_convert_encoding($string, 'UTF-8', $encoding);
        } else {
            return mb_convert_encoding($string, 'UTF-8', 'UTF-8');
        }
    } else {
        return wp_check_invalid_utf8($string, true);
    }
}

WordPress Version: 4.3

/**
 * Convert a string to UTF-8, so that it can be safely encoded to JSON.
 *
 * @ignore
 * @since 4.1.0
 * @access private
 *
 * @see _wp_json_sanity_check()
 *
 * @staticvar bool $use_mb
 *
 * @param string $string The string which is to be converted.
 * @return string The checked string.
 */
function _wp_json_convert_string($string)
{
    static $use_mb = null;
    if (is_null($use_mb)) {
        $use_mb = function_exists('mb_convert_encoding');
    }
    if ($use_mb) {
        $encoding = mb_detect_encoding($string, mb_detect_order(), true);
        if ($encoding) {
            return mb_convert_encoding($string, 'UTF-8', $encoding);
        } else {
            return mb_convert_encoding($string, 'UTF-8', 'UTF-8');
        }
    } else {
        return wp_check_invalid_utf8($string, true);
    }
}

WordPress Version: 4.2

/**
 * Convert a string to UTF-8, so that it can be safely encoded to JSON.
 *
 * @ignore
 * @since 4.1.0
 * @access private
 *
 * @see _wp_json_sanity_check()
 *
 * @param string $string The string which is to be converted.
 * @return string The checked string.
 */
function _wp_json_convert_string($string)
{
    static $use_mb = null;
    if (is_null($use_mb)) {
        $use_mb = function_exists('mb_convert_encoding');
    }
    if ($use_mb) {
        $encoding = mb_detect_encoding($string, mb_detect_order(), true);
        if ($encoding) {
            return mb_convert_encoding($string, 'UTF-8', $encoding);
        } else {
            return mb_convert_encoding($string, 'UTF-8', 'UTF-8');
        }
    } else {
        return wp_check_invalid_utf8($string, true);
    }
}

WordPress Version: 4.1

/**
 * Convert a string to UTF-8, so that it can be safely encoded to JSON.
 *
 * @see _wp_json_sanity_check()
 *
 * @since 4.1.0
 * @access private
 * @internal
 *
 * @param string $string The string which is to be converted.
 * @return string The checked string.
 */
function _wp_json_convert_string($string)
{
    static $use_mb = null;
    if (is_null($use_mb)) {
        $use_mb = function_exists('mb_convert_encoding');
    }
    if ($use_mb) {
        $encoding = mb_detect_encoding($string, mb_detect_order(), true);
        if ($encoding) {
            return mb_convert_encoding($string, 'UTF-8', $encoding);
        } else {
            return mb_convert_encoding($string, 'UTF-8', 'UTF-8');
        }
    } else {
        return wp_check_invalid_utf8($string, true);
    }
}