plugin development - Performing ajax request in wordpress

admin2025-06-03  3

I am trying to perform ajax request in my plugin. What I have done is:

    Class Someclass {

        public function __construct(){

        }

        public function current_page_function(){

            add_action(
                "wp_ajax_test_ajax",array($this,'test_ajax')
            );

            $this->data=array();

            View_Includer::load('index',$this->data,false,false,false,false,true,false,false);
        }

         public function test_ajax(){
            wp_send_json_success('Science achieved!');
            exit();
        }

}

Script:

    var send_ajax_request = function( cpt_id ) {

        // Define the function and the data to send to the server.
        var data = {
            'action':    'test_ajax',
            'post_type': cpt_id
        };

        // Send the request to the server and handle the response.
        jQuery.post( Essestials.ajaxurl, data, function( response ) {
            return response;
            // Handle your response here.
        });

    };

When I do this, the wordpress return 400 error code. But when I do as below:

    Class Someclass {

        public function __construct(){
            add_action(
                "wp_ajax_test_ajax",array($this,'test_ajax')
            );
        }

        public function current_page_function(){
            $this->data=array();
            View_Includer::load('index',$this->data,false,false,false,false,true,false,false);
        }

         public function test_ajax(){
            wp_send_json_success('Science achieved!');
            exit();
        }
    }

it returns the following response :

    {success: true, data: "Science achieved!"}

what wrong am I doing on the first process ? Why it works only when I add the hook in constructor ? Any kind of helps are highly appreciated. Thanks.

I am trying to perform ajax request in my plugin. What I have done is:

    Class Someclass {

        public function __construct(){

        }

        public function current_page_function(){

            add_action(
                "wp_ajax_test_ajax",array($this,'test_ajax')
            );

            $this->data=array();

            View_Includer::load('index',$this->data,false,false,false,false,true,false,false);
        }

         public function test_ajax(){
            wp_send_json_success('Science achieved!');
            exit();
        }

}

Script:

    var send_ajax_request = function( cpt_id ) {

        // Define the function and the data to send to the server.
        var data = {
            'action':    'test_ajax',
            'post_type': cpt_id
        };

        // Send the request to the server and handle the response.
        jQuery.post( Essestials.ajaxurl, data, function( response ) {
            return response;
            // Handle your response here.
        });

    };

When I do this, the wordpress return 400 error code. But when I do as below:

    Class Someclass {

        public function __construct(){
            add_action(
                "wp_ajax_test_ajax",array($this,'test_ajax')
            );
        }

        public function current_page_function(){
            $this->data=array();
            View_Includer::load('index',$this->data,false,false,false,false,true,false,false);
        }

         public function test_ajax(){
            wp_send_json_success('Science achieved!');
            exit();
        }
    }

it returns the following response :

    {success: true, data: "Science achieved!"}

what wrong am I doing on the first process ? Why it works only when I add the hook in constructor ? Any kind of helps are highly appreciated. Thanks.

Share Improve this question asked Feb 2, 2019 at 19:03 TShresthaTShrestha 1011 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Some actions must be registered as early as possible. When you put it in the "current_page_function" method, (depending on your program) it might be too late.

Hence, the wp_ajax_* hook cannot find your action, and it returns the error.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748950475a315089.html

最新回复(0)