The way wp_localize_script()
is supposed to be used is for that 2nd argument to be your namespace. Then you put an array as the 3rd argument for the values:
wp_localize_script(
'my-script',
'namespace_name',
array(
'var_name' => $var,
)
);
Now you can access the variable in the script with namespace_name.var_name
.
If you also use the namespace in your script, instead of initialising the object, retrieve the object created by WordPress (or fall back to a new object if it’s missing) and add to it:
var namespace_name = window.namespace_name || {};
namespace_name.new_var="Hello world!";
Related Posts:
- Passing PHP Variables to JS using Localize Script
- Using wp_localize_script to store the template url into a variable to use in JS
- How can I output a php value into a JS file within WordPress?
- Ajax random number always the same
- Integrating PHP into Javascript to display map markers with Google API – problem with wp_localize
- How to register javascript variables in wp_localize_script?
- I can’t load my images from a js file using wp_localize_script
- Import js variables loaded via wp_localize_script() into js module without global scope connection
- AJAX request status 200 but no actual “response”
- Button not refreshing page
- Ajax not working es expected (Returns 0)
- Use menu link or onClick to set a variable
- How to select the contents in the text view textrea in wp_editor?
- Creating a Multi-Level Associative Object Using AJAX
- WP functions in .js
- Adding custom directory and PHP files in wordpress
- Ajax – Call to undefined function get_option()
- Populate dropdown with Child Pages based on Parent Page chosen
- remove wp floating submenu in wp dashboard
- Displaying admin notices inside the block editor from rest_after_insert_{$this->post_type} hook
- search and replace preword from author
- How to prevent XSS alter custom global javascript object & methods in WordPress
- How to complete two other input fields, completed the first
- Status 400 for AJAX POST Request with admin-ajax.php
- How to inject variables into public-facing JS using wp_enqueue_script
- How to pass aa JS variable to PHP?
- Customize Theme comment template to Insert VoteUp and VoteDown buttons
- Image upload via FormData API and AJAX is not working ($_FILES always empty)
- I have a problem in the order of enqueues while enqueuing stylesheets and scripts for a specific page in my function.php
- JS file work only in index page
- Need Help Fixing My Iframes [closed]
- wp_enqueue_scripts leads to error
- Javascript file is not working on single.php wordpress [closed]
- How to change redirection route to a php page for making it only accessible by logged-in members?
- Save Search System
- dynamic dependent select dropdown
- Passing UTM Parameters To Modify Page In WordPress
- How to send Ajax request from my plugin in admin dashboard?
- Incorrect MIME type for assets with Valet
- Customizer: Multiple CSS styles on same element in Live Preview
- wordpress visual composer change grid builder post link
- Ajax load more button for comments wordpress
- Open post-content in archive page in a Modal box with bootstrap
- Creating own shortcode – echoeing php variable based on JS variable
- How to add a do_action on refreshing of WP customizer?
- How to pre populate a form field with a link of a current user’s author profile?
- parts of url disappear when using # inside href=”” [closed]
- How to pass Select value from Javascript to PHP to generate select option on change
- Menu jumping when calling it via PHP
- How does wp_enqueue_scripts work?
- wp attempt focus, disabling without touching core
- Setting value of session with Ajax not working
- AJAX success response is not working but it’s saving my changes
- PHP array to JS array to use in google map
- PhantomJS with wordpress
- How to add a Custom Mailchimp AJAX Newsletter Subscribe Form
- Media Upload , file name changed automatically
- Add / Update Custom Fields After Select Pictures in Media Window
- Post repeating with infinite scroll
- WordPress Scripts Being Loaded in Footer
- Fancybox type popup window that’s not an iframe
- Generate sidebar depending on height of post
- Accessing WPDB Multidimensional JSON Array w/ Javascript
- how to make my website secured
- How do I Import / Upload Files with jQuery AJAX?
- wordpress wp_enqueue_script() not working
- How to add specific meta tags to head of cart and checkout pages in woocommerce?
- Noscript not working as it should in wordpress site
- WordPress Ajax Call inserting data but success response false
- How to optimize the IF condition with many conditions and same output [closed]
- How to correctly add Javascript to this PHP ‘Page Not Found’ Page?
- Is the wp_enqueue method efficient?
- PHP echo inside javascript
- admin-ajax Firing Error 400 When Logged In
- How to add API security keys into JS of wordpress securely
- Error – ‘create_function is deprecated’ [closed]
- get taxonomy thumbnail and use it as a variable in code
- What are the advantages/disadvantages of using jQuery DOM manipulation as opposed to PHP DOM manipulation?
- Add Codepen animation as Preloader to WordPress
- element not working in Safari [closed]
- Tell WP register script to load a script only when the entire page is loaded
- Making an AJAX call to refresh a table
- HTML escaping data with ajax requests
- add onchange to select in a wp form
- Is it save to use eval for a jQuery callback method coming from the database?
- Jquery window.send_to_editor function
- load ajax using admin-ajax.php
- Get html data with javascript to php array and store to wordpress database
- How do I get an external php page to load using javascript
- 2nd page displaying the exact same posts as my first page (minus the very first post)
- trouble with page_id & highlighting current page
- wordpress admin plugin menu custom css
- How to add drag and drop functionalities to a div inside option panel
- Why is a wp function used in current PHP namespace’s callback not resolved to global scope?
- Load Posts on Click via Ajax into a DIV
- An audio player that showcase a random song from playlist every 24h [closed]
- AJAX wp_insert_user WORKS but responds with “The site is not enabled”
- trying to put an active hover to my custom nav category buttons [closed]
- How to Change CSS Colors from Custom Plugin Settings Page
- Passing the name of selected color from the custom component to `render_callback`