wp_ajax + wp_ajax_nopriv weird behaviour inside a foreach

I am writing a WP plugin and I am trying to add the wp_ajax and wp_ajax_nopriv_.

In the same place, these two codes have have different behaviors (we are inside a method):

WORKING, I get my json in js

$controller = $this->controller;
$method = ‘echo_foo’;

if(method_exists($this->controller, $method )) {
add_action( ‘wp_ajax_’ . $method, array($this->controller, $method));
add_action( ‘wp_ajax_nopriv_’ . $method, array($this->controller, $method));
}

NOT WORKING – returns 0 Bad Request

foreach($methods as $method)
{
if(method_exists($this->controller, $method ))
{
add_action( ‘wp_ajax_’ . $method, array($this->controller, $method));
add_action( ‘wp_ajax_nopriv_’ . $method, array($this->controller, $method));
}
}

$methods is an array of strings.

Any idea?

Read more here:: wp_ajax + wp_ajax_nopriv weird behaviour inside a foreach

Leave a Reply

Your email address will not be published. Required fields are marked *