Updates, Tips & Tricks

Updates as Released, Plus My Personal Collection Of WordPress Best Practices

No Nonsense WordPress Logout Link Without a Plugin

07 March

Add this WordPress Logout Link snippet and any logged in user can just go to /logout/ and they will be logged out:

// WordPress Logout Link
function wpbase_logout_redirect() {
    $path = $_SERVER['REQUEST_URI'];
    if ( !is_user_logged_in() ) {
        return;
    }
    // Allow Logged in Users to Log out from /logout/
    if (preg_match("~/logout~", $path) or is_page('logout')) {
        wp_logout();
        wp_redirect( wp_login_url().'?loggedout=true' );
        exit;
    }
}
add_action('template_redirect', 'wpbase_logout_redirect');
add_action('login_init', 'wpbase_logout_redirect');
No comments yet.

Leave a Reply