W3cubDocs

/WordPress

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

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

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_json_encode