I’m trying to use Ajax with Sage. The JS script in which I make the Ajax call (using $.post()
) is located in scripts.js at the root of my Sage theme. Then in my functions.php
(or more precisely in lib/extras.php
) I do:
wp_enqueue_script( 'js_scripts', get_stylesheet_directory_uri() . '/scripts.js', array('jquery'), true );
wp_localize_script( 'js_scripts', 'WPaAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
But this requires me to keep my JS Ajax call in its own separate JS file, whereas I’d prefer to put it with the rest of my custom JS code in main.js
. However if I do so then which handle should I use when running wp_localize_script()
? I tried wp_localize_script( 'sage/js', 'WPaAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
but it doesn’t work.