I am creating AJAX loading and need to use WP_LOCALIZE_SCRIPT but cannot make it work.

Here is my code. I place it in functions.php

wp_enqueue_script( 'page_data');
$gallery_js_data_array = array(
            'maxPages' => '$gallery_max_load = $gallery->max_num_pages;',
            'curPage' => '1'
wp_localize_script( 'script', 'glr_dt', $gallery_js_data_array);

2 Answers 2


You have to use the correct handle:

wp_localize_script('page_data', 'glr_dt', $gallery_js_data_array);

// Edit
Your code is also wrong. What do you want to do in this line:

'maxPages' => '$gallery_max_load = $gallery->max_num_pages;'

You are assigning a string to maxPages.

I guess you want it that way (or similar):

'maxPages' => $gallery->max_num_pages,

Now, you can access the var in your JS as glr_dt.maxPages

  • I am sorry I paste the code with wrong handle. It is not working with PAGE_DATA handle !
    – user43506
    Commented Dec 20, 2013 at 22:09

Try making the original script a dependency

  <?php wp_enqueue_script( $handle, $src, 'original_script_handle'); ?>
  • The only way I found to run it is if i give path to enqueue a file wp_enqueue_script( 'page_data',get_template_directory_uri() . '/js/gallery.js', array('jquery')); But I don't want to enqueue any file. I want just to have the those 2 vars
    – user43506
    Commented Dec 20, 2013 at 22:49

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.