E
E
eisenheim942016-02-11 11:25:31
WordPress
eisenheim94, 2016-02-11 11:25:31

Why doesn't wp_verify_nonce() work from within a function?

I'm creating a link with wp_nonce in the GET parameter:

$link = esc_url( wp_nonce_url( add_query_arg( 'my_arg', 1 ) , 'product_subscription' ) );
echo '<a href="'. $link .'">'. $link .'</a>';

And if I paste it just in functions.php - it works
if(isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'product_subscription' ) ) {
    echo "Nonce verified";
}

But if I insert this check into a function
function check_subscription() { 
    echo "check_subscription";           
    if(isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'product_subscription' ) ) {
        echo "Nonce verified";
    }                       
}

And then I call this function from any page, it doesn't pass verification (isset passes, wp_verify_none doesn't). The function is called 100% because the text "check subscription" is displayed, but "Nonce verified" is not.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Chesnokov, 2016-02-11
@cesnokov

Try to see the results:

echo $_GET['_wpnonce'];
echo wp_verify_nonce( $_GET['_wpnonce'], 'product_subscription' );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question