Calling a method from controller using ajax

sage9

#1

I’m trying to run a method from page controller using ajax. Here what I got until now:

On filter.php, I run the hooks:
add_action( 'wp_ajax_addBanco', ['App\PageProprietario', 'addBanco'] );
add_action( 'wp_ajax_nopriv_addBanco', ['App\PageProprietario', 'addBanco'] );

And on the controller, I wil stored the method:

public static function addBanco()
{
   wp_send_json_success($_POST);
}

My question is: this is the correct way? The method must be static or could be public?


#2

Static methods must be called differently.
Change this
add_action( 'wp_ajax_addBanco', ['App\PageProprietario', 'addBanco'] );
To this
add_action( 'wp_ajax_addBanco', ['App\PageProprietario::addBanco'] );


#3

Thanks for the reply @codepuncher. But I believe the correct way is setting callback function outside the array. Like this:

add_action( 'wp_ajax_addBanco', 'App\PageProprietario::addBanco' );