0

I have an ajax which passes some jQuery variables to PHP function. My function was supposed to be in a class file. But ajax won't work and shows bad request. But if i place php function in my child theme, it works perfectly.

Here is my jQuery AJAX

 $.ajax({
      type: 'POST',
      url: ajaxurl,                        
      data: {
      action: 'thwec_get_element_block_data',
      block_element_list: block_element_list
    },
     success:function(data){
      alert('success');
      console.log(data);
     },
     error: function(){
      alert('error');
     }
    });

And my PHP function.

 public function __construct() {
        add_action('wp_ajax_thwec_get_element_block_data',array($this,'get_element_block_data')); 

        // $this->get_element_block_data();
        // $this->init_constants();
    }

public function get_element_block_data(){
    var_dump("hello");
    exit();
}
  • Are you sure you're logged in? You'd need to use the wp_ajax_nopriv_{$_REQUEST[‘action’]} hook for non-authenticated Ajax actions for logged-out users. – swissspidy Feb 7 '18 at 7:40
  • yes. am logged in. Tired those. But same error – melvin Feb 7 '18 at 7:42
  • Are you sure WP has loaded before you init the class? – Drupalizeme Feb 7 '18 at 7:43
  • yes . sure @Drupalizeme – melvin Feb 7 '18 at 7:55
  • 1
    Where is the class being instantiated? – Jacob Peattie Feb 7 '18 at 10:00
1

And i found the answer. As Jacob Peattie asked the instance of the class was defined but was not called. Although the class file was included , instance was not called. Thank you everyone for your valuable time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.