WP_LOCALIZE_SCRIPT doesn’t work

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 … Read more

How do you pass a boolean value to wp_localize_script [duplicate]

Unfortunately wp_localize_script() casts all scalars (simple types) in the passed-in array to strings (and then runs html_entity_decode() on them?!), so the casts mentioned by the answer you quote & @TheDeadMedic will get stringified “1”https://wordpress.stackexchange.com/”” if boolean, and number strings if ints, which won’t work with javascript plugins that demand exact values. A way around it … Read more

How to localize value of posts

Try: function register_and_enqueue_script() { if(‘myPostType’ == get_post_type() && have_posts()) { wp_register_script( ‘js_script’, plugin_dir_url(__FILE__).’js/script.js’, array(), ‘1.8.5’ ); wp_enqueue_script(‘js_script’); $myCustomValue = array(); while(have_posts()) { the_post(); $mypostid = get_the_ID(); $myCustomValue[”.$mypostid] = nl2br(get_post_meta($mypostid, ‘custom_value’, true)); } // end while rewind_posts(); wp_localize_script(‘js_script’, ‘myCustomValue’, $myCustomValue); } // end if }

Can’t seem to get wp_localize_script to work

I tested that code like this: wp_enqueue_script(‘jquery’); wp_localize_script( ‘jquery’, ‘MS_Ajax’, array( ‘ajaxurl’ => admin_url( ‘admin-ajax.php’ ), ‘nextNonce’ => wp_create_nonce( ‘myajax-next-nonce’ )) ); And it works but throws a Notice. It should be hooked to wp_enqueue_scripts like: function my_enqueue_scripts() { wp_enqueue_script(‘jquery’); wp_localize_script( ‘jquery’, ‘MS_Ajax’, array( ‘ajaxurl’ => admin_url( ‘admin-ajax.php’ ), ‘nextNonce’ => wp_create_nonce( ‘myajax-next-nonce’ )) ); … Read more

Passing PHP Variables to JS using Localize Script

Your code should be in an action callback function: function wpse186202_enqueue_scripts(){ wp_enqueue_script( ‘stats’, get_stylesheet_directory_uri() .’/js/t5-demo.js’ , array( ‘jquery’ ), ‘1.0.0’, true ); $categories = implode( ‘, ‘, wp_list_pluck( get_the_category( get_the_ID() ), ‘name’ ) ); $datatoBePassed = array( ‘author’ => get_queried_object()->post_author, ‘category’ => $categories, ‘title’ => single_post_title( ”, false ) ); wp_localize_script( ‘stats’, ‘php_vars’, $datatoBePassed ); … Read more

Problem in wp_localize_script

First of all, you are NOT enqueuing the script.. Try this code, working in my test install. function load_my_scripts() { // Create the localizations array $localize = array( ‘ajaxurl’ => admin_url( ‘admin-ajax.php’ ), ); // Register The Script First wp_register_script(‘custom-js’, get_stylesheet_directory_uri() . ‘/js/custom.js’, array(‘jquery’)); wp_enqueue_script(‘twentyfifteen-custom-js’, get_template_directory_uri() . ‘/js/custom.js’, array(), ‘20141010’, true); // Register the localization … Read more

Localize variable for multiple Shortcodes

Your ploblem is that wp_localize_script print to the html markup a javascript object similar to: var slider = {“id”:”a_unique_id_here”}; if you call it more times, e.g. using more shortcodes in same page, whati is printend in html markup is var slider = {“id”:”a_unique_id_here”}; var slider = {“id”:”another_unique_id_here”}; var slider = {“id”:”third_unique_id_here”}; so you are overwriting … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)