I am testing ajax in wordpress, and it basically works but I do not want the url to be hardcoded, so I tried using wp_localize_script
to get an object to use, but I get an error saying: "ReferenceError: WPURLS is not defined" when alerting the siteurl in ajax_script.js
.
functions.php
function my_scripts_method() {
wp_enqueue_script(
'ajax_script',
get_stylesheet_directory_uri() . '/js/ajax_script.js',
array( 'jquery' )
);
}
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
wp_localize_script('ajax_script', 'WPURLS', array( 'siteurl' => get_option('siteurl') ));
ajax_script.js
$(function () {
$('#vru-btn').click(function() {
// alert('code');
$.post( 'wp-content/themes/wpcleantheme/ajax/test.php', function(data) {
$('#vru-div').html(data);
});
});;
alert(WPURLS.siteurl);
});
siteurl
but trying to accesstemplate_url
. :)