CP2.0 wp_verify_nonce, wp_send_json_success it doesn't work properly

Expected behavior

When a nonce is verified with wp_verify_nonce, it should return true when the nonce is correct.

Current behavior

Apparently in CP2.0 it doesn’t work, it always returns false.

    if (!wp_verify_nonce($nonce, 'ajax_pagination2')) {
        wp_die('Nonce invalid', 403);
    }

I even tried manually, it doesn’t work either.

if (!wp_verify_nonce('277035857a', 'ajax_pagination2')) {
         wp_die('Nonce invalid', 403);
}

Besides, wp_send_json_success doesn’t work either.

wp_send_json_success($rendered_content);

It doesn’t send anything to the browser.

Possible solution

Steps to reproduce

$nonce = wp_create_nonce( 'nonce_test' );
if (!wp_verify_nonce($nonce, 'nonce_test'));
    echo '<h1>Nonce fail</h1>';

Context

Simply implementing a pagination through ajax, or infinite scroll using standard solution like

add_action('wp_ajax_nopriv_ajax_pagination2', 'ajax_pagination2');
add_action('wp_ajax_ajax_pagination2', 'ajax_pagination2');

Tested using this code in a utility plugin and works.

echo wp_verify_nonce( wp_create_nonce( 'nonce_test' ), 'nonce_test' ) ? 'Good nonce. ' : 'Bad nonce. ';
wp_send_json_success();

Output:
Good nonce. {"success":true}

wp_verify_nonce() is pluggable, so it’s not available in some contexts.

I will check again. With my context, wp_verify_nonce worked in the template used on the last version of wordpress and without any change it did not work on CP2.0… I just moved the template to a clean installation of classicpress and the theme no longer works correctly.

In the meantime, I wrote another function for the same purpose, something more adapted to my needs for using a key one time only! I also changed the method: I used api REST endpoint via register_rest_route, it seems more intuitive to me!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.