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
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 ...
LWS-Mo's user avatar
  • 1,562
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 ...
BrigantiumXIX's user avatar
-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 ...
wharfdale's user avatar
  • 348
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 ...
user avatar
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 ...
matt7861's user avatar
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/...
Victor Sokoliuk's user avatar
0 votes
1 answer
864 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.__; ...
choseɳ's user avatar
  • 17
1 vote
1 answer
693 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 ...
yaara's user avatar
  • 13
1 vote
1 answer
324 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 ...
HHeckner's user avatar
  • 111
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 = ...
sico87's user avatar
  • 209
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 ...
Marc's user avatar
  • 357
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: ....
philolegein's user avatar
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-...
Or Shalmayev's user avatar
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(), '...
user8463989's user avatar
2 votes
0 answers
776 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 ); ...
DevelJoe's user avatar
  • 497
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', ...
fr0zen's user avatar
  • 11
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 ...
iSaumya's user avatar
  • 936
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'...
Ahmed Saad's user avatar
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 ...
Jonattan Salcedo's user avatar
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() { ...
Lz430's user avatar
  • 137
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 ...
Robert Morel's user avatar
0 votes
0 answers
483 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', ...
Axel Murman's user avatar
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 ...
JohnG's user avatar
  • 344
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 ...
sialfa's user avatar
  • 329
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 ...
Daniel Nebert's user avatar
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 ...
Artus2000's user avatar
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", { ...
user avatar
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 ...
markb's user avatar
  • 299
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 ...
Burgi's user avatar
  • 403
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('...
Jess_Pinkman's user avatar
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 ...
fogx's user avatar
  • 101
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 ...
dreddx's user avatar
  • 13
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',...
Flutterer's user avatar
  • 115
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-...
hage's user avatar
  • 1
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 ...
FSK's user avatar
  • 3
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 ...
shubhra's user avatar
  • 148
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'), ...
Asking's user avatar
  • 121
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 = ...
user avatar
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 ...
Piotr Mucha's user avatar
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', '...
Behseini's user avatar
  • 525
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 ...
Sean's user avatar
  • 667
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 ...
joshmoto's user avatar
  • 457
1 vote
1 answer
748 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 ...
Alfrex92's user avatar
  • 131
0 votes
1 answer
1k views

Using wp_localize_script to get data from cpt and pass it to maplace-js locations

I'm currently building an Dealerlocator with ACF and CPT. For the dealerlocator to shop the dealers on the map I want to use MAPlace-JS. This works fine when adding an array to the locations variable ...
Wouter's user avatar
  • 13
0 votes
1 answer
388 views

bundled jquery in theme js not working with wp_localize_script

I've bundled jquery in my theme js using grunt-uglify. Now I want to prevent wp jquery from loading since I don't need 2 jquery versions and want to save unnecessary requests. This is what I have: ...
Tom M's user avatar
  • 101
0 votes
0 answers
730 views

Frontend AJAX Request causes Error: 'Call to undefined function add_action'

I wrote a little plugin that is supposed to handle a form submission and send some mails. When the form is submitted the request fails. Im on a local MAMP Environment and my PHP error log shows me ...
user avatar
0 votes
1 answer
58 views

Ajax WordPress pass post URLs

I'm using wp_localize_script to send post data to ajax. wp_localize_script( 'my-script.js', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php')) ); add_action( 'wp_ajax_load_more_posts',...
CyberJ's user avatar
  • 437
0 votes
1 answer
443 views

Why is wp_localize_script returning false?

I am enqueuing the script.js via a plugin succesfully but the wp_localize_script is not working and returning false. I have no clue why... object_name is unavailable in the script.js when I remove ...
user avatar
0 votes
1 answer
387 views

Ajax Response Error | just getting error as the response

Below is the code for the AJAX File. alert(cont) is given the correct result but the problem is with the response. I am just getting error as the response. function updateRadioButton(rating,pid){ ...
Mohit's user avatar
  • 111
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 ...
rappongy's user avatar
  • 121