Wednesday, December 28, 2022
HomeWordPress Developmentnonce - Logout button in menu with "fairly" URL

nonce – Logout button in menu with “fairly” URL


I want to add a menu merchandise that merely has the URL /log-out, which when clicked would log a consumer out with out asking them for affirmation. To this finish, I’ve tried to create a redirection to the precise logout URL with a correct WordPress nonce added to bypass the consumer affirmation web page, like so:

/**
 * Redirects the consumer to the actual logout web page upon clicking a hyperlink with a "fairly" logout URL, and bypasses affirmation by including a correct WordPress nonce
 */
add_action('template_redirect', operate() {
    if (empty($_SERVER['REQUEST_URI'])) return;
    
    $uri = $_SERVER['REQUEST_URI'];
    
    if (preg_match('#^/log-out/?$#', $uri, $matches) === 1) {
        wp_redirect(home_url(
            '/wp-login.php?motion=logout&redirect_to=%2F&_wpnonce=".wp_create_nonce()
        ), 302);
        exit;
    }
})

This does reach redirecting the consumer to the logout web page with nonce added. Nonetheless, the presence of the nonce doesn”t really cease the affirmation web page from showing! In reality, on the affirmation web page itself, the ultimate logout hyperlink has its personal separate nonce.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments