ajax - Adding add_action in functions.php causes error
one text
I want to create a new add_action in my functions.php and call it witch ajax but it triggers an erro which says "Fatal error: Uncaught Error: Call to undefined function add_action()".
I see the error in the console.log.
I haven't developped this theme myself, and it's the only thing I add in the functions.php.
mypage.js
const ajaxUrl = "<?php echo get_template_directory_uri(); ?>/functions.php"
$.get(ajaxUrl, {action: 'newAddAction'}, function(response) {
console.log(response);
})
functions.php
add_action('wp_ajax_newAddAction', 'newAddAction');
function newAddAction() {
echo 'hello world';
die;
}
Source