wp_json_encode( mixed $data, int $options, int $depth = 512 )

Encode a variable into JSON, with some sanity checks.


Parameters

$data

(mixed) (Required) Variable (usually an array or object) to encode as JSON.

$options

(int) (Optional) Options to be passed to json_encode(). Default 0.

$depth

(int) (Optional) Maximum depth to walk through $data. Must be greater than 0.

Default value: 512


Return

(string|false) The JSON encoded string, or false if it cannot be encoded.


Source

File: wp-includes/functions.php

function wp_json_encode( $data, $options = 0, $depth = 512 ) {
	$args = array( $data, $options, $depth );

	// 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.
	if ( false !== $json )  {
		return $json;
	}

	try {
		$args[0] = _wp_json_sanity_check( $data, $depth );
	} catch ( Exception $e ) {
		return false;
	}

	return call_user_func_array( 'json_encode', $args );
}


Changelog

Changelog
Version Description
WP-4.1.0 Introduced.