wp_json_encode

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

WordPress Version: 6.5

/**
 * Encodes a variable into JSON, with some confidence checks.
 *
 * @since 4.1.0
 * @since 5.3.0 No longer handles support for PHP < 5.6.
 * @since 6.5.0 The `$data` parameter has been renamed to `$value` and
 *              the `$options` parameter to `$flags` for parity with PHP.
 *
 * @param mixed $value Variable (usually an array or object) to encode as JSON.
 * @param int   $flags Optional. Options to be passed to json_encode(). Default 0.
 * @param int   $depth Optional. Maximum depth to walk through $value. Must be
 *                     greater than 0. Default 512.
 * @return string|false The JSON encoded string, or false if it cannot be encoded.
 */
function wp_json_encode($value, $flags = 0, $depth = 512)
{
    $json = json_encode($value, $flags, $depth);
    // If json_encode() was successful, no need to do more confidence checking.
    if (false !== $json) {
        return $json;
    }
    try {
        $value = _wp_json_sanity_check($value, $depth);
    } catch (Exception $e) {
        return false;
    }
    return json_encode($value, $flags, $depth);
}

WordPress Version: 6.1

/**
 * Encodes a variable into JSON, with some sanity checks.
 *
 * @since 4.1.0
 * @since 5.3.0 No longer handles support for PHP < 5.6.
 *
 * @param mixed $data    Variable (usually an array or object) to encode as JSON.
 * @param int   $options Optional. Options to be passed to json_encode(). Default 0.
 * @param int   $depth   Optional. Maximum depth to walk through $data. Must be
 *                       greater than 0. Default 512.
 * @return string|false The JSON encoded string, or false if it cannot be encoded.
 */
function wp_json_encode($data, $options = 0, $depth = 512)
{
    $json = json_encode($data, $options, $depth);
    // If json_encode() was successful, no need to do more sanity checking.
    if (false !== $json) {
        return $json;
    }
    try {
        $data = _wp_json_sanity_check($data, $depth);
    } catch (Exception $e) {
        return false;
    }
    return json_encode($data, $options, $depth);
}

WordPress Version: 5.3

/**
 * Encode a variable into JSON, with some sanity checks.
 *
 * @since 4.1.0
 * @since 5.3.0 No longer handles support for PHP < 5.6.
 *
 * @param mixed $data    Variable (usually an array or object) to encode as JSON.
 * @param int   $options Optional. Options to be passed to json_encode(). Default 0.
 * @param int   $depth   Optional. Maximum depth to walk through $data. Must be
 *                       greater than 0. Default 512.
 * @return string|false The JSON encoded string, or false if it cannot be encoded.
 */
function wp_json_encode($data, $options = 0, $depth = 512)
{
    $json = json_encode($data, $options, $depth);
    // If json_encode() was successful, no need to do more sanity checking.
    if (false !== $json) {
        return $json;
    }
    try {
        $data = _wp_json_sanity_check($data, $depth);
    } catch (Exception $e) {
        return false;
    }
    return json_encode($data, $options, $depth);
}

WordPress Version: 4.6

/**
 * Encode a variable into JSON, with some sanity checks.
 *
 * @since 4.1.0
 *
 * @param mixed $data    Variable (usually an array or object) to encode as JSON.
 * @param int   $options Optional. Options to be passed to json_encode(). Default 0.
 * @param int   $depth   Optional. Maximum depth to walk through $data. Must be
 *                       greater than 0. Default 512.
 * @return string|false The JSON encoded string, or false if it cannot be encoded.
 */
function wp_json_encode($data, $options = 0, $depth = 512)
{
    /*
     * json_encode() has had extra params added over the years.
     * $options was added in 5.3, and $depth in 5.5.
     * We need to make sure we call it with the correct arguments.
     */
    if (version_compare(PHP_VERSION, '5.5', '>=')) {
        $args = array($data, $options, $depth);
    } elseif (version_compare(PHP_VERSION, '5.3', '>=')) {
        $args = array($data, $options);
    } else {
        $args = array($data);
    }
    // Prepare the data for JSON serialization.
    $args[0] = _wp_json_prepare_data($data);
    $json = @call_user_func_array('json_encode', $args);
    // If json_encode() was successful, no need to do more sanity checking.
    // ... unless we're in an old version of PHP, and json_encode() returned
    // a string containing 'null'. Then we need to do more sanity checking.
    if (false !== $json && (version_compare(PHP_VERSION, '5.5', '>=') || false === strpos($json, 'null'))) {
        return $json;
    }
    try {
        $args[0] = _wp_json_sanity_check($data, $depth);
    } catch (Exception $e) {
        return false;
    }
    return call_user_func_array('json_encode', $args);
}

WordPress Version: 4.4

/**
 * Encode a variable into JSON, with some sanity checks.
 *
 * @since 4.1.0
 *
 * @param mixed $data    Variable (usually an array or object) to encode as JSON.
 * @param int   $options Optional. Options to be passed to json_encode(). Default 0.
 * @param int   $depth   Optional. Maximum depth to walk through $data. Must be
 *                       greater than 0. Default 512.
 * @return string|false The JSON encoded string, or false if it cannot be encoded.
 */
function wp_json_encode($data, $options = 0, $depth = 512)
{
    /*
     * json_encode() has had extra params added over the years.
     * $options was added in 5.3, and $depth in 5.5.
     * We need to make sure we call it with the correct arguments.
     */
    if (version_compare(PHP_VERSION, '5.5', '>=')) {
        $args = array($data, $options, $depth);
    } elseif (version_compare(PHP_VERSION, '5.3', '>=')) {
        $args = array($data, $options);
    } else {
        $args = array($data);
    }
    // Prepare the data for JSON serialization.
    $data = _wp_json_prepare_data($data);
    $json = @call_user_func_array('json_encode', $args);
    // If json_encode() was successful, no need to do more sanity checking.
    // ... unless we're in an old version of PHP, and json_encode() returned
    // a string containing 'null'. Then we need to do more sanity checking.
    if (false !== $json && (version_compare(PHP_VERSION, '5.5', '>=') || false === strpos($json, 'null'))) {
        return $json;
    }
    try {
        $args[0] = _wp_json_sanity_check($data, $depth);
    } catch (Exception $e) {
        return false;
    }
    return call_user_func_array('json_encode', $args);
}

WordPress Version: 4.3

/**
 * Encode a variable into JSON, with some sanity checks.
 *
 * @since 4.1.0
 *
 * @param mixed $data    Variable (usually an array or object) to encode as JSON.
 * @param int   $options Optional. Options to be passed to json_encode(). Default 0.
 * @param int   $depth   Optional. Maximum depth to walk through $data. Must be
 *                       greater than 0. Default 512.
 * @return string|false The JSON encoded string, or false if it cannot be encoded.
 */
function wp_json_encode($data, $options = 0, $depth = 512)
{
    /*
     * json_encode() has had extra params added over the years.
     * $options was added in 5.3, and $depth in 5.5.
     * We need to make sure we call it with the correct arguments.
     */
    if (version_compare(PHP_VERSION, '5.5', '>=')) {
        $args = array($data, $options, $depth);
    } elseif (version_compare(PHP_VERSION, '5.3', '>=')) {
        $args = array($data, $options);
    } else {
        $args = array($data);
    }
    $json = call_user_func_array('json_encode', $args);
    // If json_encode() was successful, no need to do more sanity checking.
    // ... unless we're in an old version of PHP, and json_encode() returned
    // a string containing 'null'. Then we need to do more sanity checking.
    if (false !== $json && (version_compare(PHP_VERSION, '5.5', '>=') || false === strpos($json, 'null'))) {
        return $json;
    }
    try {
        $args[0] = _wp_json_sanity_check($data, $depth);
    } catch (Exception $e) {
        return false;
    }
    return call_user_func_array('json_encode', $args);
}

WordPress Version: 4.1

/**
 * Encode a variable into JSON, with some sanity checks.
 *
 * @since 4.1.0
 *
 * @param mixed $data    Variable (usually an array or object) to encode as JSON.
 * @param int   $options Optional. Options to be passed to json_encode(). Default 0.
 * @param int   $depth   Optional. Maximum depth to walk through $data. Must be
 *                       greater than 0. Default 512.
 * @return bool|string The JSON encoded string, or false if it cannot be encoded.
 */
function wp_json_encode($data, $options = 0, $depth = 512)
{
    /*
     * json_encode() has had extra params added over the years.
     * $options was added in 5.3, and $depth in 5.5.
     * We need to make sure we call it with the correct arguments.
     */
    if (version_compare(PHP_VERSION, '5.5', '>=')) {
        $args = array($data, $options, $depth);
    } elseif (version_compare(PHP_VERSION, '5.3', '>=')) {
        $args = array($data, $options);
    } else {
        $args = array($data);
    }
    $json = call_user_func_array('json_encode', $args);
    // If json_encode() was successful, no need to do more sanity checking.
    // ... unless we're in an old version of PHP, and json_encode() returned
    // a string containing 'null'. Then we need to do more sanity checking.
    if (false !== $json && (version_compare(PHP_VERSION, '5.5', '>=') || false === strpos($json, 'null'))) {
        return $json;
    }
    try {
        $args[0] = _wp_json_sanity_check($data, $depth);
    } catch (Exception $e) {
        return false;
    }
    return call_user_func_array('json_encode', $args);
}