Questions tagged [wp-localize-script]
Localizes a script, but only if script has already been added. Can also be used to include arbitrary Javascript data in a page.
143 questions
31
votes
4
answers
20k
views
Is it possible to use wp_localize_script to create global JS variables without a specific script handle?
Can we somehow use wp_localize_script() to create global js variables without a specific script handle which can be accessed from all the js files, even if the js scripts are not enqueued properly by ...
11
votes
4
answers
5k
views
How to intercept already localized scripts
If a plugin uses some script (prominent example: jQuery UI Datepicker), but you're not happy with how the script renders the output, then there're two possibilities:
1. Unregister the script > Add ...
8
votes
4
answers
5k
views
Is there a JavaScript API? How to access public and private data in JS?
According to this post there is no built-in JavaScript API for Wordpress. Therefore developers who want to build on AJAX seem to come up all with their own solution which doesn't seem right to me.
...
8
votes
3
answers
3k
views
Passing boolean values with wp_localize_script
I am using wp_localize_script to pass a couple of values from my theme options to a javascript file. First I got the values from my theme options:
$options = get_option('theme');
$flex_auto = $...
7
votes
2
answers
3k
views
What is the correct way to build a widget using OOP
I am working on a simple search form widget, with a built in autocomplete capability (You can download the current version here). The plugin is working, but I am currently rewriting all the code using ...
7
votes
1
answer
3k
views
Multiple wp_localize_script
Is it possible to localize a script more than once? I want to send two separate arrays of params.
Or is it only possible to localize a script once, in which case, I'll have to combine the arrays?
...
7
votes
2
answers
5k
views
Creating Multiple wp_localize_script for Shortcode?
I have a shortcode that displays recent WooCommerce products in a carousel, however i would like the end user to be able to use the shortcode multiple times on the same page, currently when this ...
5
votes
1
answer
626
views
Do I need to escape data passed to wp_localize_script()?
Title pretty much says it all. I took a look at the source, and I didn't see any escaping being done. But, I can't say for sure.
wp_localize_script( 'script-hook', 'object_name', array(
'param_1' ...
5
votes
1
answer
1k
views
w3 total cache minification breaks wp_localize_script() [closed]
I am trying to configure W3 total cache plugin to speed up my site. The minify option is great but I am running into the following issue:
Let's say I have a JavaScript file I am attached wp_localize ...
4
votes
1
answer
4k
views
wp_localized_script is not defined when called via jquey ajax
I am struggling to get my ajax request to work. It fails when I fire my $ajax and I get this error...
Uncaught ReferenceError: feature_ajax is not defined
This is my functions.php
// load our ...
3
votes
4
answers
1k
views
wp_localize_script with boolean and init
I have problem with wp_localize_script, That I cannot get boolean and int as variable
wp_enqueue_script( 'helloworld' , 'helloworld.js', false, '1.0.0', true);
$site_config = array();
$site_config['...
3
votes
1
answer
3k
views
enqueue and localize script in footer
I have a script which must run in my footer, after some variables are declared. It works if I just put the code directly in my footer file, but I think best practices dictate I should do this via ...
3
votes
3
answers
1k
views
wp_localize_script $handle
Can anyone tell me what the "$handle" ( first parameter ) of wp_localize_script is normally used for. Thanks.
P.s.: I have no idea why but stackexchange is telling me this question dosen't meet ...
3
votes
1
answer
3k
views
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 ...
3
votes
2
answers
757
views
Nonce best practices: hidden input vs. wp_localize_script?
I noticed while looking at the HTML of an edit page that WordPress uses a lot of hidden input elements for storing nonces. Is there a significant advantage to using either this style of storing ...
3
votes
2
answers
933
views
Use wp_localize_script for non existing script
Task: What I'm trying to do
I'm fetching data via the WP Http API from a remote server. The data I'm getting back is JSON encoded. In one of my methods, I'm converting them back and then push it into ...
3
votes
0
answers
1k
views
How to overwrite JS variable with wp_localize _script?
The function wp_localize_script( $handle, $name, $data ); does not work when trying to overwrite the same variable, that is when calling the function second time with the same $handle and $name but ...
2
votes
1
answer
2k
views
How do you pass a boolean value to wp_localize_script [duplicate]
What is the best way to pass a boolean into wp_localize_script as from what I can see everything is interpretted as strings.
I have a backend user options form for a plugin, when the values are ...
2
votes
2
answers
1k
views
WP_LOCALIZE_SCRIPT doesn't work
I am creating AJAX loading and need to use WP_LOCALIZE_SCRIPT but cannot make it work.
Here is my code. I place it in functions.php
wp_enqueue_script( 'page_data');
$gallery_js_data_array = array(
...
2
votes
1
answer
21k
views
Use of global variables within plugin [closed]
I am creating my first plugin ever and I found my self having to call the same option variables within various parts of the same page therefore I sought for a solution and stumbled upon global ...
2
votes
2
answers
1k
views
Localize variable for multiple Shortcodes
I have this code for flexslider Shortcode
add_shortcode('flexslider', function($atts){
global $post;
$ids = explode(',', $atts[ids]);
$uniqid = uniqid();
wp_enqueue_script( 'shortcode_flexslider');
...
2
votes
1
answer
2k
views
wp_add_inline_script not adding when script_loader_tag filtered
I'm trying to add the new GA4 code to an existing site, and there seems to be some conflict / ordering issue between script_loader_tag filters and wp_add_inline_script. My functions.php looks thusly:
....
2
votes
2
answers
9k
views
How to Globally Use wp_localize_script() Ajax URL
I have added this to my functions.php and need to use ajaxURL in all of enqueued scripts in the template (instead of enqueuing only one script here
add_action( 'wp_enqueue_scripts', '...
2
votes
1
answer
3k
views
How to localize inline script called with ajax
I have a loop of posts and I allow a user to edit each post using ajax. When the edit button is clicked for the post I use ajax to retrieve the template part containing the edit form for the post. ...
2
votes
1
answer
658
views
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 ...
2
votes
2
answers
581
views
Admin-Ajax Error
I making homepage in the local host.
I need wp_query with Ajax.
but there's some error. I don't know why. Can you help me?
----> this is load_more_ajax.js
var page = 2;
var date_pass = "<?php ...
2
votes
1
answer
464
views
Pass multiple PHP variables to JS in Options Page
I'm passing variables from options to the front-end using wp_localize_script. Issue is that now I have 71 input fields in the options page with these ids: calc_m_transport_1, calc_m_transport_2 and so ...
2
votes
1
answer
407
views
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 ...
2
votes
0
answers
777
views
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
);
...
2
votes
0
answers
2k
views
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 ...
2
votes
0
answers
932
views
Wp_localize_script from Shortcode [closed]
I've been trying for hours and tried several answers from here and Google but still no luck.. need help.
I am making a shortcode (slider) and trying to execute a javascript file (for the slider's ...
2
votes
1
answer
43
views
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 ...
1
vote
1
answer
2k
views
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 ...
1
vote
1
answer
4k
views
Using wp_localize_script to store the template url into a variable to use in JS
I was trying to store the template url (bloginfo(template_url);) into a variable so I could use it in my JavaScript. However, I realized this wasn't possible and someone alerted me to ...
1
vote
1
answer
880
views
wp_localize_script and host/browser cache
I am writing a custom made plugin that will have advanced JS scripts, in order to preload relevant data i am localizing scripts.
PHP
$post_array = my_function_to_get_my_data();
wp_localize_script('...
1
vote
1
answer
2k
views
Can wp_localize_script be used within a shortcode?
I am developing a plugin, and I would like to make data available to the client-side JS code executed by one of my shortcodes. wp_localize_script seems like the obvious way to do that, but it's not ...
1
vote
1
answer
4k
views
How to get the post ID when creating JS variables with localize_script
how do you set the current post ID as a JS variable with localize_script? It seems like the $post variable isn't availible in the functions.php file. When is it created? Do i have to add localize ...
1
vote
2
answers
1k
views
esc_html__ security : what for in this example?
how does esc_html__ (the 2nd one) protect the $message variable from being hacked ? what's the point to use this protection here (the second one with a plain text)?
<?php
function unknown(){
...
1
vote
1
answer
1k
views
How to add ajax url to js using wp_add_inline_script()?
I usually enable ajax in a js script by using wp_localize_script like this
wp_localize_script( 'map-scripts', 'ajax_info', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
but now I see it ...
1
vote
1
answer
3k
views
Using wp_localize_script inside template page to transfer var from php to js
I have been looking for a help on the forum, but nothing matches really my problem.
I am actually trying to use bxSlider with Wordpress. My problem is that where I create my gallery image (a ...
1
vote
2
answers
6k
views
Get Current User info using wp_localize_script, in functions.php
I need to pass the info from wp_get_current_user() to the front end for a script that uses it. To achieve this, I am using wp_localize_script() to pass the information. I put the code at the top of my ...
1
vote
1
answer
795
views
How can I output a php value into a JS file within Wordpress?
I am trying to use get_theme_mod option from php to load into my js file. I've used wp_localize_script to convert. The tweet_id is where I need to place the $tweet value outputted.
Any help would ...
1
vote
1
answer
191
views
Transate plugin with js & wp_localize_script
I'm trying to translate a plugin with JS functionality.
I was able to translate the classic php part with a .po file etc...
In my plugin there are 2 words in js that I would like to translate and I ...
1
vote
1
answer
694
views
WordPress wp_localize_script nonce and ajax URL
I'm making an Ajax call in an Elementor site, and I'd like to use a nonce while doing it. Since my code is really simple, I didn't create a new JS file for it placed in Elementor's HTML widget, and I ...
1
vote
1
answer
325
views
Localization of JavaScript which is only used in one page
Environment:
WordPress 5.8
WPML: 4.4.10
DIVI: 4.9.4
What I am trying to achieve?
I have a JavaScript validation script which should validate input on the registration page. This script shows error ...
1
vote
1
answer
710
views
how to send Ajax request in wordpress backend
On the fronted the wp_localize_script seems to be working as when I view the source I can see the nonce.
wp_localize_script('scripts', 'myAjax', array(
'root_url' => get_site_url(),
'...
1
vote
1
answer
381
views
Populate a Map at The Same Time as showing Posts via AJAX
I have a map search page on my site. I'm able to display all the locations in a list in HTML but I am struggling to pass the locations to JavaScript at the same time in order to populate the pins on ...
1
vote
1
answer
2k
views
wp_localize_script not working on ajax response
When a file is uploaded to my page I make an ajax call to add an entry in a database. I then want to send a string back to the client using wp_localize_script but when I try to access it I just get an ...
1
vote
1
answer
173
views
Different uniqid when calld in wp_localize_script and shortcode
So I am making a shortcode and trying to integrate vue + wp_query and having difficulty in making it work when I use multiple shortcodes on the same page with different attributes. I am using the code ...
1
vote
1
answer
705
views
Script Localization doesn't work
I don't get how script localization works in wordpress.
I created an associative array in php:
$translations = array(
'value1' =>'This is first value',
'value2' =>'This is second ...