WordPress ajax function in child class

If I have these classes

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"; } } 

How to call only ajax_func function in stchild ajax class?
when i try this code

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

it gets all the functions called ajax_func , I want to define a specific class to get this function from it. Note that there are many child classes from the something class, and all of them are activated.

+7
jquery ajax php wordpress
source share
1 answer

You can wrap it inside an action function.

 add_action( 'wp_ajax_do_something', 'my_do_something_callback' ); function my_do_something_callback() { $object = new stchild; $object->ajax_func(); die(); } 

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); } }); 
+5
source share

All Articles