Maybe I got your question wrong. I don’t get what you mean by:
If I don’t do the localize, I can’t get response.
But in general, this function lets you localize your JS strings, but you can also use it to set JS variables:
function set_my_js_var() {
// logic here for setting the right JS var
return "some value";
}
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 you JS File by my_global.my_js_var
Related Posts:
- JavaScript implementation of Gzip
- Is there a JavaScript API? How to access public and private data in JS?
- How to HTML5 FormData Ajax
- How to properly use wp.ajax.post?
- Custom PHP endpoint for a plugin’s AJAX call
- Refused to execute script from ‘***’ because its MIME type (‘text/html’) is not executable, and strict MIME type checking is enabled
- WordPress Ajax Login without page reload
- Dynamically changing navigation links (next and previous) via AJAX
- Drag and drop multiple file upload using Ajax WordPress
- Quick Edit: Selected Custom Taxonomy Not Refreshing After Save
- Vue.js + AJAX Shortcode
- Turn jQuery.ajax() request into XMLHttpRequest (vanilla JavaScript)
- fetching via fetch/ajax gutenberg block data from third party
- ajax and nonce when JavaScript is in a seperate file
- admin-ajax.php HTTP400: BAD REQUEST – The request could not be processed by the server due to invalid syntax
- AJAX issue – Uncaught SyntaxError when processing Zip File
- WordPress AJAX Call Not Return Result
- Nonces, AJAX, script variables & security in WordPress
- Extending wp JavaScript base class to make a post request to a custom REST endpoint
- wp-admin AJAX with Fetch API is done without user
- Admin Ajax and HTML5 Formdata
- Call javascript function when category was added via ajax
- Populating content dynamically via AJAX and Advanced Custom Fields [closed]
- Help with AJAX front end comment moderation
- Woocommerce Ajax Add cart not working
- Ajax Modal Flickers When Opened Multiple Times
- Show Post Content with AJAX
- Load JavaScript from a post that’s loading into Fancybox via ajax
- splitting the URL using jQuery
- AJAX Load more on CPT returning random posts
- Refresh Markercluster after ajax call
- Making an ajax request from a different domain
- Performance optimization of tree like structure
- How do I query posts by a sub value with the API?
- Radio buttons live refresh in the customizer
- Gravity Forms closes my popup on Validation Error [closed]
- Why is wp_localize_script returning false?
- Using admin-ajax prevents regular php form submission
- wp_mail doesn’t work when logged in?
- Hide Load more Ajax button if there is no more users to load or less than the number?
- How to disable drag-and-drop upload function in Media Library?
- javascript ajax and nonce
- Making POST request with AJAX returns a 400 error (without jQuery)
- Is there a better way to access transients using javascript
- Run javascript upon successfully set featured image
- admin-ajax.php (aborted) error when using jQuery.get
- Replace link with form to pass variables to javascript / ajax
- Ajax page load without reload
- Load page HTML content through AJAX
- How to display contact form 7 form in vanilla js without jquery in frontend
- Get uploaded attachment width & height and attachment ID after upload them
- Get localize of a loaded javascript
- Updating failed. The response is not a valid JSON response. specific to my browser when I include javascript in my html
- Uncaught TypeError: Cannot read properties of undefined (reading ‘message’) [closed]
- Refresh checkout fields on add to cart with order bump
- Refresh Gutenberg with JS for it show updated post
- javascript onClick update user_meta from jquery.ajax
- how to make sure js is enabled before executing php function
- How to Object.freeze wp_localize_script
- How to pass value from ajax to php in no conflict mode?
- ERROR while passing data from JS to PHP via AJAX
- Ajax sometimes work and sometimes just don’t work
- React to AJAX adding to the page
- Download doccument on server rather than clients browser
- Using existing ajax data that is loaded into page
- Javascript output now showing in custom widget
- Ajax call from Plugin using Class
- Ajax call on class returns old data
- How to safely pass post_id and user_id via AJAX to the backend (prevent user from changing it via JS)?
- Ajax show custom post data form & script
- AJAX form not working, still reloads on submit
- Create a post with REST API and adding a category
- Get post details with pure javascript ajax
- Ajax WordPress pass post URLs
- Jquery wrap permalink in a data-attribute?
- Session variables lost during Ajax calls – WordPress – Sage Starter Theme
- ‘Dehighlighting’ navigation once clicked
- Bad Request when adding new post via ajax form
- AJAX admin Internal 500 error Failed to Upload
- Using Javascript Callback from plugin in a theme
- WordPress Ajax send response on every iteration of a loop
- Running js in html code with same content
- How to get current_user_id from wordpress in node js?
- AJAX call not initializing for non-admins in WordPress
- Escaping admin_url output being passed to js (esc_js vs esc_url)
- How to submit a button automatically after every scheduled hours?
- My javascript is returning this error: $.ajax is not a function
- jQuery Autocomplete not working with wp_localize_script
- Ajax returning correct data BUT at wrong place
- admin-ajax.php mixed content
- Admin WP List Table Columns Missing
- (updated) How to add AJAX error handling to a (fully) custom registration form?
- Image upload via FormData API and AJAX is not working ($_FILES always empty)
- Using infinite scroll ajax load more with search parameter
- AJAX action through direct link
- Ajax call on new site with jupiterx theme getting 400 response [closed]
- My function containing a mysql query launched by ajax is not working in wordpress. What am I missing?
- Having a self updating list
- Why does my Ajax Get request give a 400 bad request?
- How to update my jquery/PHP function to add/remove user as favorites in (WordPress) users list