wp_ajax_{$action} WordPress Action Hook
The wp_ajax_{$action} hook allows you to perform an action when an AJAX request is made to WordPress.
do_action( "wp_ajax_{$action}" ) #
Fires authenticated Ajax actions for logged-in users.
Description
The dynamic portion of the hook name, $action
, refers to the name of the Ajax action callback being fired.
More Information
- This hook allows you to handle your custom AJAX endpoints. The
wp_ajax_
hooks follows the format “wp_ajax_$action
“, where$action
is the ‘action
‘ field submitted toadmin-ajax.php
. - This hook only fires for logged-in users. If your action only allows Ajax requests to come from users not logged-in, you need to instead use wp_ajax_nopriv_$action such as:
add_action( 'wp_ajax_nopriv_add_foobar', 'prefix_ajax_add_foobar' );
. To allow both, you must register both hooks! - See also wp_ajax__requestaction
- See also Ajax Plugin Handbook
Source
File: wp-admin/admin-ajax.php
Changelog
Version | Description |
---|---|
2.1.0 | Introduced. |