Skip to content

Instantly share code, notes, and snippets.

@lukecav
Last active April 3, 2024 23:20
Show Gist options
  • Save lukecav/9e7775cbe3172ef32b5191f5b56d64fb to your computer and use it in GitHub Desktop.
Save lukecav/9e7775cbe3172ef32b5191f5b56d64fb to your computer and use it in GitHub Desktop.
Logout of WordPress without confirmation message
function getLogoutUrl($redirectUrl = ''){
if(!$redirectUrl) $redirectUrl = site_url();
$return = str_replace("&", '&', wp_logout_url($redirectUrl));
return $return;
}
/**
* Bypass logout confirmation on nonce verification failure
*/
function logout_without_confirmation($action, $result){
if(!$result && ($action == 'log-out')){
wp_safe_redirect(getLogoutUrl());
exit();
}
}
add_action( 'check_admin_referer', 'logout_without_confirmation', 1, 2);
@MarceloGlez
Copy link

Perfect for me! (youpage.com/wp-login.php?action=logout)

/**

  • Generates custom logout URL
    */
    function getLogoutUrl($redirectUrl = ''){
    if(!$redirectUrl) $redirectUrl = site_url();
    $return = str_replace("&", '&', wp_logout_url($redirectUrl));
    return $return;
    }

/**

  • Bypass logout confirmation on nonce verification failure
    */
    function logout_without_confirmation($action, $result){
    if(!$result && ($action == 'log-out')){
    wp_safe_redirect(getLogoutUrl());
    exit();
    }
    }
    add_action( 'check_admin_referer', 'logout_without_confirmation', 1, 2);

@bradthebluefish
Copy link

Below code worked fine for me

/**
 * Generates custom logout URL
 */
function getLogoutUrl($redirectUrl = ''){
    if(!$redirectUrl) $redirectUrl = site_url();
    $return = str_replace("&", '&', wp_logout_url($redirectUrl));
    return $return;
}

/**
 * Bypass logout confirmation on nonce verification failure
 */
function logout_without_confirmation($action, $result){
    if(!$result && ($action == 'log-out')){ 
        wp_safe_redirect(getLogoutUrl()); 
        exit(); 
    }
}
add_action( 'check_admin_referer', 'logout_without_confirmation', 1, 2);

Tried this yesterday. Worked for me. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment