Encodes a variable into JSON, with some confidence checks.
$valuemixedrequired
$flagsintoptional
$depthintoptional
Default:512
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 );
}
© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_json_encode