Passing widget options to external script
You can use the get_settings() member function: public function register_widget_scripts() { $options = $this->get_settings(); wp_localize_script( ‘widget-name’, ‘obj-name’, $options ); }
You can use the get_settings() member function: public function register_widget_scripts() { $options = $this->get_settings(); wp_localize_script( ‘widget-name’, ‘obj-name’, $options ); }
Your code looks correct. alert(wp_ajax.nonce); should display the nonce. You can also verify the existence of the wp_ajax object via console.log(wp_ajax) in your browser’s Javascript console. wp_localize_script adds the object(s) to the footer (a call to wp_footer() should be present in the theme’s footer.php file). It’ll look like so: <script type=”text/javascript”> /* <![CDATA[ */ var … Read more
Passing a location-dependent array via wp_localize_script within a shortcode
Pass list of categories to JS
The wp action hook fires pretty early, before the $post object is set. I would suggest using the appropriate hook wp_enqueue_scripts instead, and amending your code to: function profolio_localize_script () { if ( is_singular( ‘my-gallery’ ) ) { $layoutType = get_post_meta( get_queried_object_id(), ‘gallery_layout’, true ); // Rest of code } }
Pass max posts to Javascript
Dynamic variable names are unpleasant to work with. A slightly better solution would be to use a single array / object which references each ‘instance’ using an auto-incremented integer. This isn’t a far cry from what you have already, but it keeps all you parameters inside on variable, and accessing a particular property of an … Read more
May i suggest: $(‘tr td .widget-remove a’).click(function(){ var toremove = $(this).attr(‘rel’); // Out of ideas – you shouldn’t really be for we are here! $.ajax({ type: ‘POST’, // use the method you want dataType: ‘json’, url: ajaxurl, // this is the url to the admin ajax script data: { nonce : your_script_data_object.nonce, toremove: toremove }, … Read more
That’s how the wp_localize_script function works. It prints all the variables before your script is included to make sure that the variables are available to the script. If you want to print the localized variables only one time, but make sure that they are accessible for all three scripts – you can do that by … Read more
How does your function pw_load_scripts() know about the existence of $dataToBePassed? Is the variable global? Looking at the code you’ve got, I’m guessing that, as far as wp_localize_script() is concerned, $dataToBePassed is a local variable, and it’s probably null.