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

Leave a Reply

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