i'm really getting crazy about this. after hours of trials, i can't get out of this.

I'm trying to pass a php variable value to an enqueued jquery script but i can't.

here is my code.

php :

function itinerary_enqueuescripts(){
wp_enqueue_script('itenqueue', JSONURL.'/js/loadjson.js', array('jquery'));
wp_localize_script( 'itenqueue', 'itajax', array( 'ajaxurl' => "hithere" ) );
add_action('wp_enqueue_scripts', 'itinerary_enqueuescripts');

in my loadjson.js file i have a simple

var _latitude = 43.309; 
var _longitude = 8.499;
var jsonPath = itajax.ajaxurl; //and should transform, in this example, in hithere

But when the script is enqueued, the var jsonPath as the value of itajax.ajaxurl :)

The scripts gets enqueued correctly, in this example i make a simple variable declaration (just to let out problems.. e.g. the script gets enqueued too early).

Funny thing (and perhaps here's the clue, this is quite a copy/paste from another plugin of mine, for a function that points to admin-ajax.php for front end posting. It's the same syntax, with different variable names of course, but in this case it doesn't work).

Thanks for any clue. (i tried quite anything, registering-localizing-enqueueing, enqueueing in wp_footer at the very last.. even changing double qoutes to single quotes :) Now i suppose i really must miss something.

2 Answers 2


JS variables are not directly replaced as you are expecting. Localizing in your code is correct.

When you do var jsonPath = itajax.ajaxurl;, value of itajax.ajaxurl is assigned to the variable jsonPath.

If you debug this variable in the console, you will get the correct value. console.log(jsonPath);

  • of course.. wordpress adds the variable as a global scope var! Commented Jun 22, 2015 at 8:37

Seems like you need to add __ before your variable value, try this code:

wp_localize_script('itenqueue', 'itajax', array(
    'ajaxurl' => __('hi there')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.