How to add WordPress nonces to ajax request

I figured it out. Simply, in my request, under data, I added

"nonce" : "<?php echo wp_create_nonce( 'refresh_my_plugin' ); ?>"

then to verify

if (isset($_POST['refresh_my_plugin']))
    if ( wp_verify_nonce( $_POST['nonce'], 'refresh_my_plugin' ) )
        refresh_my_plugin();

With incorrect wp_verify_nonce, I instead get a 403, which is reflected on the button with the error handler.

Leave a Comment