Below is the code to add a Login/Logout toggle item in WordPress navigation menu.
Below is the code to redirect admins to WordPress Dashboard and other users to site’s homepage after a successful login.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Redirect admins to WordPress Dashboard and other users to site's homepage after a successful login | |
* https://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect | |
* | |
* @param string $redirect_to URL to redirect to | |
* @param string $request URL the user is coming from | |
* @param object $user Logged user's data | |
* @return string | |
*/ | |
add_filter( 'login_redirect', 'customprefix_login_redirect', 10, 3 ); | |
function customprefix_login_redirect( $redirect_to, $request, $user ) { | |
//is there a user to check? | |
global $user; | |
if ( isset( $user->roles ) && is_array( $user->roles ) ) { | |
//check for admins | |
if ( in_array( 'administrator', $user->roles ) ) { | |
// redirect them to the default place | |
return $redirect_to; | |
} else { | |
return home_url(); | |
} | |
} else { | |
return $redirect_to; | |
} | |
} |
Below is the code to redirect all users to site’s homepage after logout.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//* Redirect all users to site's homepage after logout | |
add_filter( 'logout_redirect', create_function( '$url,$query,$user', 'return home_url();' ), 10, 3 ); |
Another (much simpler?) way to add a Log in/Log out toggle item in your navigation menu and manage redirections per user after login/logout is to use both following plugins: Login Logout Menu and Peter’s Login Redirect.