In general, this function lets you localize your JS strings, but you can also use it to set global JS variables:
function set_my_js_var() {
// logic here for returning the right JS var
$myVar = "Hello World";
return $myVar;
}
function load_fe_scripts() {
wp_enqueue_script( 'global-js-var', get_template_directory_uri() . '/js/my_file.js' );
$localize_array = array(
'my_js_var' => set_my_js_var()
);
wp_localize_script( 'global-js-var', 'my_global', $localize_array );
}
add_action('wp_enqueue_scripts', 'load_fe_scripts');
You can now acces the var in your JS File by my_global.my_js_var
Related Posts:
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
- WordPress Ajax always returns a 404 error
- Click loads template via ajax
- Display notification only once
- Access/update database with jQuery
- How to display something in a div when the user clicks on a text in another div – no page refresh [closed]
- How to speed up admin-ajax.php in wordpress
- Colorbox ajax loading of outside HTML content works perfect on localhost but not on server
- Sending jsPDF documents to the server
- Ajax return code 400
- How to test nonce with AJAX – Plugin development
- class click counter save number
- AJAX request status 200 but no actual “response”
- Creating a Multi-Level Associative Object Using AJAX
- Save Search System
- dynamic dependent select dropdown
- How to add a do_action on refreshing of WP customizer?
- Setting value of session with Ajax not working
- How to add a Custom Mailchimp AJAX Newsletter Subscribe Form
- Fancybox type popup window that’s not an iframe
- How do I Import / Upload Files with jQuery AJAX?
- WordPress Ajax Call inserting data but success response false
- Why i can’t get custom fields value or post ID via Ajax?
- Accessing an API with jQuery and AJAX
- Change Query Arguments (filter) with jQuery/Ajax or PHP?
- WordPress cascading dropdown selection search based on Parent Page & Child Page
- Tracking Visitor LatLng with WordPress using JS, PHP. How to put data which was extract using JS into DB
- Admin-ajax.php 404 error
- How to pass values from one function to the other using an array variable
- How to use wp_ajax_set_post_thumbnail?
- Update $wpdb query with AJAX
- AJAX to add to cart multiple products woocommerce
- How can i send id of the currently posted form data through json response in custom wordpress plugin?
- Cannot successfully execute AJAX script to call function.php specific function. Using XAMPP localhost to test
- How to echo a PHP code into WooCommerce variation product?
- How can i get the same ajax result using WP REST API instead of admin-ajax?
- How to call a function from functions.php with ajax?
- number of posts with “Load More”
- Find the method which AJAX GET calls
- AJAX Filter WHILE Loop not working WordPress
- Why I’m Not Having Access to “$_POST” Data Outside My AJAX Callback?
- 500 (Internal Server Error) of external php file
- Dynamic Gallery
- JSON Object Value Show Undefine
- notify users when changed database
- Ajax success function
- Do I require the use of nonce?
- Security – Ajax and Nonce use [closed]
- Nice scroll to wordpress
- Looking for a better way to handle an ajax script that pulls in post data on click
- Can’t use get_results() in ajax query
- Passing jQuery value using Ajax to a page template
- ajax_object is not defined when ajax calls the function
- Page Reloads Before AJAX Request Finishes
- Why is this Ajax not working?
- How to select the contents in the text view textrea in wp_editor?
- ajax, right way to do it and make it works?
- WordPress ajax doesn’t display object method on jQuery .change() function
- Ajax date_query not working past the first iteration
- Menu jumping when calling it via PHP
- WordPress admin-ajax.php 400 bad request
- Ajax contact form return 0
- AJAX – Returning Two JSON Objects with One PHP Function
- admin-ajax Firing Error 400 When Logged In
- Add Ajax Hooks and Call from Custom Template PHP
- WordPress Ajax Always returning 0
- Jquery window.send_to_editor function
- How to keep track of when a vote is cast?
- How to add drag and drop functionalities to a div inside option panel
- AJAX wp_insert_user WORKS but responds with “The site is not enabled”
- My jQuery Ajax form submit is still refreshing page?
- Submitting a form, using Ajax, to run a SQL Select query based on user input from the form
- Incrementing PHP variable onclick to display posts by month
- JS file is not loading when i put wp_head() and wp_footer
- Covert WordPress Blogname into JQuery
- Hot to check if new posts have been published since page load and notify online users?
- Display WordPress search results based on the selected Sub-Category
- jquery & ajax getting data from php in wordpress . admin-ajax.php returns 400
- Settings API form – submit with AJAX
- How do I call a value from the datase using ajax
- Post form – AJAX won’t upload featured image – Plugin development
- Where to add my PHP codes for AJAX Jquery to work?
- How can i find wrong word in search box and replace with correct word
- How to reload a php script, using AJAX and jQuery
- Filtering custom posts by using checkboxes for taxonomy in an ajax loop always gives server 500 error
- Show the subcategory name based on current product
- ajax polling with admin-ajax.php
- Bootstrap Carousel Indicators Won’t Cycle On Custom WordPress Theme
- Is there a reason why my wordpress PHP page isn’t loading into my PHP template
- A javascript function that simply runs a php function on the plugin
- Ajax not updating to database
- WordPress 404 when using AJAX to call php function
- how use php query in AJAX jquery
- Submitting my form to the database and then redirecting to payment site
- Pass the updated value of aid from form using ajax
- Execute PHP code in Javascript onclick
- best way to communicate between php and jquery/javascript
- Add specific phrase after every product title including the discounted price? [closed]
- AJAX: WordPress filters inside $html do not work as intended
- Why not showing all post by default in my jquery filter