简体   繁体   English

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

[英]How to call the ajax function in my plugin file using wordpress?

I need to call the ajax call in my plugin . 我需要在插件中调用ajax调用。 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 . Ajax调用返回0。 How can I fix this ? 我怎样才能解决这个问题 ?

Try like this 这样尝试

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

Within class you need to add $this within array. 在类内,您需要在数组内添加$this and end the function with exit; 并以exit;结束函数exit; or wp_die(); wp_die();

In end of ajax function please write die; 在ajax函数结束时,请写die; and

if you are using same class for calling ajax function then you must have to write array( $this,'my_ajax' ) . 如果您使用相同的类来调用ajax函数,则必须编写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;
    }
}

Use this 用这个

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

$this is used with in a class.. $ this在一个类中使用。

Hope it will Work 希望它能工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM