钩子文档

wp_ajax_{$action}

💡 云策文档标注

概述

wp_ajax_{$action} 是一个 WordPress 钩子,用于处理已登录用户的认证 Ajax 请求。它允许开发者注册自定义 Ajax 端点,通过动态的 $action 参数来指定回调函数。

关键要点

  • 此钩子仅对已登录用户触发,格式为 wp_ajax_$action,其中 $action 是提交到 admin-ajax.php 的 'action' 字段。
  • 对于未登录用户的 Ajax 请求,需使用 wp_ajax_nopriv_$action 钩子;若两者都需支持,必须同时注册两个钩子。
  • 处理 Ajax 请求时,应输出响应并调用 wp_die() 来停止执行,或使用 wp_send_json() 自动处理 JSON 响应和终止。

代码示例

// 注册 Ajax 处理函数
add_action( 'wp_ajax_foobar', 'my_ajax_foobar_handler' );
function my_ajax_foobar_handler() {
    // 处理请求并输出响应
    echo 'Response';
    wp_die();
}

// 使用 jQuery 触发 Ajax 请求
jQuery.post(
    my_foobar_client.ajaxurl, 
    {
        'action': 'foobar',
        'foobar_id': 123
    }, 
    function(response) {
        console.log('The server responded: ', response);
    }
);

// 使用 wp_send_json() 输出 JSON 响应
add_action( 'wp_ajax_foobar_json', 'my_ajax_foobar_json_handler' );
function my_ajax_foobar_json_handler() {
    $array_result = array(
        'data' => 'your data',
        'message' => 'your message'
    );
    wp_send_json($array_result);
}

注意事项

  • 在 jQuery.post 示例中,ajaxurl 应正确设置为 WordPress 提供的 Ajax URL,如 my_foobar_client.ajax_url。
  • 使用 wp_send_json() 时,无需额外调用 wp_die(),因为它已包含终止执行的功能。

📄 原文内容

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 to admin-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

do_action( "wp_ajax_{$action}" );

Changelog

Version Description
2.1.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Example migrated from Codex:

    If you need to create an AJAX handler for an “add_foobar” request, you would create a hook like this:

    add_action( 'wp_ajax_foobar', 'my_ajax_foobar_handler' );
    
    function my_ajax_foobar_handler() {
        // Make your response and echo it.
    
        // Don't forget to stop execution afterward.
        wp_die();
    }

    The following code is an example using jQuery that would trigger the above hook.

    jQuery.post(
        my_foobar_client.ajaxurl, 
        {
            'action': 'foobar',
            'foobar_id':   123
        }, 
        function(response) {
            console.log('The server responded: ', response);
        }
    );

    Note: The foobar_id would be available in your PHP hook handler via $_POST['foobar_id'].

  2. Skip to note 4 content

    using wp_send_json() to output your result as a json format

    add_action( 'wp_ajax_foobar_json', 'my_ajax_foobar_json_handler' );
    function my_ajax_foobar_json_handler() {
        // Your response in array
    	$array_result = array(
    		'data' => 'your data',
    		'message' => 'your message'
    	);
    
        // Make your array as json
    	wp_send_json($array_result);
     
        // Don't forget to stop execution afterward.
        wp_die();
    }