This is happening because a separate nonce with the action wp_rest is not being sent by the server to the client and received back from the client in an HTTP request header called X-WP-Nonce with every REST request.
To get this working, you will have to generate a nonce like this:
wp_create_nonce('wp_rest')
…and provide it to the client making the rest call. Once your client has the nonce value, you need to add it to every REST request e.g.:
headers: {
'X-WP-Nonce': nonce,
}
Creating the nonce on the server and accessing it on the client can be done several ways. Using wp_localize_script()
is the most common and probably best practice for WordPress. wp_localize_script()
addds a global variable to the client for a script to access. See https://developer.wordpress.org/reference/functions/wp_localize_script/.
Related Posts:
- AJAX call not initializing for non-admins in WordPress
- How to get a unique nonce for each Ajax request?
- Nonces and Cache
- Stop admin-ajax?
- Is it safe to assume that a nonce may be validated more than once?
- Multiple ajax nonce requests
- AJAX request on the frontend always returns 0 if user is not admin
- Using Nonces for AJAX that only retrieves data
- How to verify nonce from Bulk/Quick Edit in save_post?
- How can I get logged in user’s session data from admin-ajax?
- How to add WordPress nonces to ajax request
- Nonces and Ajax request to REST API and verification
- Ajax function returns -1
- Serving nonces through AJAX is not refreshing nonce, returning 403 error
- wp_verify_nonce always returns false when logged in as admin
- ajax and nonce when JavaScript is in a seperate file
- wp_verify_nonce doesn’t return true on server when it matches the nonce
- How to allow to user non logged in WP system upload in media library?
- AJAX requests broken due to HTTPS for wp-admin
- Check if username exist with AJAX
- Nonces, AJAX, script variables & security in WordPress
- Why does WordPress Heartbeat login not refresh the nonces?
- wp-admin AJAX with Fetch API is done without user
- How do I check if AJAX nonces are implemented correctly?
- How to check an ajax nonce in PHP
- Can a wp_nonce created from domain 1 to be verified on domain 2?
- Force redirect not logged in user to (wp-login.php or wp-admin) for specific page
- how to send Ajax request in wordpress backend
- Identical wp_rest nonce returned from rest_api
- WP Admin AJAX Security – using POST to include a relative URL
- wp_create_nonce() in REST API makes user->ID zero
- ajax nonce verification failing
- Plugin: AJAX query external API to sync to tables
- SSO autologin WordPress + Ajax
- Nonce fails on ajax save
- Ajax Request for both logged and non logged users
- Unable to successfully verify nonce
- Cache plugins and ajax nonce verification
- Nonce doesn’t validate in nopriv call
- Why does check_ajax_referer give a 403 error on https websites?
- javascript ajax and nonce
- How to check nonce lifetime value of plugins?
- Using nonce when loading posts with AJAX
- admin-ajax.php returns 0 even when the post status code is 200 OK
- Should wordpress nonce be placed in html form or in javascript file
- Add Server Side validation in Ajax mail form
- Can I use application/json content type in WordPress
- wp_verify_nonce not working on the mobile device
- How do I mitigate replay attacks when talking about actions that shouldn’t happen twice?
- AJAX form not working, still reloads on submit
- Ajax Security regarding user priviliges and nonces
- How to use nonces for frontend AJAX voting if the page gets cached?
- admin ajax is not working for non logged in users
- WordPress wp_localize_script nonce and ajax URL
- How to stop a nonce from being cached in an inline script, or alternatives to regenerate it if expired?
- How do i set up ajax nonce
- nonce_user_logged_out to assign guests unique nonces breaks ajax calls
- Nonce verification problem when logging in after a logout
- PHP “php://input” vs $_POST
- Google Maps API throws “Uncaught ReferenceError: google is not defined” only when using AJAX
- Access-Control-Allow-Origin error sending a jQuery Post to Google API’s
- Access-Control-Allow-Origin error sending a jQuery Post to Google API’s
- How to solve the error “SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.” in IE
- Show spinner GIF during an $http request in AngularJS?
- Refresh HTML Page in Browser Automatically on Timer – Every 15 Min
- jQuery Form Validation before Ajax submit
- JavaScript implementation of Gzip
- jQuery: Performing synchronous AJAX requests
- ASP.NET MVC controller actions that return JSON or partial html
- jQuery’s .on() method combined with the submit event
- Ajax takes 10x as long as it should/could
- Admin Ajax is returning 0
- How to check if I am in admin-ajax.php?
- wp_verify_nonce vs check_admin_referer
- Best way to end WordPress ajax request and why?
- How to load wp_editor() through AJAX/jQuery
- Admin Page Redirect
- How does admin-ajax.php work?
- How to cache json with wp-super cache
- Load minimum WordPress environment
- Why use wp_send_json() over echo json_encode()?
- Why use admin-ajax.php and how does it work?
- Can I use the same nonce for multiple requests on the same page?
- Open a Thickbox with content trough AJAX
- Setting admin edit panels & metaboxes positions and visibility for ALL users and admins
- Initialize TinyMCE editor / visual editor after AJAX insert
- Showing User’s Post Counts by Custom Post Type in the Admin’s User List?
- Update post counts (published, draft, unattached) in admin interface
- Why not register shortcodes if is_admin dashboard?
- WordPress AJAX with Axios
- Why is die() used at the end of function that handles an Ajax request?
- Why might a plugin’s ‘do_shortcode’ not work in an AJAX request?
- Making my AJAX powered WordPress Crawlable
- Is there a JavaScript API? How to access public and private data in JS?
- Get Previous & Next posts by Post ID
- is_admin() returns true when using admin-ajax.php from front end script
- failed to load wp-admin/admin-ajax.php
- How to save dismissable notice state in WP 4.2?
- Using Backbone with the WordPress AJAX API
- Using Ajax with a Class file