@@ -2975,6 +2975,15 @@ function wp_die( $message = '', $title = '', $args = array() ) {
2975
2975
* @param callable $function Callback function name.
2976
2976
*/
2977
2977
$ 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 ' );
2978
2987
} elseif ( defined ( 'XMLRPC_REQUEST ' ) && XMLRPC_REQUEST ) {
2979
2988
/**
2980
2989
* Filters the callback for killing WordPress execution for XML-RPC requests.
@@ -3215,6 +3224,40 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) {
3215
3224
die ();
3216
3225
}
3217
3226
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
+
3218
3261
/**
3219
3262
* Kill WordPress execution and display XML message with error message.
3220
3263
*
0 commit comments