skip to Main Content

WordPress Login and LogoutHave you ever wondered how to guide users to your login page instead of the default wp-login.php login page? Moreover, have you considered the possibility of redirecting users to the “WordPress Login and Logout” upon exiting?

The initial code snippet demonstrates the process of redirecting login errors to the same page, eliminating the need for any alterations. This approach offers a seamless user experience while ensuring smoother login redirection.


//if login fails, redirect to current page
add_action( 'wp_login_failed', 'my_front_end_login_fail' ); // hook failed login
function my_front_end_login_fail( $username ) {
$referrer = $_SERVER['HTTP_REFERER']; // where did the post submission come from?
// if there's a valid referrer, and it's not the default log-in screen
if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
wp_redirect( $referrer . '?login=failed' ); // let's append some information (login=failed) to the URL for the theme to use
exit;
}
}

This second snippet shows you how to redirect your user to your login page you just have to change “home()” if it is not your login page.

//Redirect after logout
add_action('wp_logout','ccw_redirect_logout_url');
function ccw_redirect_logout_url(){
wp_redirect(home_url());
exit;
}

You just have to place both codes in your theme’s function.php. You must take note however, that using this WordPress Login and Logout quick fix assumes that you have your own login page.

That’s all there is to it. Enjoy!

Back To Top