How to redirect to home page after login ?
In general, default WordPress system is built to show dashboard after successful login. But sometimes we need to redirect our users to any other page rather than go into dashboard.
Here i am giving few examples code which you need to place in functions.php file to work.
Example-1: This example redirects admins to the dashboard and other users to the homepage.
function my_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; } } add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
Example-2: Below example redirects to homepage for all types of users .
function redirect_home( $redirect_to, $request, $user ) { return home_url(); } add_filter( 'login_redirect', 'redirect_home', 10, 3 );
Example-3: This example also redirects all admins to the dashboard and other users to the homepage.
function my_login_redirect( $redirect_to, $request, $user ) { return ( is_array( $user->roles ) && in_array( 'administrator', $user->roles ) ) ? admin_url() : site_url(); } // end soi_login_redirect add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
If anyone doesn’t like to change functions file code they can use the plugin. Here i am attaching one plugin that can solve the same issue.
This plugin redirect users to specific locations after login, based in the role and is compatible with custom roles and new links in the sidebar of the admin panel and you can also set a custom URL to redirect the user.
This post has already been read 6474 times!
That’s works for me 🙂 Thanks Mehedi.
Good post. I will be experiencing a few of these issues as well..