Sends a JSON response back to an Ajax request, indicating failure.
Description
If the $value
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
$value
mixedoptional- Data to encode as JSON, then print and die.
Default:
null
$status_code
intoptional- The HTTP status code to output.
Default:
null
$flags
intoptional- Options to be passed to json_encode(). Default 0.
More Information
The response object will always have a success
key with the value false
. If anything is passed to the function in the $data
parameter, it will be encoded as the value for a data
key.
Source
function wp_send_json_error( $value = null, $status_code = null, $flags = 0 ) {
$response = array( 'success' => false );
if ( isset( $value ) ) {
if ( is_wp_error( $value ) ) {
$result = array();
foreach ( $value->errors as $code => $messages ) {
foreach ( $messages as $message ) {
$result[] = array(
'code' => $code,
'message' => $message,
);
}
}
$response['data'] = $result;
} else {
$response['data'] = $value;
}
}
wp_send_json( $response, $status_code, $flags );
}
Related
Uses | Description |
---|---|
wp_send_json()wp-includes/functions.php | Sends a JSON response back to an Ajax request. |
is_wp_error()wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
Used by | Description |
---|---|
WP_Plugin_Dependencies::check_plugin_dependencies_during_ajax()wp-includes/class-wp-plugin-dependencies.php | Checks plugin dependencies after a plugin is installed via AJAX. |
wp_ajax_activate_plugin()wp-admin/includes/ajax-actions.php | Handles activating a plugin via AJAX. |
locate_block_template()wp-includes/block-template.php | Finds a block template with equal or higher specificity than a given PHP template file. |
wp_ajax_send_password_reset()wp-admin/includes/ajax-actions.php | Handles sending a password reset link via AJAX. |
wp_ajax_toggle_auto_updates()wp-admin/includes/ajax-actions.php | Handles enabling or disable plugin and theme auto-updates via AJAX. |
wp_ajax_media_create_image_subsizes()wp-admin/includes/ajax-actions.php | Handles creating missing image sub-sizes for just uploaded images via AJAX. |
wp_ajax_health_check_get_sizes()wp-admin/includes/ajax-actions.php | Handles site health check to get directories and database sizes via AJAX. |
wp_ajax_health_check_dotorg_communication()wp-admin/includes/ajax-actions.php | Handles site health checks on server communication via AJAX. |
wp_ajax_health_check_background_updates()wp-admin/includes/ajax-actions.php | Handles site health checks on background updates via AJAX. |
wp_ajax_health_check_loopback_requests()wp-admin/includes/ajax-actions.php | Handles site health checks on loopback requests via AJAX. |
wp_ajax_health_check_site_status_result()wp-admin/includes/ajax-actions.php | Handles site health check to update the result status via AJAX. |
wp_privacy_process_personal_data_export_page()wp-admin/includes/privacy-tools.php | Intercept personal data exporter page Ajax responses in order to assemble the personal data export file. |
wp_privacy_generate_personal_data_export_file()wp-admin/includes/privacy-tools.php | Generate the personal data export file. |
wp_privacy_process_personal_data_erasure_page()wp-admin/includes/privacy-tools.php | Mark erasure requests as completed after processing is finished. |
wp_ajax_wp_privacy_export_personal_data()wp-admin/includes/ajax-actions.php | Handles exporting a user’s personal data via AJAX. |
wp_ajax_wp_privacy_erase_personal_data()wp-admin/includes/ajax-actions.php | Handles erasing personal data via AJAX. |
WP_Customize_Manager::handle_load_themes_request()wp-includes/class-wp-customize-manager.php | Loads themes into the theme browsing/installation UI. |
WP_Customize_Manager::handle_override_changeset_lock_request()wp-includes/class-wp-customize-manager.php | Removes changeset lock when take over request is sent via Ajax. |
WP_Customize_Manager::handle_dismiss_autosave_or_lock_request()wp-includes/class-wp-customize-manager.php | Deletes a given auto-draft changeset or the autosave revision for a given changeset or delete changeset lock. |
WP_Customize_Manager::handle_changeset_trash_request()wp-includes/class-wp-customize-manager.php | Handles request to trash a changeset. |
wp_ajax_edit_theme_plugin_file()wp-admin/includes/ajax-actions.php | Handles editing a theme or plugin file via AJAX. |
wp_ajax_get_community_events()wp-admin/includes/ajax-actions.php | Handles Ajax requests for community events |
WP_Customize_Nav_Menus::ajax_insert_auto_draft_post()wp-includes/class-wp-customize-nav-menus.php | Ajax handler for adding a new auto-draft post. |
wp_ajax_search_install_plugins()wp-admin/includes/ajax-actions.php | Handles searching plugins to install via AJAX. |
wp_ajax_delete_plugin()wp-admin/includes/ajax-actions.php | Handles deleting a plugin via AJAX. |
wp_ajax_search_plugins()wp-admin/includes/ajax-actions.php | Handles searching plugins via AJAX. |
wp_ajax_install_theme()wp-admin/includes/ajax-actions.php | Handles installing a theme via AJAX. |
wp_ajax_update_theme()wp-admin/includes/ajax-actions.php | Handles updating a theme via AJAX. |
wp_ajax_delete_theme()wp-admin/includes/ajax-actions.php | Handles deleting a theme via AJAX. |
wp_ajax_install_plugin()wp-admin/includes/ajax-actions.php | Handles installing a plugin via AJAX. |
WP_Customize_Selective_Refresh::handle_render_partials_request()wp-includes/customize/class-wp-customize-selective-refresh.php | Handles the Ajax request to return the rendered partials for the requested placements. |
wp_ajax_save_wporg_username()wp-admin/includes/ajax-actions.php | Handles saving the user’s WordPress.org username via AJAX. |
WP_Customize_Nav_Menus::ajax_load_available_items()wp-includes/class-wp-customize-nav-menus.php | Ajax handler for loading available menu items. |
WP_Customize_Nav_Menus::ajax_search_available_items()wp-includes/class-wp-customize-nav-menus.php | Ajax handler for searching available menu items. |
wp_ajax_crop_image()wp-admin/includes/ajax-actions.php | Handles cropping an image via AJAX. |
WP_Customize_Manager::refresh_nonces()wp-includes/class-wp-customize-manager.php | Refreshes nonces for the current preview. |
wp_ajax_press_this_add_category()wp-includes/deprecated.php | Ajax handler for creating new category from Press This. |
wp_ajax_update_plugin()wp-admin/includes/ajax-actions.php | Handles updating a plugin via AJAX. |
wp_ajax_press_this_save_post()wp-includes/deprecated.php | Ajax handler for saving a post from Press This. |
wp_ajax_destroy_sessions()wp-admin/includes/ajax-actions.php | Handles destroying multiple open sessions for a user via AJAX. |
Custom_Background::ajax_background_add()wp-admin/includes/class-custom-background.php | Handles Ajax request for adding custom background context to an attachment. |
wp_ajax_parse_embed()wp-admin/includes/ajax-actions.php | Applies Ajax handlers to a string. |
wp_ajax_parse_media_shortcode()wp-admin/includes/ajax-actions.php | |
wp_ajax_set_attachment_thumbnail()wp-admin/includes/ajax-actions.php | Handles setting the featured image for an attachment via AJAX. |
wp_ajax_save_attachment_order()wp-admin/includes/ajax-actions.php | Handles saving the attachment order via AJAX. |
wp_ajax_send_attachment_to_editor()wp-admin/includes/ajax-actions.php | Handles sending an attachment to the editor via AJAX. |
wp_ajax_send_link_to_editor()wp-admin/includes/ajax-actions.php | Handles sending a link to the editor via AJAX. |
wp_ajax_heartbeat()wp-admin/includes/ajax-actions.php | Handles the Heartbeat API via AJAX. |
wp_ajax_get_revision_diffs()wp-admin/includes/ajax-actions.php | Handles getting revision diffs via AJAX. |
wp_ajax_save_user_color_scheme()wp-admin/includes/ajax-actions.php | Handles auto-saving the selected color scheme for a user’s own profile via AJAX. |
wp_ajax_query_themes()wp-admin/includes/ajax-actions.php | Handles getting themes from themes_api() via AJAX. |
wp_ajax_image_editor()wp-admin/includes/ajax-actions.php | Handles image editing via AJAX. |
wp_ajax_wp_fullscreen_save_post()wp-admin/includes/ajax-actions.php | Handles saving posts from the fullscreen editor via AJAX. |
wp_ajax_get_attachment()wp-admin/includes/ajax-actions.php | Handles getting an attachment via AJAX. |
wp_ajax_query_attachments()wp-admin/includes/ajax-actions.php | Handles querying attachments via AJAX. |
wp_ajax_save_attachment()wp-admin/includes/ajax-actions.php | Handles updating attachment attributes via AJAX. |
wp_ajax_save_attachment_compat()wp-admin/includes/ajax-actions.php | Handles saving backward compatible attachment attributes via AJAX. |
wp_ajax_find_posts()wp-admin/includes/ajax-actions.php | Handles querying posts for the Find Posts modal via AJAX. |
Custom_Image_Header::ajax_header_crop()wp-admin/includes/class-custom-image-header.php | Gets attachment uploaded by Media Manager, crops it, then saves it as a new object. Returns JSON-encoded object details. |
Custom_Image_Header::ajax_header_add()wp-admin/includes/class-custom-image-header.php | Given an attachment ID for a header image, updates its “last used” timestamp to now. |
Custom_Image_Header::ajax_header_remove()wp-admin/includes/class-custom-image-header.php | Given an attachment ID for a header image, unsets it as a user-uploaded header image for the active theme. |
WP_Customize_Manager::save()wp-includes/class-wp-customize-manager.php | Handles customize_save WP Ajax request to save/update a changeset. |
WP_Customize_Widgets::wp_ajax_update_widget()wp-includes/class-wp-customize-widgets.php | Updates widget settings asynchronously. |
Doing just
wp_send_json_error();
won’t send error – it will actually return200
instead. So, if you want to avoid AJAX’success
and triggererror
, make sure to add error code, like500
, or whatever you prefer.Example
save_field.php