wp_json_encode() WordPress Function

The wp_json_encode() function is used to encode a value to JSON format. This function is useful for values that need to be stored in JSON format, such as in the WordPress database.

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


Top ↑

Return

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


Top ↑

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
VersionDescription
5.3.0No longer handles support for PHP < 5.6.
4.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More