What you need to do is register and/or enqueue your script and then use wp_localize_script
to include a Javascript variable on your page. The Codex page has this example:
wp_enqueue_script( 'some_handle' );
$translation_array = array(
'some_string' =>
__( 'Some string to translate' ),
'a_value' => '10'
);
wp_localize_script( 'some_handle', 'object_name', $translation_array );
You can then access the variable with, to quote the same Codex page:
<script>
alert(object_name.some_string); // alerts 'Some string to translate'
</script>
Related Posts:
- How to load wp_editor via AJAX
- AJAX Call Via Vanilla JavaScript In WordPress Plugin Development
- Custom plugin giving: wp-admin/admin-ajax.php 400 (Bad Request)
- Create Proper WordPress Ajax Request in JavaScript, NOT jQuery
- How to save Clicks of a download link button while it doesn’t matter if we refresh the page or again login/logout
- ‘Lazy loading’ list of posts with pagination
- Ajax call doesn’t work in frontend but it’s working in backend (when I’m logged in)
- How to use wp-ajax in wp-cron
- How to create popup same as wordpress popup feature
- Shortcode button dosent work for all posts. Work for first post only
- WordPress + JavaScipt + AJAX + MySQL: insert query for form
- How to load wp_editor via AJAX
- wordpress add_action() issue in ajax call
- How to get specific string/value from an array? php [closed]
- coding a WordPress AJAX Form using PHP to check if User is Logged Out and Show error
- Ajax call not working in wordpress through a plugin
- Send Array from JS to php file then insert it in wp database through a plugin
- Jquery POST Ajax Return null data in wordpress
- Trouble with AJAX using GET
- Calling PHP function with AJAX
- Ajax Call not Working in Plugin
- JS working when used normally but not in wordpress
- Use AJAX to fetch Current Post Thumbnail for WordPress when Uploaded throughMedia Uploader Frontend
- AJAX update fails for public/non-admin users
- How to disable/enable PHP plugin functionality based on a TinyMCE toggle-button
- WordPress ajax error 400 bad request for sending data to remote site [duplicate]
- Conditional Fields depending on checkboxes
- Custom plugin giving: wp-admin/admin-ajax.php 400 (Bad Request)
- How to limit each front-end user to view just his own uploaded files on Amazon S3?
- Javascript not working
- How do I convince this button to do something when it is clicked?
- Ajax, jQuery and WordPress
- Load css classes after using ajax calls
- How to make a rest style plugin?
- How do I call an action hook into wp_ajax hook callback function
- Select posts from list and add them in a new list
- A better way to override plugin’s JS files?
- Help making my pagination plugin better
- admin-ajax returning 400 error when request is made with Fetch API
- How can I reduce the amount of files loaded/included per plugin?
- Errors while using ajax from external wordpress page
- Built in admin ajax hooks?
- delete_user_meta Delete one value out of array
- Calling shortcode in wp_localize_script strips result
- Insert code after tag without using functions.php?
- Adding with javascript to admin bar. Works in Chrome/Safari, not Firefox
- how to use ajax to display from database and load more in wordpress plugin
- Passing select control value to block
- Reuse javascripts from plugins within the theme
- $wpdb working with ajax but showing ajax error instead of success
- WP-Cufon adding extra space in my paragraphs in Firefox and Chrome
- Why do plugins that cause ‘unexpected output’ create AJAX problems?
- AJAX button run function
- Best approach to fetch data from wp options to js file or php file
- wordpress site | All-in-one-cufon IE Issue
- How to add Internationalization in WordPress using Javascript/React?
- Using a custom plugin to capture input data via Ajax and PHP
- How to get the element ID from new menu list that added with add_filter()?
- Ajax contact form widget plugin data not insert in database
- Modifying a .js Function for Print [QZ Tray, WooCommerce POS]
- AJAX in plugin wp_send_json() sending html
- Is there a standard way to listen for an on change event of a posts featured image
- How to insert HTML/JavaScript form into WordPress page? [closed]
- Create tabs in admin options page from custom post type loop
- GeoMashup plugin: how to customize cluster markers / icons? [closed]
- How can I remove the WordPress-Version (?ver=5.x) from my plugin
- plugin shortcode not working on ajax request call
- Localizing methods for jQuery append prepend wrap parent
- Ajax Plugin Not Echoing Response
- Which plugin can do horizontal scrolling of RSS feeds in WordPress [closed]
- WordPress Ajax code is not Working
- Where Should i write the code for wordpress ajax voting?
- How to include my Ajax calls in one function instead of calling different ones every time?
- how to request a script more than once in wordpress
- How to turn off automatic operation for Nivo Slider?
- Using AJAX to run SQL statement and populate dropdown
- Wp-minify doesn’t seem to minify my JS code
- Need help creating asynchronous data scraper in WordPress
- Ajax 400 error when used inside a plugin
- Strange admin-ajax / CSS / $_SESSION issue
- Performing CRUD operations on front end in wordpress
- In a plugin, How to update a json file using ajax
- How to add specific script to WordPress webpage that will working with user input and databases
- Why can I not deregister my style and script on custom post type?
- WordPress Ajax request “Failed to load resource: the server responded with a status of 400 ()”
- Ajax call fails and returns [object Object]
- Adding javascript to header of MetroMagazine theme
- wordpress ajax pagination object value does not change
- What’s the preferred method of writing AJAX-enabled plugins?
- Adding an onchange event to a custom dropdown menu in WordPress
- using jquery serialize in ajax operation for plugin
- Like/Dislike Bar not working when updating
- Possible jquery / library conflict in plugin usage
- Trouble Removing Plugin [closed]
- How to change response of admin-ajax request?
- Ajax not working for logged in users
- making a simple plugin
- Javascript as Jquery Function Call?
- Ajax button “Load more” is not loading correct language version posts for logged out users
- wordpress admin-ajax bad request 400