: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @param string $message Error message.
* @param string $title Optional. Error title (unused). Default empty string.
* @param string|array $args Optional. Arguments to control behavior. Default empty array.
function _ajax_wp_die_handler( $message, $title = '', $args = array() ) {
// Set default 'response' to 200 for Ajax requests.
array( 'response' => 200 )
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args );
if ( ! headers_sent() ) {
// This is intentional. For backward-compatibility, support passing null here.
if ( null !== $args['response'] ) {
status_header( $parsed_args['response'] );
if ( is_scalar( $message ) ) {
$message = (string) $message;
if ( $parsed_args['exit'] ) {
* Kills WordPress execution and displays JSON response with an error message.
* This is the handler for wp_die() when processing JSON requests.
* @param string $message Error message.
* @param string $title Optional. Error title. Default empty string.
* @param string|array $args Optional. Arguments to control behavior. Default empty array.
function _json_wp_die_handler( $message, $title = '', $args = array() ) {
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args );
'code' => $parsed_args['code'],
'status' => $parsed_args['response'],
'additional_errors' => $parsed_args['additional_errors'],
if ( isset( $parsed_args['error_data'] ) ) {
$data['data']['error'] = $parsed_args['error_data'];
if ( ! headers_sent() ) {
header( "Content-Type: application/json; charset={$parsed_args['charset']}" );
if ( null !== $parsed_args['response'] ) {
status_header( $parsed_args['response'] );
echo wp_json_encode( $data );
if ( $parsed_args['exit'] ) {
* Kills WordPress execution and displays JSONP response with an error message.
* This is the handler for wp_die() when processing JSONP requests.
* @param string $message Error message.
* @param string $title Optional. Error title. Default empty string.
* @param string|array $args Optional. Arguments to control behavior. Default empty array.
function _jsonp_wp_die_handler( $message, $title = '', $args = array() ) {
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args );
'code' => $parsed_args['code'],
'status' => $parsed_args['response'],
'additional_errors' => $parsed_args['additional_errors'],
if ( isset( $parsed_args['error_data'] ) ) {
$data['data']['error'] = $parsed_args['error_data'];
if ( ! headers_sent() ) {
header( "Content-Type: application/javascript; charset={$parsed_args['charset']}" );
header( 'X-Content-Type-Options: nosniff' );
header( 'X-Robots-Tag: noindex' );
if ( null !== $parsed_args['response'] ) {
status_header( $parsed_args['response'] );
$result = wp_json_encode( $data );
$jsonp_callback = $_GET['_jsonp'];
echo '/**/' . $jsonp_callback . '(' . $result . ')';
if ( $parsed_args['exit'] ) {
* Kills WordPress execution and displays XML response with an error message.
* This is the handler for wp_die() when processing XMLRPC requests.
* @global wp_xmlrpc_server $wp_xmlrpc_server
* @param string $message Error message.
* @param string $title Optional. Error title. Default empty string.
* @param string|array $args Optional. Arguments to control behavior. Default empty array.
function _xmlrpc_wp_die_handler( $message, $title = '', $args = array() ) {
global $wp_xmlrpc_server;
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args );
if ( ! headers_sent() ) {
if ( $wp_xmlrpc_server ) {
$error = new IXR_Error( $parsed_args['response'], $message );
$wp_xmlrpc_server->output( $error->getXml() );
if ( $parsed_args['exit'] ) {
* Kills WordPress execution and displays XML response with an error message.
* This is the handler for wp_die() when processing XML requests.
* @param string $message Error message.
* @param string $title Optional. Error title. Default empty string.
* @param string|array $args Optional. Arguments to control behavior. Default empty array.
function _xml_wp_die_handler( $message, $title = '', $args = array() ) {
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args );
$message = htmlspecialchars( $message );
$title = htmlspecialchars( $title );
<code>{$parsed_args['code']}</code>
<title><![CDATA[{$title}]]></title>
<message><![CDATA[{$message}]]></message>
<status>{$parsed_args['response']}</status>
if ( ! headers_sent() ) {
header( "Content-Type: text/xml; charset={$parsed_args['charset']}" );
if ( null !== $parsed_args['response'] ) {
status_header( $parsed_args['response'] );
if ( $parsed_args['exit'] ) {
* Kills WordPress execution and displays an error message.
* This is the handler for wp_die() when processing APP requests.
* @since 5.1.0 Added the $title and $args parameters.
* @param string $message Optional. Response to print. Default empty string.
* @param string $title Optional. Error title (unused). Default empty string.
* @param string|array $args Optional. Arguments to control behavior. Default empty array.
function _scalar_wp_die_handler( $message = '', $title = '', $args = array() ) {
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args );
if ( $parsed_args['exit'] ) {
if ( is_scalar( $message ) ) {
die( (string) $message );
if ( is_scalar( $message ) ) {
* Processes arguments passed to wp_die() consistently for its handlers.
* @param string|WP_Error $message Error message or WP_Error object.
* @param string $title Optional. Error title. Default empty string.
* @param string|array $args Optional. Arguments to control behavior. Default empty array.
* @type string $0 Error message.
* @type string $1 Error title.
* @type array $2 Arguments to control behavior.
function _wp_die_process_input( $message, $title = '', $args = array() ) {
'additional_errors' => array(),
$args = wp_parse_args( $args, $defaults );
if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) {
if ( ! empty( $message->errors ) ) {
foreach ( (array) $message->errors as $error_code => $error_messages ) {
foreach ( (array) $error_messages as $error_message ) {
'message' => $error_message,
'data' => $message->get_error_data( $error_code ),
$message = $errors[0]['message'];
if ( empty( $args['code'] ) ) {
$args['code'] = $errors[0]['code'];
if ( empty( $args['response'] ) && is_array( $errors[0]['data'] ) && ! empty( $errors[0]['data']['status'] ) ) {
$args['response'] = $errors[0]['data']['status'];
if ( empty( $title ) && is_array( $errors[0]['data'] ) && ! empty( $errors[0]['data']['title'] ) ) {
$title = $errors[0]['data']['title'];
if ( WP_DEBUG_DISPLAY && is_array( $errors[0]['data'] ) && ! empty( $errors[0]['data']['error'] ) ) {
$args['error_data'] = $errors[0]['data']['error'];
$args['additional_errors'] = array_values( $errors );
$have_gettext = function_exists( '__' );
// The $title and these specific $args must always have a non-empty value.
if ( empty( $args['code'] ) ) {
$args['code'] = 'wp_die';
if ( empty( $args['response'] ) ) {
$title = $have_gettext ? __( 'WordPress › Error' ) : 'WordPress › Error';
if ( empty( $args['text_direction'] ) || ! in_array( $args['text_direction'], array( 'ltr', 'rtl' ), true ) ) {
$args['text_direction'] = 'ltr';
if ( function_exists( 'is_rtl' ) && is_rtl() ) {
$args['text_direction'] = 'rtl';
if ( ! empty( $args['charset'] ) ) {
$args['charset'] = _canonical_charset( $args['charset'] );
return array( $message, $title, $args );
* Encodes a variable into JSON, with some confidence checks.
* @since 5.3.0 No longer handles support for PHP < 5.6.
* @since 6.5.0 The `$data` parameter has been renamed to `$value` and
* the `$options` parameter to `$flags` for parity with PHP.
* @param mixed $value Variable (usually an array or object) to encode as JSON.
* @param int $flags Optional. Options to be passed to json_encode(). Default 0.
* @param int $depth Optional. 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.
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.
$value = _wp_json_sanity_check( $value, $depth );
} catch ( Exception $e ) {
return json_encode( $value, $flags, $depth );
* Performs confidence checks on data that shall be encoded to JSON.
* @throws Exception If depth limit is reached.
* @param mixed $value Variable (usually an array or object) to encode as JSON.
* @param int $depth Maximum depth to walk through $value. Must be greater than 0.
* @return mixed The sanitized data that shall be encoded to JSON.
function _wp_json_sanity_check( $value, $depth ) {
throw new Exception( 'Reached depth limit' );
if ( is_array( $value ) ) {
foreach ( $value as $id => $el ) {
// Don't forget to sanitize the ID!
if ( is_string( $id ) ) {
$clean_id = _wp_json_convert_string( $id );
// Check the element type, so that we're only recursing if we really have to.
if ( is_array( $el ) || is_object( $el ) ) {
$output[ $clean_id ] = _wp_json_sanity_check( $el, $depth - 1 );
} elseif ( is_string( $el ) ) {
$output[ $clean_id ] = _wp_json_convert_string( $el );
$output[ $clean_id ] = $el;
} elseif ( is_object( $value ) ) {
$output = new stdClass();
foreach ( $value as $id => $el ) {
if ( is_string( $id ) ) {
$clean_id = _wp_json_convert_string( $id );
if ( is_array( $el ) || is_object( $el ) ) {
$output->$clean_id = _wp_json_sanity_check( $el, $depth - 1 );
} elseif ( is_string( $el ) ) {
$output->$clean_id = _wp_json_convert_string( $el );
$output->$clean_id = $el;
} elseif ( is_string( $value ) ) {
return _wp_json_convert_string( $value );
* Converts a string to UTF-8, so that it can be safely encoded to JSON.
* @see _wp_json_sanity_check()
* @param string $input_string The string which is to be converted.
* @return string The checked string.
function _wp_json_convert_string( $input_string ) {
if ( is_null( $use_mb ) ) {
$use_mb = function_exists( 'mb_convert_encoding' );
$encoding = mb_detect_encoding( $input_string, mb_detect_order(), true );
return mb_convert_encoding( $input_string, 'UTF-8', $encoding );
return mb_convert_encoding( $input_string, 'UTF-8', 'UTF-8' );
return wp_check_invalid_utf8( $input_string, true );
* Prepares response data to be serialized to JSON.
* This supports the JsonSerializable interface for PHP 5.2-5.3 as well.
* @deprecated 5.3.0 This function is no longer needed as support for PHP 5.2-5.3
* @param mixed $value Native representation.
* @return bool|int|float|null|string|array Data ready for `json_encode()`.
function _wp_json_prepare_data( $value ) {
_deprecated_function( __FUNCTION__, '5.3.0' );
* Sends a JSON response back to an Ajax request.
* @since 4.7.0 The `$status_code` parameter was added.
* @since 5.6.0 The `$flags` parameter was added.
* @param mixed $response Variable (usually an array or object) to encode as JSON,
* @param int $status_code Optional. The HTTP status code to output. Default null.
* @param int $flags Optional. Options to be passed to json_encode(). Default 0.
function wp_send_json( $response, $status_code = null, $flags = 0 ) {
if ( wp_is_serving_rest_request() ) {
/* translators: 1: WP_REST_Response, 2: WP_Error */
__( 'Return a %1$s or %2$s object from your callback when using the REST API.' ),
if ( ! headers_sent() ) {
header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
if ( null !== $status_code ) {
status_header( $status_code );
echo wp_json_encode( $response, $flags );