Skip to content

Commit ea43e5d

Browse files
author
flixos90
committed
Bootstrap/Load: Add support for JSON requests to wp_die().
In addition to AJAX and XML-RPC requests, `wp_die()` now handles JSON requests correctly, returning information in the expected content type. Props spacedmonkey. See #45933, #44458. Built from https://develop.svn.wordpress.org/trunk@44625 git-svn-id: http://core.svn.wordpress.org/trunk@44456 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent 22c1c45 commit ea43e5d

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

wp-includes/functions.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2975,6 +2975,15 @@ function wp_die( $message = '', $title = '', $args = array() ) {
29752975
* @param callable $function Callback function name.
29762976
*/
29772977
$function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' );
2978+
} elseif ( wp_is_json_request() ) {
2979+
/**
2980+
* Filters the callback for killing WordPress execution for JSON requests.
2981+
*
2982+
* @since 5.1.0
2983+
*
2984+
* @param callable $function Callback function name.
2985+
*/
2986+
$function = apply_filters( 'wp_die_json_handler', '_json_wp_die_handler' );
29782987
} elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
29792988
/**
29802989
* Filters the callback for killing WordPress execution for XML-RPC requests.
@@ -3215,6 +3224,40 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) {
32153224
die();
32163225
}
32173226

3227+
/**
3228+
* Kill WordPress execution and display JSON message with error message.
3229+
*
3230+
* This is the handler for wp_die when processing JSON requests.
3231+
*
3232+
* @since 5.1.0
3233+
* @access private
3234+
*
3235+
* @param string $message Error message.
3236+
* @param string $title Optional. Error title. Default empty.
3237+
* @param string|array $args Optional. Arguments to control behavior. Default empty array.
3238+
*/
3239+
function _json_wp_die_handler( $message, $title = '', $args = array() ) {
3240+
$defaults = array( 'response' => 500 );
3241+
3242+
$r = wp_parse_args( $args, $defaults );
3243+
3244+
$data = array(
3245+
'code' => 'wp_die',
3246+
'message' => $message,
3247+
'status' => $r['response'],
3248+
);
3249+
3250+
if ( ! headers_sent() ) {
3251+
header( 'Content-Type: application/json; charset=utf-8' );
3252+
if ( null !== $r['response'] ) {
3253+
status_header( $r['response'] );
3254+
}
3255+
}
3256+
3257+
echo wp_json_encode( $data );
3258+
die();
3259+
}
3260+
32183261
/**
32193262
* Kill WordPress execution and display XML message with error message.
32203263
*

wp-includes/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @global string $wp_version
1515
*/
16-
$wp_version = '5.1-beta1-44624';
16+
$wp_version = '5.1-beta1-44625';
1717

1818
/**
1919
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)