0

In my plugin, I'm using wp_ajax, This hook does not work when I wrap the init function call with a condition like this :

public function __construct(){
   if( isset( $_GET["page"] ) && $_GET["page"] === 'edit-foo' ){ <-- This line prevents the call 'wp_ajax_foo' !
        add_action( 'admin_init', array($this, 'init') );
   }
}

init(){
    ... <- initalize $this->editable_item->item
    add_action('wp_ajax_foo',array( $this->editable_item->item, 'foo' ) );
}

1 Answer 1

0

As foo is a static function you don't need to specify the object, so you can use it outside the condition to correctly load it like this :

 public function __construct(){
    if( isset( $_GET["page"] ) && $_GET["page"] === 'edit-foo' ){
        add_action( 'admin_init', array($this, 'init') );
    }
    add_action('wp_ajax_foo',array( 'MyPlugin/Classes/MyClassWithFooFunction', 'foo' ) );
 }

 init(){
     ... <- initalize $this->editable_item->item
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.