wp_json_encode( mixed $value, int $flags, int $depth = 512 ): string|false

Encodes a variable into JSON, with some confidence checks.

Parameters

$valuemixedrequired
Variable (usually an array or object) to encode as JSON.
$flagsintoptional
Options to be passed to json_encode(). Default 0.
$depthintoptional
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.

Source

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 );
}

Changelog

VersionDescription
6.5.0The $data parameter has been renamed to $value and the $options parameter to $flags for parity with PHP.
5.3.0No longer handles support for PHP < 5.6.
4.1.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.