wp_send_json_error( mixed $data = null, int $status_code = null )
Send a JSON response back to an Ajax request, indicating failure.
Description
If the $data
parameter is a WP_Error object, the errors within the object are processed and output as an array of error codes and corresponding messages. All other types are output without further processing.
Parameters
- $data
-
(Optional) Data to encode as JSON, then print and die.
Default value: null
- $status_code
-
(Optional) The HTTP status code to output.
Default value: null
Source
File: wp-includes/functions.php
function wp_send_json_error( $data = null, $status_code = null ) {
$response = array( 'success' => false );
if ( isset( $data ) ) {
if ( is_wp_error( $data ) ) {
$result = array();
foreach ( $data->errors as $code => $messages ) {
foreach ( $messages as $message ) {
$result[] = array( 'code' => $code, 'message' => $message );
}
}
$response['data'] = $result;
} else {
$response['data'] = $data;
}
}
wp_send_json( $response, $status_code );
}
Related
Uses
Uses | Description |
---|---|
wp-includes/load.php: is_wp_error() |
Check whether variable is a ClassicPress Error. |
wp-includes/functions.php: wp_send_json() |
Send a JSON response back to an Ajax request. |
Used By
Used By | Description |
---|---|
wp-includes/deprecated.php: wp_ajax_press_this_save_post() |
Ajax handler for saving a post from Press This. |
wp-includes/deprecated.php: wp_ajax_press_this_add_category() |
Ajax handler for creating new category from Press This. |
wp-includes/customize/class-wp-customize-selective-refresh.php: WP_Customize_Selective_Refresh::handle_render_partials_request() |
Handles the Ajax request to return the rendered partials for the requested placements. |
wp-includes/class-wp-customize-widgets.php: WP_Customize_Widgets::wp_ajax_update_widget() |
Updates widget settings asynchronously. |
wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::handle_override_changeset_lock_request() |
Removes changeset lock when take over request is sent via Ajax. |
wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::refresh_nonces() |
Refresh nonces for the current preview. |
wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::handle_dismiss_autosave_or_lock_request() |
Delete a given auto-draft changeset or the autosave revision for a given changeset or delete changeset lock. |
wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::handle_load_themes_request() |
Load themes into the theme browsing/installation UI. |
wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::save() |
Handle customize_save WP Ajax request to save/update a changeset. |
wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::handle_changeset_trash_request() |
Handle request to trash a changeset. |
wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::ajax_insert_auto_draft_post() |
Ajax handler for adding a new auto-draft post. |
wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::ajax_load_available_items() |
Ajax handler for loading available menu items. |
wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::ajax_search_available_items() |
Ajax handler for searching available menu items. |
wp-admin/custom-background.php: Custom_Background::ajax_background_add() |
Ajax handler for adding custom background context to an attachment. |
wp-admin/custom-header.php: Custom_Image_Header::ajax_header_crop() |
Gets attachment uploaded by Media Manager, crops it, then saves it as a new object. Returns JSON-encoded object details. |
wp-admin/custom-header.php: Custom_Image_Header::ajax_header_add() |
Given an attachment ID for a header image, updates its “last used” timestamp to now. |
wp-admin/custom-header.php: Custom_Image_Header::ajax_header_remove() |
Given an attachment ID for a header image, unsets it as a user-uploaded header image for the current theme. |
wp-admin/includes/ajax-actions.php: wp_ajax_wp_privacy_erase_personal_data() |
Ajax handler for erasing personal data. |
wp-admin/includes/ajax-actions.php: wp_ajax_search_install_plugins() |
Ajax handler for searching plugins to install. |
wp-admin/includes/ajax-actions.php: wp_ajax_edit_theme_plugin_file() |
Ajax handler for editing a theme or plugin file. |
wp-admin/includes/ajax-actions.php: wp_ajax_wp_privacy_export_personal_data() |
Ajax handler for exporting a user’s personal data. |
wp-admin/includes/ajax-actions.php: wp_ajax_crop_image() |
Ajax handler for cropping an image. |
wp-admin/includes/ajax-actions.php: wp_ajax_save_wporg_username() |
Ajax handler for saving the user’s WordPress.org username. |
wp-admin/includes/ajax-actions.php: wp_ajax_install_theme() |
Ajax handler for installing a theme. |
wp-admin/includes/ajax-actions.php: wp_ajax_update_theme() |
Ajax handler for updating a theme. |
wp-admin/includes/ajax-actions.php: wp_ajax_delete_theme() |
Ajax handler for deleting a theme. |
wp-admin/includes/ajax-actions.php: wp_ajax_install_plugin() |
Ajax handler for installing a plugin. |
wp-admin/includes/ajax-actions.php: wp_ajax_update_plugin() |
Ajax handler for updating a plugin. |
wp-admin/includes/ajax-actions.php: wp_ajax_delete_plugin() |
Ajax handler for deleting a plugin. |
wp-admin/includes/ajax-actions.php: wp_ajax_search_plugins() |
Ajax handler for searching plugins. |
wp-admin/includes/ajax-actions.php: wp_ajax_save_attachment_order() |
Ajax handler for saving the attachment order. |
wp-admin/includes/ajax-actions.php: wp_ajax_send_attachment_to_editor() |
Ajax handler for sending an attachment to the editor. |
wp-admin/includes/ajax-actions.php: wp_ajax_send_link_to_editor() |
Ajax handler for sending a link to the editor. |
wp-admin/includes/ajax-actions.php: wp_ajax_heartbeat() |
Ajax handler for the Heartbeat API. |
wp-admin/includes/ajax-actions.php: wp_ajax_get_revision_diffs() |
Ajax handler for getting revision diffs. |
wp-admin/includes/ajax-actions.php: wp_ajax_save_user_color_scheme() |
Ajax handler for auto-saving the selected color scheme for a user’s own profile. |
wp-admin/includes/ajax-actions.php: wp_ajax_query_themes() |
Ajax handler for getting themes from themes_api(). |
wp-admin/includes/ajax-actions.php: wp_ajax_parse_embed() |
Apply [embed] Ajax handlers to a string. |
wp-admin/includes/ajax-actions.php: wp_ajax_parse_media_shortcode() | |
wp-admin/includes/ajax-actions.php: wp_ajax_destroy_sessions() |
Ajax handler for destroying multiple open sessions for a user. |
wp-admin/includes/ajax-actions.php: wp_ajax_set_attachment_thumbnail() |
Ajax handler for setting the featured image for an attachment. |
wp-admin/includes/ajax-actions.php: wp_ajax_wp_fullscreen_save_post() |
Ajax handler for saving posts from the fullscreen editor. |
wp-admin/includes/ajax-actions.php: wp_ajax_get_attachment() |
Ajax handler for getting an attachment. |
wp-admin/includes/ajax-actions.php: wp_ajax_query_attachments() |
Ajax handler for querying attachments. |
wp-admin/includes/ajax-actions.php: wp_ajax_save_attachment() |
Ajax handler for updating attachment attributes. |
wp-admin/includes/ajax-actions.php: wp_ajax_save_attachment_compat() |
Ajax handler for saving backward compatible attachment attributes. |
wp-admin/includes/ajax-actions.php: wp_ajax_find_posts() |
Ajax handler for querying posts for the Find Posts modal. |
wp-admin/includes/user.php: wp_privacy_process_personal_data_erasure_page() |
Mark erasure requests as completed after processing is finished. |
wp-admin/includes/file.php: wp_privacy_generate_personal_data_export_file() |
Generate the personal data export file. |
wp-admin/includes/file.php: wp_privacy_process_personal_data_export_page() |
Intercept personal data exporter page ajax responses in order to assemble the personal data export file. |
Changelog
Version | Description |
---|---|
WP-4.7.0 | The $status_code parameter was added. |
WP-4.1.0 | The $data parameter is now processed if a WP_Error object is passed in. |
WP-3.5.0 | Introduced. |