Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

-1
votes
0answers
27 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 = ...
1
vote
1answer
17 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 ...
1
vote
2answers
83 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', '...
1
vote
0answers
47 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 ...
3
votes
1answer
62 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
1answer
18 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 ...
0
votes
1answer
82 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 ...
0
votes
1answer
63 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: ...
0
votes
0answers
134 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 ...
0
votes
1answer
35 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',...
0
votes
1answer
55 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 ...
0
votes
1answer
24 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){ ...
2
votes
0answers
853 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 ...
0
votes
1answer
52 views

What about Deferring a localized script?

I would love to defer the loading of the script for a plugin, Simple Locator, which uses Google.maps API. However it uses wp_localize_script, which prints a wpsl_locator javascript object in the HTML ...
0
votes
1answer
105 views

mutiple shortcode instance on the same page

i'm building a google map shortcode plugin and i use wp_localize_script to pass variables to js script like (lat , lng , zoom...) . but i have an issue : when i use this shortcode twice or more in a ...
0
votes
1answer
65 views

wp_localize_script() and JavaScript namespaces

As you are aware, wp_localize_script() allows the creation of a JavaScript object from a PHP object. So, after running the code in the example below: wp_localize_script('my-script', 'var_name', $var)...
0
votes
0answers
20 views

PHP/jQuery flow in WordPress Code - Can it be simplified?

I'm working on some plugin code I've inherited and I'm not quite experienced enough to know if the way it currently works is as over-complex as it seems to be. I know that the project has had several ...
1
vote
2answers
345 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 ...
0
votes
1answer
118 views

Can't access PHP array inside script localization from javascript

I have a loop to retrieve all users with the specific meta_key user values from wp_user_meta database and then put it in wp_localize_script array so I could access the data with Javascript and do my ...
0
votes
0answers
27 views

wp localize script makes variable global, how to solve that?

I don't want my variable "infinite" to be global variable. but it's like wp_localize_script would make it global automatically. How to solve that? This is my code. Wordpress php file: public ...
1
vote
1answer
220 views

How to enqueue the script without hardcoded in the theme files?

I have been working on the Custom Gallery Layout style and was wondering how do I enqueue and make the following script working without hard coded in the file. Or is there a better way of adding it ...
1
vote
0answers
88 views

How to register javascript variables in wp_localize_script?

When translating PHP-strings with variables, I use the following approach: sprintf(__('Some text <b>%s</b>, more text.','my-plugin' ), $somestring); When translating javascript strings, ...
1
vote
1answer
286 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 ...
0
votes
1answer
64 views

Multiple shortcodes only use attributes from one of the shortcodes on custom plugin

Created a shortcode, however when multiple shortcodes are used on a page the $atts passed in are only from the last shortcode that was used. What am I doing wrong? public function init(){ ...
0
votes
1answer
69 views

How to make sure, that only the selected post is changing?

So I was able to save the content of each post in an array, thanks to a very good answer on it: How to localize value of posts I just copied the solution here: function register_and_enqueue_script() ...
0
votes
1answer
68 views

How to localize value of posts

I've got a problem again. I scripted a little JS function, that alters content of a post. To pass the content to the function I used this: function register_and_enqueue_script() { ...
3
votes
1answer
154 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 ...
1
vote
1answer
884 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 ...
0
votes
1answer
330 views

WordPress Ajax Filter

I am trying to get the AJAX filter to work in Wordpress but I keep getting an error: Uncaught ReferenceError: reports is not defined filter.js?ver=1.0:5 This is the plugin that I use to enqueue the ...
0
votes
1answer
50 views

How to get, in WP page's script, a wp enqueued script (in Functions.php)?

I’m very new to WP, and to JS as well, and need to do a simple change to existing code, so forgive my ignorance… For getting the current User Id I followed examples and added an action + function to ...
1
vote
2answers
2k 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
votes
1answer
527 views

How to define variables in WordPress AJAX?

I have a plugin that works fine, so I was a little shocked when I turned debugging mode on the other day. The first notices are telling me that the variables in the wp_localize_script are undefined. ...
2
votes
3answers
266 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['...
2
votes
1answer
32 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 ...
0
votes
1answer
524 views

wp_localize_script not passing the data

I've been trying to work with a small plugin and I tried to pass my data to js via wp_localize_script but when I'm doing console.log() inside my js it is returning undefined. Can anyone tell me what ...
0
votes
0answers
162 views

Using shortcode variables with wp_localize_script

I am trying to use wp_localize_script to pass variables from a shortcode without success. The following would work fine: wp_enqueue_script('script_handle', plugin_dir_url(__FILE__) . 'script.js', ...
-2
votes
1answer
393 views

Custom Backend data for map to JSON file?

I want to create a map in the frontend with data that is populated with Point of interests. I want to manage the POI data in the backend but need a generated JSON file from that backend data that ...
0
votes
1answer
269 views

Using wp_localize_script in template file - is it secure?

I have the following code inside one of my template files (NOT functions.php) $datatoBePassed = array( 'pageTitle' => get_the_title() ); wp_localize_script( 'main-js', '...
1
vote
1answer
251 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 ...
0
votes
1answer
323 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
1answer
712 views

Getting a variable inside foreach from PHP to JS after localization

I believe I have properly enqueue'ed and localized my script - I've got $q showing up in the DOM. Now I'm sure I'm missing the simple last step to accessing a variable in the JS function. Here's my ...
0
votes
1answer
1k views

Ajax call with javascript in post content (not enqueued)

I would like to make an Ajax call within a JS which I do not wish to enqueue for the whole website. To do that, I put the script directly at the end of the post for which it is relevant rather than ...
4
votes
1answer
284 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' ...
1
vote
1answer
1k views

WordPress Ajax problem with undefined url

I have problem with wordpress plugin - in jQuery.ajax part, the ajax_object (url: ajax_object.ajax_url) is undefined and I don't know why. I cant recieve correct code from my php plugin, but if I ...
1
vote
1answer
460 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 ...
0
votes
1answer
391 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 ( '...
0
votes
1answer
393 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
1answer
206 views

help with wp_localize_script

I am a little confused on this function. I have it working for one javascript variable. I tried to get it working for two others and it found that it repeats the code in the header 3 times. I tried ...
0
votes
1answer
961 views

Problem in wp_localize_script

I am try to enque and localize my word-press scripts My code for for enque my scripts is function load_my_scripts() { wp_register_script('custom-js', get_stylesheet_directory_uri() . '/js/custom....
0
votes
1answer
547 views

Update Data parameter of a wp_localize_script() call

I'm working on a child theme, In my-page-template.php I have : //... $current_key= 5; //calculated result wp_localize_script('my_js', 'ajaxload', array('post_id' => $current_key)); In my_js.js I ...