如何使用wordpress在我的插件文件中调用ajax函数?

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 ?

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

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();

Use this

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

$this is used with in a class..

Hope it will Work