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

Encode a variable into JSON, with some sanity checks.

Description Description

Parameters Parameters


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


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


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

Default value: 512

Top ↑

Return Return

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

Top ↑

Source Source

File: wp-includes/functions.php

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

Top ↑

Changelog Changelog

Version Description
5.3.0 No longer handles support for PHP < 5.6.
4.1.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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