Home Wordpress Ajax is Returning a Fatal Error
Reply: 0

Wordpress Ajax is Returning a Fatal Error

Sergio
1#
Sergio Published in 2018-01-12 23:01:44Z

I am trying to make an AJAX call in a child theme I am playing around with, and I am receiving the following error:

Fatal error: Call to undefined function add_action() in

Basically my ajax call goes to functions.php file and in the functions.php I am requiring once the file (ajax.php) with the PHP logic. If in ajax.php file all i am doing is echoing a string, it works fine, but if I try to use an add_action function, it then returns the Fatal Error.

Here is some code.

JS:

$.ajax({
   url: 'url to functions.php file',
   data: {'my-ajax': 'something'},
   type: 'POST',
   cache: false,
   success: function(data, status) {
      if(status == "success") {
         $('.container').append(data);
      }
   },
   error: function(xhr, desc, err) {
      console.log(xhr);
      console.log("Details: " + desc + "\nError:" + err);
   }
});

PHP - functions.php File:

if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
      if($_POST['my-ajax'] != "") {
         require_once('prod-cats.php');
         die();
      }
}

PHP - ajax.php:

add_action( 'testing_ajax', 'testing_ajax_func' );
function testing_ajax_func(){
  echo "Hey There";
}

The above code fails, but if I remove everything from the ajax.php file and leave behind the echo, it then works.

What is it with the add_action function that is causing the issue?

Thanks.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.338061 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO