I am trying to separate a part of my code from the functions.php file to make it easier to understand and maintain. So I want to put all my "ajax" related code in a different PHP file.
Here is the require in my functions.php file:
require_once( __DIR__ . '/includes/ajax.php');
And here is some of the content of the ajax.php file:
function theme_enqueue_ajax(){
wp_localize_script( 'myJSScript', 'ajaxUrl', array( 'url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( "wp_enqueue_scripts", "theme_enqueue_ajax");
If I put the code from ajax.php directly in my functions.php file, everything works fine, but once I move it to ajax.php the ajaxUrl variable doesnt exist anymnore.