Skip to main content

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.

Filter by
Sorted by
Tagged with
1 vote
1 answer
749 views

How to call wp_localize_script() after the script?

I am trying to use the WordPress path URL in my js script file. I read here that I need to use wp_localize_script. But, I according to Codex's documentation wp_localize_script() MUST be called ...
1 vote
1 answer
1k views

Add action hook into wp_localize_script

Is it possible to add an action hook via wp_localize_script, so I can position where I want the hook to run in HTML markup that is made in the JavaScript? So something like: $data = array ( '...
1 vote
1 answer
915 views

Integrating PHP into Javascript to display map markers with Google API - problem with wp_localize

I would display Google maps markers on custom site temple with Google API maps. I do everything like this post: Integrating PHP into Javascript to display map markers with Google API But a console ...
0 votes
2 answers
4k views

Ajax is not defined

Stack Exchange long time listener, first time caller. I have found examples on the developer.wordpress site but I have been still struggling. Localizing scripts: wp_localize_script() In my theme's ...
1 vote
1 answer
31 views

Object name undefined using localize script

Iam having problems enqueueing a script with wp_localize_script. I have done this several times before and I cannot see what Iam doing wrong here. I have changed the handle of the script several times ...
0 votes
0 answers
44 views

Uncaught ReferenceError: custom_ajax is not defined

I am trying to create an application that will add products to a cart using PHP and JavaScript + Ajax. In order to do so, I have written this script in functions.php: // Enqueue Fetch API script ...
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 ...
-1 votes
1 answer
51 views

Pass global variable data to localize_script

In my template file: product-list-section.php I loop through my products and add data to a global variable: global $product_filter_data; $product_filter_data = array(); // Loop products, grab ...
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 ...
0 votes
1 answer
146 views

Localize script not working in ajax

I'm getting the error "Uncaught ReferenceError: WPURLS is not defined" when I try to use ajax. Have I done something wrong here? Running the localize script after the script.min.js file, is ...
0 votes
0 answers
465 views

Why the wp_localize_script() does not work correct in my case? ( /wp-admin/admin-ajax.php 400 (Bad Request) )

Hi I use the next environment for adding the script and style to my WordPress theme: function theme_scripts() { // Load the html5 IE. wp_enqueue_script('iceers-html5', ' https://cdnjs.cloudflare.com/...
0 votes
1 answer
865 views

Translate javascript with WordPress built-in localization API for static strings

I have been trying to implement the translation to a couple of strings in a .js file but nothing has worked. First try to do it like I do in php, in the .js file add this variable var __ = wp.i18n.__; ...
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 ...
0 votes
1 answer
1k views

How can I get user data into a javascript object?

I have the code below in my functions.php file. However, when I try to access the variable theUser in my custom.js file it comes back as undefined. Functions.php: function theme_scripts() { ...
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 ...
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 ...
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 ...
0 votes
1 answer
309 views

localize_script but data changes dependent on product ID

I currently have this code in my functions.php file. $dataToBePassed = array(); add_action( 'wp_enqueue_scripts', function() { $handle = 'three-d-scripts'; $src = ...
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 = $...
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: ....
0 votes
1 answer
26 views

How to Object.freeze wp_localize_script

I have a localize script that have sensitive information that i dont want other users change it from the console. is it possible to Object freeze my localized script? wp_localize_script('test-...
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(), '...
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 ); ...
1 vote
1 answer
258 views

Enqueue scripts all over but not in single.php

I need to insert this script in the footer on the home, in categories, in pages, in the tags in the search but NOT in single.php: function live_search() { wp_enqueue_script('live_search', ...
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 ...
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 ...
0 votes
1 answer
1k views

Is there a better way to access transients using javascript

I've been searching around the web trying to find a better way to use WordPress transients with javascript. Currently I am using admin-Ajax to run a php function that retrieves the transient and ...
0 votes
1 answer
346 views

wp_localize_script no longer working after 5.5 update

Why this function is no longer working in my theme after WordPress 5.5 updater: add_action('wp_enqueue_scripts', function() { $vars = array('11' => '22'); wp_localize_script('jquery', 'vars'...
0 votes
0 answers
2k views

Get with jQuery the value of an ACF field

I'm trying to store in a variable the content of an ACF field so that later on I can use it in a jQuery function using the plugin "Simple Custom CSS and JS". I found what I thought was the ...
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['...
1 vote
0 answers
212 views

Ajax random number always the same

I have a button that when I click it should generate a random number using ajax ( It needs to be processed by PHP in real world application). This works fine except that every time the button is ...
0 votes
0 answers
485 views

Pass post ID to JavaScript outside the loop

I try to pass the post/pages ID to my JavaScript file. url and template_url works but the id just return 0 on every post. PHP: // Register scripts wp_enqueue_script('scripts', ...
1 vote
0 answers
77 views

Using wp_localize_scripts

I'm trying to work out how to send a PHP variable from a page template to an AJAX function (which in turn pulls data via a function in functions.php using admin-ajax.php). I understand I'm meant to ...
0 votes
1 answer
415 views

How to pass data to javascript in custom widget class

I have a custom widget where the user can insert an username in an <input> field. It's working fine, but I need to get the inserted value from php and pass it to the javascript file that is ...
0 votes
1 answer
779 views

How can I get variable from php function and use it in wp_localize_script?

I need to have dynamically created meta fields for WooCommerce product category. Almost everything works, just one thing leave to solve for me. I need to get dynamic $count variable from php and use ...
1 vote
0 answers
610 views

How to make tabulator ajax call in wordpress?

As per tabulator documentation the ajax request can be made using the format like this Ajax documentiation : http://tabulator.info/docs/4.5/data#ajax var table = new Tabulator("#example-table", { ...
1 vote
1 answer
639 views

How to pass conditional array to wp_localize_script

I'm trying to generate a custom array from a PHP function to pass on wp_localize_script but I seem to not be able to set page conditionals. I have the following function: function ...
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
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('...
0 votes
1 answer
255 views

localize_script or rest api

If the data is available in both options, in what use cases should i use the localize script to make data available to my javascript code, and in what cases should i use the rest_api? I have a custom ...
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 ...
0 votes
1 answer
192 views

issue in wp_localize_script

I am initializing wp_localize_script after enqueueing scripts like that wp_enqueue_script('main',get_template_directory_uri().'/js/mains.js','','1.1',true); wp_localize_script( 'ajax-pagination',...
0 votes
1 answer
254 views

Issue with wp_localize_script

unfortunately I have not found a solution here and via google. I get the following error message when calling: Uncaught ReferenceError: ait_php_var is not defined ../assets/js/ait_buttons.js?wp-...
0 votes
1 answer
158 views

I can't load my images from a js file using wp_localize_script

I am using a slider to show my images that works fine on my sit but when I converted my site in to a wordpress theme it started giving me troubles on loading the images, slider is active but images ...
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 ...
0 votes
1 answer
588 views

Proper use of wp_localize_script?

I want to pass an array of data to my script mygaloochart_script located in the chart.js file. Here's what I tried: //I'm no using $atts directly because of reasons $dataToBePassed = array ( ...
0 votes
0 answers
454 views

Translate string with wp_localize_script()

function s_localization() { $var = [ 'theme_assets_uri' => get_stylesheet_directory_uri() . '/assets', 'translations' => [ 'view_less' => __('View Less'), ...
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 ...
0 votes
0 answers
99 views

How to echo inside the js file with wp_localize_script?

I have used wp_localize_script() to pass a variable to a javascript file. var countries = [searchresults.data]; The variable is getting passed but I want it in a js file like below: var countries = ...
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', '...