Dark

Light

Dark

Light

Scroll to top

Localization of script in wordpress

/* Enqueue script and Localize */

function my_scripts(){
	wp_enqueue_script("custom-script", get_stylesheet_directory_uri()."/custom.js", array('jquery'));
	wp_localize_script( 'custom-script', 'myajax', array('ajaxurl' => admin_url( 'admin-ajax.php' )) );
}
add_action("wp_enqueue_scripts","my_scripts");

Now you can use ajax url in js file with variable myajax.ajaxurl

Author avatar
Amitpal Singh
https://amitpalsingh.com

Post a comment

Your email address will not be published. Required fields are marked *