wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_json_encode › WordPress Function

Since4.1.0
Deprecatedn/a
wp_json_encode ( $data, $options = 0, $depth = 512 )
Parameters: (3)
  • (mixed) $data Variable (usually an array or object) to encode as JSON.
    Required: Yes
  • (int) $options Optional. Options to be passed to json_encode(). Default 0.
    Required: No
    Default:
  • (int) $depth Optional. Maximum depth to walk through $data. Must be greater than 0. Default 512.
    Required: No
    Default: 512
Returns:
  • (string|false) The JSON encoded string, or false if it cannot be encoded.
Defined at:
Codex:
Change Log:
  • 5.3.0

Encodes a variable into JSON, with some sanity checks.



Source

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