If anyone still needs it:
wp_add_inline_script(
'map-scripts',
'const ajax_info = ' . json_encode(array(
'ajaxurl' => admin_url('admin-ajax.php'),
)),
'before'
);
but, it’s good practice to use nonce as well for better security:
wp_add_inline_script(
'map-scripts',
'const ajax_info = ' . json_encode(array(
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('your_nonce_handler'), //the more specific the better
'your_other_item' => "some additional data", //etc..
)),
'before'
);
https://developer.wordpress.org/reference/functions/wp_add_inline_script/
Related Posts:
- Is there a JavaScript API? How to access public and private data in JS?
- jQuery Autocomplete not working with wp_localize_script
- Can’t seem to get wp_localize_script to work
- How to localize inline script called with ajax
- Nonces, AJAX, script variables & security in WordPress
- Ajaxing in functions.php
- how to send Ajax request in wordpress backend
- Populate a Map at The Same Time as showing Posts via AJAX
- wp_localize_script not working on ajax response
- How to make tabulator ajax call in wordpress?
- Admin-Ajax Error
- Why is wp_localize_script returning false?
- Ajax call with javascript in post content (not enqueued)
- WordPress Ajax Filter
- Is there a better way to access transients using javascript
- How to load dynamic option with ajax
- Why the wp_localize_script() does not work correct in my case? ( /wp-admin/admin-ajax.php 400 (Bad Request) )
- Get localize of a loaded javascript
- How to Object.freeze wp_localize_script
- Ajax is not defined
- Ajax WordPress pass post URLs
- wp_localize_script not create variable in head section
- WordPress wp_localize_script nonce and ajax URL
- Access-Control-Allow-Origin error sending a jQuery Post to Google API’s
- WordPress AJAX with Axios
- Why might a plugin’s ‘do_shortcode’ not work in an AJAX request?
- Get Previous & Next posts by Post ID
- Nonces and Cache
- REST API endpoint for elasticpress autosuggest
- ajax – why multiple calls to wp_create_nonce() return same value?
- AJAX request on the frontend always returns 0 if user is not admin
- Using ajax on categories and wordpress loops
- Cannot load admin-ajax.php. No access-control allow origin*
- Nonces and Ajax request to REST API and verification
- wp_remote_get() to get AJAX url /wp-admin/admin-ajax.php
- WP_LOCALIZE_SCRIPT doesn’t work
- Ajax – gettext without a plugin
- WordPress AJAX calls not detecting language properly?
- AJAX issue – Uncaught SyntaxError when processing Zip File
- wp_verify_nonce doesn’t return true on server when it matches the nonce
- update_user_meta doesn’t work with AJAX
- WordPress ajax get content post id
- Using wp_handle_upload and media_handle_sideload with ajax
- Load ajax if is_home()
- How to know what page is calling admin-ajax.php?
- Get wp_title wp ajax
- Why Does WordPress not output admin-ajax.php path by default?
- Ajax custom search not functioning as expected
- Jetpack Infinite Scroll – Add more than 7 posts each time?
- SSO autologin WordPress + Ajax
- Speed/Performance difference between `wp_ajax` and `init` checks for AJAX/POST requests?
- Test WordPress api with postman
- Remove trailing 0 from function that is used both statically and dynamically
- How to process wordpress ajax call without action parameter?
- How to load next and previous posts by Ajax
- Update attachment metadata fails
- Nonce fails on ajax save
- WordPress P2 live problem
- All AJAX requests return a 400 error
- Unable to successfully verify nonce
- Create custom POST Method URL
- Getting a variable using $post ajax back from php to js response in WP
- Get response for check_ajax_referer
- WordPress ajax-action how to return content
- Custom Login with Ajax not working with IE
- AJAX jQuery post frontend returning failed to load resource status 400
- ajax form is returning the dreaded “[HTTP/1.1 400 Bad Request” and a zero
- How to check nonce lifetime value of plugins?
- How can I wp_send_json data?
- Making POST request with AJAX returns a 400 error (without jQuery)
- Front end theme options ajax returns 0
- PHP session when called wp_ajax_nopriv
- Custom RPC end-point security best pratice?
- AJAX – SHORTINIT set to TRUE returns blank
- How to make image in TinyMCE clickable
- How to use Jeditable plugin with admin-ajax.php?
- Uncaught TypeError: Cannot read properties of undefined (reading ‘message’) [closed]
- Disabling ajax code that does a POST request that ends in a 400 error code
- How to prevent my external API call from being called by anyone but me (my site)
- get a bad request 400 on my ajax-admin.php file
- Registration form AJAX check for existing username (simple version)
- Trigger action via button
- Syntax error on query_vars while handling with Jquery
- Want to send ajax request in wordpress to a custom file in plugin
- How to send the checkbox value to email
- how to get password from user instead wp-generate-password
- Ajax Response Error | just getting error as the response
- Sending email with wp_email and AJAX
- Passing variable data from external jQuery file to options.php
- How to use nonces for frontend AJAX voting if the page gets cached?
- AJAX Call in Plugin Returns More than JSON
- Ajax Form seems to post, but does not return
- Why can wordpress not find the actions I add in my constructor?
- delete attachment for one post without deleting actual attachment post
- Memberpress isn’t cooperating with WooCommerce
- where does my function output from load-* go?
- Facebook Pixel + WooCommerce + AJAX = Confusion
- Front-end ajax problem all users and guests getting a 302 redirect when accessing wp-admin/admin-ajax.php
- Use Ajax to get an Options Settings Value
- Run PHPMailer function after ajax function completes that adds row to custom table