WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a group of javascript functions in one js file. each function acts on a specific action and get its own ajax response. some are on different pages, some are on the same page. If I don't do the localize, I can't get response. How can I localize this file for multi actions?

share|improve this question
up vote 0 down vote accepted

Maybe I got your question wrong. I don't get what you mean by:

If I don't do the localize, I can't get response.

But in general, this function lets you localize your JS strings, but you can also use it to set JS variables:

function set_my_js_var() {
    // logic here for setting the right JS var
    return "some value";
function load_fe_scripts() {
    wp_enqueue_script( 'global-js-var', get_template_directory_uri() . '/js/my_file.js' );
    $localize_array = array(
        'my_js_var' => set_my_js_var()
    wp_localize_script( 'global-js-var', 'my_global', $localize_array );
add_action('wp_enqueue_scripts', 'load_fe_scripts');

You can now acces the var in you JS File by my_global.my_js_var

share|improve this answer
Thanks for the tip, it helps! – Jenny Jul 18 '12 at 2:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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