How to get $post object available inside functions.php to localize script?

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 } }

Is there a way to know the name of all variables passed by wp_localize_script?

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

Passing variable data from external jQuery file to options.php

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

help with wp_localize_script

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

Using wp_localize_script in template file – is it secure?

Input/output related security issue can be roughly sorted into two buckets: someone manages to read information they are not supposed to; someone manages to write information they are not supposed to. Localize is not capable of writing anything into site, so you are safe on that front. On the read side it’s not much different … Read more

How to get, in WP page’s script, a wp enqueued script (in Functions.php)?

You have to make sure your script is registered with wp_register_script before your call to wp_localize_script. So in your functions.php file: add_action(‘init’,’my demo_function’); function my demo_function() { // Register the script wp_register_script( ‘myuserscript’, ‘path/to/myscript.js’ ); // Localize the script with your data $theid=get_current_user_id(); $params = array( ‘userid’ => $theid ); wp_localize_script( ‘myuserscript’, ‘MyUserParams’, $params ); … Read more

How to make sure, that only the selected post is changing?

If you want to add the post id to your javaScript, i would think of 2 methods. Output a variable and then retrieve it using javascript Let’s say you need the post it. You can create a hidden div, and assign the value of your post id to it: <div id=”my-post-id”><?php echo get_the_ID(); ?></div> Now … Read more