jQuery is not defined because you’ve not referred jQuery as dependency for your script so wp isn’t aware it should load it for your script.
wp_localize_script
should be called on script that is already linked via wp_enqueue_script
, so you first run wp_enqueue_script
then wp_localize_script
as you do in your UPD.
Another thing about wp_localize_script
is it takes 3 arguments (see https://developer.wordpress.org/reference/functions/wp_localize_script/), and last one is associative array.
So if in your JS code you want to get ajaxurl
via myAjax.ajaxurl
you need to call wp_localize_script( 'zip_js', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ));
Code below takes into account your new naming of script as zip_js
and global js variable is now Zip_JS
, so you will access your ajaxurl
in js code like Zip_JS.ajaxurl
instead of myAjax.ajaxurl
.
Here’s how you need to do to fix jQuery missing error, and also use localize script properly:
function my_load_scripts() {
wp_enqueue_script( 'zip_js', get_template_directory_uri() . '/js/zip_search.js', array('jquery') );
wp_localize_script( 'zip_js', 'Zip_JS', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ));
}
add_action('wp_enqueue_scripts', 'my_load_scripts');