0

I need to call the ajax call in my plugin . I have created plugin file .

My Plugin File :-

  class Wp_MYplugin{
        function __construct() {
            add_action( 'wp_ajax_my_ajax', 'my_ajax' );
        }

        function my_ajax(){
            echo "test";
        }
   }

Ajax call returns 0 . How can I fix this ?

1
  • Add more code, the question is unclear at the moment. Where is the javascript part (actuall AJAX call)? Commented May 26, 2016 at 16:42

3 Answers 3

1

Try like this

add_action( 'wp_ajax_my_ajax', array( $this, 'my_ajax' ) );

Within class you need to add $this within array. and end the function with exit; or wp_die();

Sign up to request clarification or add additional context in comments.

Comments

0

In end of ajax function please write die; and

if you are using same class for calling ajax function then you must have to write array( $this,'my_ajax' ).

For Example:

class Wp_MYplugin{
    function __construct() {
        add_action( 'wp_ajax_my_ajax', array( $this,'my_ajax' ) );
    }

    function my_ajax(){
        echo "test";
        die;
    }
}

Comments

0

Use this

add_action( 'wp_ajax_my_ajax', array( $this, 'my_ajax' ) );

$this is used with in a class..

Hope it will Work

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.