Pass list of categories to JS
Pass list of categories to JS
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.
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
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
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