$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - Performing ajax request in wordpress|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - Performing ajax request in wordpress

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far