WordPress在子类中的ajax函数

Ada*_*Mo. 7 php ajax wordpress jquery

如果我有这个课程

class something {
    public function __construct() {
        add_action('wp_ajax_ajax_func', array( $this, 'ajax_func' ) );
    }

    public function ajax_func() {

    }  

}
class stchild extends something {

    public function __construct() {

    }

    public function ajax_func() {
        echo "Test child1";
    }  
}
Run Code Online (Sandbox Code Playgroud)

如何通过ajax 只ajax_func调用stchild类中的函数?
当我尝试这个代码

jQuery.ajax({
url: 'admin-ajax.php',
data: {action : 'ajax_func'},
success: function(data){
    console.log(data);
}
});
Run Code Online (Sandbox Code Playgroud)

它获取所有调用的函数ajax_func,我想定义特定的类来从中获取此函数.请注意,类中有许多子类,something并且都已激活.

Pat*_*ore 5

你可以将它包装在一个动作函数中.

add_action( 'wp_ajax_do_something', 'my_do_something_callback' );

function my_do_something_callback() {
    $object = new stchild;
    $object->ajax_func();
    die();
}
Run Code Online (Sandbox Code Playgroud)

JS:

jQuery.ajax({
    url: ajaxurl, // Use this pre-defined variable,
                  // instead of explicitly passing 'admin-ajax.php',
    data: { action : 'do_something' },
    success: function(data){
        console.log(data);
    }
});
Run Code Online (Sandbox Code Playgroud)