wp_ajax_(action)
Fires during a WordPress AJAX request for an authenticated user. Such requests are sent to /wp-admin/admin-ajax.php.
This event does not fire if the user is not authenticated: ! is_user_logged_in(). For unauthenticated users, use wp_ajax_nopriv_(action). It works exactly like this event, except that it fires only for unauthenticated users.
(action) is the dynamic part of the hook where the AJAX event name is specified. This name must be passed as the action GET|POST parameter in the AJAX request to /wp-admin/admin-ajax.php. A hook based on the same name must also be created to handle the AJAX request.
Consider an example.
Suppose we choose find-best-posts as the event name. The hook name will then be wp_ajax_find-best-posts, and a PHP function that handles the authenticated user's AJAX request must be attached to it. Add the following code to a plugin or the theme's functions.php file:
add_action( 'wp_ajax_find-best-posts', 'find_best_posts__ajax_callback' );
function find_best_posts__ajax_callback(){}
// The PHP function can have any name
When creating an AJAX request to /wp-admin/admin-ajax.php in JavaScript, pass our action value in a GET or POST parameter. Example JavaScript code:
let ajaxurl = '/wp-admin/admin-ajax.php';
let data = {
action: 'find-best-posts',
more_data: '...'
}
jQuery.post( ajaxurl, data, function( response ){
console.log( response );
} );
For more about creating AJAX requests in WordPress, see AJAX in WordPress (basics).
Reserved (action) values in WordPress core
In WordPress core, PHP functions for an event have the same name as the event, with - replaced by _. For example, the PHP function for an AJAX request with the get-permalink event is wp_ajax_get_permalink().
For GET requests: action → PHP function (as of WordPress 5.7.2):
fetch-list → wp_ajax_fetch_list() ajax-tag-search → wp_ajax_ajax_tag_search() wp-compression-test → wp_ajax_wp_compression_test() imgedit-preview → wp_ajax_imgedit_preview() oembed-cache → wp_ajax_oembed_cache() autocomplete-user → wp_ajax_autocomplete_user() dashboard-widgets → wp_ajax_dashboard_widgets() logged-in → wp_ajax_logged_in() rest-nonce → wp_ajax_rest_nonce()
For POST requests: action → PHP function (as of WordPress 5.7.2):
oembed-cache → wp_ajax_oembed_cache() image-editor → wp_ajax_image_editor() delete-comment → wp_ajax_delete_comment() delete-tag → wp_ajax_delete_tag() delete-link → wp_ajax_delete_link() delete-meta → wp_ajax_delete_meta() delete-post → wp_ajax_delete_post() trash-post → wp_ajax_trash_post() untrash-post → wp_ajax_untrash_post() delete-page → wp_ajax_delete_page() dim-comment → wp_ajax_dim_comment() add-link-category → wp_ajax_add_link_category() add-tag → wp_ajax_add_tag() get-tagcloud → wp_ajax_get_tagcloud() get-comments → wp_ajax_get_comments() replyto-comment → wp_ajax_replyto_comment() edit-comment → wp_ajax_edit_comment() add-menu-item → wp_ajax_add_menu_item() add-meta → wp_ajax_add_meta() add-user → wp_ajax_add_user() closed-postboxes → wp_ajax_closed_postboxes() hidden-columns → wp_ajax_hidden_columns() update-welcome-panel → wp_ajax_update_welcome_panel() menu-get-metabox → wp_ajax_menu_get_metabox() wp-link-ajax → wp_ajax_wp_link_ajax() menu-locations-save → wp_ajax_menu_locations_save() menu-quick-search → wp_ajax_menu_quick_search() meta-box-order → wp_ajax_meta_box_order() get-permalink → wp_ajax_get_permalink() sample-permalink → wp_ajax_sample_permalink() inline-save → wp_ajax_inline_save() inline-save-tax → wp_ajax_inline_save_tax() find_posts → wp_ajax_find_posts() widgets-order → wp_ajax_widgets_order() save-widget → wp_ajax_save_widget() delete-inactive-widgets → wp_ajax_delete_inactive_widgets() set-post-thumbnail → wp_ajax_set_post_thumbnail() date_format → wp_ajax_date_format() time_format → wp_ajax_time_format() wp-remove-post-lock → wp_ajax_wp_remove_post_lock() dismiss-wp-pointer → wp_ajax_dismiss_wp_pointer() upload-attachment → wp_ajax_upload_attachment() get-attachment → wp_ajax_get_attachment() query-attachments → wp_ajax_query_attachments() save-attachment → wp_ajax_save_attachment() save-attachment-compat → wp_ajax_save_attachment_compat() send-link-to-editor → wp_ajax_send_link_to_editor() send-attachment-to-editor → wp_ajax_send_attachment_to_editor() save-attachment-order → wp_ajax_save_attachment_order() media-create-image-subsizes → wp_ajax_media_create_image_subsizes() heartbeat → wp_ajax_heartbeat() get-revision-diffs → wp_ajax_get_revision_diffs() save-user-color-scheme → wp_ajax_save_user_color_scheme() update-widget → wp_ajax_update_widget() query-themes → wp_ajax_query_themes() parse-embed → wp_ajax_parse_embed() set-attachment-thumbnail → wp_ajax_set_attachment_thumbnail() parse-media-shortcode → wp_ajax_parse_media_shortcode() destroy-sessions → wp_ajax_destroy_sessions() install-plugin → wp_ajax_install_plugin() update-plugin → wp_ajax_update_plugin() crop-image → wp_ajax_crop_image() generate-password → wp_ajax_generate_password() save-wporg-username → wp_ajax_save_wporg_username() delete-plugin → wp_ajax_delete_plugin() search-plugins → wp_ajax_search_plugins() search-install-plugins → wp_ajax_search_install_plugins() activate-plugin → wp_ajax_activate_plugin() update-theme → wp_ajax_update_theme() delete-theme → wp_ajax_delete_theme() install-theme → wp_ajax_install_theme() get-post-thumbnail-html → wp_ajax_get_post_thumbnail_html() get-community-events → wp_ajax_get_community_events() edit-theme-plugin-file → wp_ajax_edit_theme_plugin_file() wp-privacy-export-personal-data → wp_ajax_wp_privacy_export_personal_data() wp-privacy-erase-personal-data → wp_ajax_wp_privacy_erase_personal_data() health-check-site-status-result → wp_ajax_health_check_site_status_result() health-check-dotorg-communication → wp_ajax_health_check_dotorg_communication() health-check-is-in-debug-mode → wp_ajax_health_check_is_in_debug_mode() health-check-background-updates → wp_ajax_health_check_background_updates() health-check-loopback-requests → wp_ajax_health_check_loopback_requests() health-check-get-sizes → wp_ajax_health_check_get_sizes() toggle-auto-updates → wp_ajax_toggle_auto_updates() send-password-reset → wp_ajax_send_password_reset() // DEPRECATED wp-fullscreen-save-post → wp_ajax_wp_fullscreen_save_post() press-this-save-post → wp_ajax_press_this_save_post() press-this-add-category → wp_ajax_press_this_add_category() health-check-dotorg-communication → wp_ajax_health_check_dotorg_communication() health-check-is-in-debug-mode → wp_ajax_health_check_is_in_debug_mode() health-check-background-updates → wp_ajax_health_check_background_updates() health-check-loopback-requests → wp_ajax_health_check_loopback_requests()
Usage
add_action( 'wp_ajax_(action)', 'wp_kama_ajax_action' );
/**
* Function for `wp_ajax_(action)` action-hook.
*
* @return void
*/
function wp_kama_ajax_action(){
// action...
}
Examples
#1 Usage example
See the AJAX article.
Changelog
| Since 2.1.0 | Introduced. |
Where the hook is called
do_action( "wp_ajax_{$action}" );
Where the hook is used in WordPress
add_action( 'wp_ajax_' . $_GET['action'], 'wp_ajax_' . str_replace( '-', '_', $_GET['action'] ), 1 );
add_action( 'wp_ajax_' . $_POST['action'], 'wp_ajax_' . str_replace( '-', '_', $_POST['action'] ), 1 );
add_action( 'wp_ajax_nopriv_generate-password', 'wp_ajax_nopriv_generate_password' );
add_action( 'wp_ajax_nopriv_heartbeat', 'wp_ajax_nopriv_heartbeat', 1 );
add_action( 'wp_ajax_check_plugin_dependencies', array( 'WP_Plugin_Dependencies', 'check_plugin_dependencies_during_ajax' ) );
add_action( 'wp_ajax_custom-background-add', array( $this, 'ajax_background_add' ) );
add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
add_action( 'wp_ajax_custom-header-crop', array( $this, 'ajax_header_crop' ) );
add_action( 'wp_ajax_custom-header-add', array( $this, 'ajax_header_add' ) );
add_action( 'wp_ajax_custom-header-remove', array( $this, 'ajax_header_remove' ) );
add_action( 'wp_ajax_customize_save', array( $this, 'save' ) );
add_action( 'wp_ajax_customize_trash', array( $this, 'handle_changeset_trash_request' ) );
add_action( 'wp_ajax_customize_refresh_nonces', array( $this, 'refresh_nonces' ) );
add_action( 'wp_ajax_customize_load_themes', array( $this, 'handle_load_themes_request' ) );
add_action( 'wp_ajax_customize_override_changeset_lock', array( $this, 'handle_override_changeset_lock_request' ) );
add_action( 'wp_ajax_customize_dismiss_autosave_or_lock', array( $this, 'handle_dismiss_autosave_or_lock_request' ) );
add_action( 'wp_ajax_load-available-menu-items-customizer', array( $this, 'ajax_load_available_items' ) );
add_action( 'wp_ajax_search-available-menu-items-customizer', array( $this, 'ajax_search_available_items' ) );
add_action( 'wp_ajax_customize-nav-menus-insert-auto-draft', array( $this, 'ajax_insert_auto_draft_post' ) );
add_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' );
remove_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' );