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
- I got this error POST https://localhost/meraboilerwp/[object%20Object] 404 (Not Found)
- jQuery AJAX cross domain
- Most efficient way to add javascript file to specific post and/or pages?
- Pass media upload value to input field
- Real time Duplicate title check
- Load custom field value into div with AJAX
- Jquery ajax to custom php file: returning blank data
- Data fetch and delete by ajax request
- Stumped – Ajax Response Returns 0
- Parsing php string in jquery [closed]
- doing an ajax request always outputs 0
- Using shipped version of jquery
- Trying to implement Ajax comments in WordPress, getting WP error
- Woocommerce add to cart quantity buttons with AJAX
- wordpress count link clicks by ip address
- Ajax – Call to undefined function get_option()
- Ajax load more posts with multiple tax query
- WordPress Sending data to Ajax with select option
- Ajax request not sending to server and returning – wp-admin/admin-ajax.php 400
- populate form fields in a loop with ajax
- How to send Ajax request from my plugin in admin dashboard?
- How to pass Select value from Javascript to PHP to generate select option on change
- AJAX success response is not working but it’s saving my changes
- AJAX not Working with php as supposed
- Post repeating with infinite scroll
- Plugins not working on AJAX requests
- How to disable controls in theme customizer?
- HTML escaping data with ajax requests
- load ajax using admin-ajax.php
- wordpress add_action() issue in ajax call
- Function won’t run onclick using Ajax
- Get related product based on subcategory selected from category id with ID 142
- jQuery Ajax PHP function call returning [object Object]
- Retrieve Json data and create multiple pages with it
- Using Ajax to submit a form, and run a SQL Select query based on user input from the form
- WordPress Search Ajax + Isotope + InfiniteScroll
- Failed to load resource: the server responded with a status of 500 (Internal Server Error) through wp_admin_ajax.php
- How to call my PHP function with AJAX ? wp_query
- Can’t pass var from php wp_ajax into ajax script : result undefined or null
- Ajax filter with loadmore button
- $.ajax results in 403 forbidden
- WordPress ajax filter returning all posts when it should be filtering by category
- ACF + WP + Slick Slider Repeater Slide Group PHP not Displaying [closed]
- Default Image not showing up in Customizer for WP_Customize_Cropped_Image_Control
- How to create a radio button to alt between settings in wp_options table
- WordPress AJAX load post metadata in modal
- How can I correctly POST to a WordPress page?
- Adding instant search to wordpress page
- Load php function with AJAX on modal window
- Adding jquery using php function
- How to hide .js files on wordpress website using php or wp plugins or any? [closed]
- Remove Woocommerce product from cart with ajax/fetch
- WooCommerce Modal Fly cart on add to cart – With AJAX
- Upload multiple files via ajax from an HTML file input
- get_post_meta returns on side but it doesn’t return on bottom of admin page