I am trying to access to a custom php file in WP theme directory to pass to an jQuery Ajax call. I have added foloowing code In function.php
to register the script and PHP file
function add_ajax() {
wp_enqueue_script( 'addproducts', get_template_directory_uri() .'/js/addproducts.js', array('jquery'),'',true );
wp_localize_script('addproducts', 'ajax_custom', array('ajaxurl' => admin_url('session.php')));
}
add_action( 'wp_enqueue_scripts', 'add_ajax' );
now in my JavaScript file I am trying to pass url like url: ajaxurl
var request = $.ajax({
type: "POST",
url: ajaxurl,
but I am getting the ajaxurl is not defined
error
addproducts.js?ver=4.9.6:6 Uncaught ReferenceError: ajaxurl is not defined at HTMLAnchorElement. (addproducts.js?ver=4.9.6:6) at HTMLAnchorElement.dispatch (jquery.min.js?ver=4.9.6:3) at HTMLAnchorElement.r.handle (jquery.min.js?ver=4.9.6:3)
can you please let me know what I am missing?