Does wp_localize_script call a Javascript Function or Simply Pass PHP Parameters to It

Easy question: I’m trying to understand wp_localize_script. Does wp_localize_script call a JavaScript function or does it simply pass PHP parameters to JavaScript? I want to pass PHP parameters to a JavaScript function and call the JS function. 1 Answer 1 wp_localize_script takes an array of data (in PHP) spits out a javascript. It’s a way … Read more

Ajaxing in functions.php

To ajax filtered posts, I needed to put most of my index.php template into the functions.php file (followed this tutorial) and do from there. But now javascript won’t work – basically the accordion style listings won’t open and close like they did before, trailers won’t embed, etc. Click on a tag to demo here. Is … Read more

Remove type attribute from script added by wp_localize_script

How can I remove type attributes from all scripts added by wp_localize_script ? Have the same problem with W3C validation as described here – Remove type attribute from script and style tags added by WordPress But solution described there cannot help with wp_localize_script script’s. — As a substitute I’ve used another function wp_add_inline_script and solution … Read more

Nonces, AJAX, script variables & security in WordPress

Alright, so let’s say you develop a website where you enqueue a js script on a page X of your frontend, using: wp_enqueue_script( ‘script_handle’, PATH_TO_SCRIPT, array(), ‘1.0.0’, true ); Inside this script, you use a simple AJAX request. You then use the nonce feature of wordpress to have what is stated as a safer AJAX … Read more

Can not pass the value of google pie chart from custom meta box

I am trying to build a google pie chart in my wordpress site based on user input . Hence I have created custom meta box , and use wp_localize_script . Here is what I have done to pass custom metabox value to wordpress . function pie_load_scripts() { global $post; $title = get_post_meta($post->ID, ‘title’, true); $telecom … Read more

Setting HTML properties in a Gutenberg plugin using WordPress settings

I want to set the maxlength property on a TextControl I define in a Gutenberg plugin. This length is to be set from a setting in my WordPress plugin (the one that loads the Gutenberg plugin). I attempted the standard approach of passing PHP data to JavaScript (using wp_localize_script, discussed for example here), but I … Read more

How to add extra attributes to the script tag added via wp_localize_script()

So, wp_localize_script() are awesome to add custom data to a script. And these data gets added to the HTML like: <script id=’woocommerce_some-js-extra’ type=”text/javascript”> var wc_some_params = {“key”:”sdfsfsdfs”,”use_iframes”:”1″,”images_dir”:”https:\/\/dadadasdad.com\/wp-content\/plugins\/woocommerce-some-gateway\/assets\/images”}; </script> This is all good. But the problem is that if I want to add an extra attribute to the script tag and instead of showing it like … Read more