Key points
- A nonce proves where a request came from, not what the user is allowed to do.
- Every action needs a current_user_can() check against the exact object being changed.
- wp_ajax_nopriv_ hooks expose an action to logged-out visitors: only use them on purpose.
During an audit of an in-house plugin, we found a “Mark as handled” button on a list of quote requests. The handler checked a nonce, so the developer assumed it was protected. In practice, any logged-in subscriber could grab a valid nonce and close every request on the site. It is the most common mistake we see in admin-ajax handlers: treating “this request came from my form” as if it meant “this user is allowed to do this”.
Three checks, every time
Any AJAX action that writes data has to answer three questions, in order: did the request come from our interface (nonce), is the input valid, and can this user act on this specific object (capability)?
add_action( 'wp_ajax_wts_mark_quote_done', 'wts_mark_quote_done' );
function wts_mark_quote_done() {
// 1. Did the request come from our screen?
check_ajax_referer( 'wts_mark_quote_done', 'nonce' );
// 2. Validate input
$quote_id = absint( $_POST['quote_id'] ?? 0 );
if ( ! $quote_id || 'wts_quote' !== get_post_type( $quote_id ) ) {
wp_send_json_error( array( 'message' => 'Request not found' ), 400 );
}
// 3. Can this user edit THIS request?
if ( ! current_user_can( 'edit_post', $quote_id ) ) {
wp_send_json_error( array( 'message' => 'Access denied' ), 403 );
}
update_post_meta( $quote_id, 'wts_status', 'done' );
wp_send_json_success( array( 'status' => 'done' ) );
}
We use the meta capability current_user_can( 'edit_post', $quote_id ) rather than a blanket edit_posts. It checks rights on that particular post, so custom roles and per-author restrictions keep working.
Passing the nonce to your script
Create the nonce in PHP and hand it to the script with wp_add_inline_script():
wp_enqueue_script( 'wts-quotes', plugins_url( 'quotes.js', __FILE__ ), array(), '1.0', true );
wp_add_inline_script( 'wts-quotes', 'const wtsQuotes = ' . wp_json_encode( array(
'url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'wts_mark_quote_done' ),
) ) . ';', 'before' );
In the browser, a plain fetch() with FormData is all you need:
const body = new FormData();
body.append('action', 'wts_mark_quote_done');
body.append('nonce', wtsQuotes.nonce);
body.append('quote_id', id);
const res = await fetch(wtsQuotes.url, { method: 'POST', body, credentials: 'same-origin' });
const json = await res.json();
Traps we keep running into
- is_admin() is not a security check. It returns true for any request to admin-ajax.php, anonymous ones included.
- wp_ajax_nopriv_ added “just in case”. It makes the action reachable without logging in. Keep it for genuinely public actions (contact form, catalogue filter) that never change anything sensitive.
- A nonce is not a long-term secret. It stays valid for up to 24 hours and is tied to the user and the action. It stops cross-site request forgery, and that is all it does.
- Over-sharing in responses. Do not send back a whole post object or its meta when the front end only needs a status.
admin-ajax or the REST API?
For new work we usually lean towards a REST route with an explicit permission_callback: the permission check is part of the route definition, so nobody can forget it inside the handler. That said, admin-ajax is perfectly fine for a small admin button, as long as every action runs these three checks.
FAQ
Is check_ajax_referer() enough if only administrators use the plugin?
No. The site may have other accounts tomorrow: clients, editors, shop customers. The capability check is a single line, and it means your security no longer depends on who happens to have an account today.
What happens when the nonce has expired?
check_ajax_referer() stops the request and returns “-1” with a 403 status. In your JavaScript, show a message asking the user to reload the page instead of a vague error.
Running in-house plugins written a few years ago? We carry out WordPress security audits and fix this kind of hole without breaking your features.



