Categories: Wordpress Support

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.

DOWNLOAD

This post has already been read 6479 times!

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

More Posts

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

Share
Published by
Mehedi Hasan

Recent Posts

WP Share A Friend – A Way to Capture Lead

What is Lead Capture: In general sense lead capture designates a process or way of…

5 years ago

Unlimited Modal POPUPS on MouseClick

Introduction Unlimited Modal POPUPS on MouseClick: Everyone knows about popup during site visits and most…

5 years ago

WordPress Pregnancy Calculator Plugin

Brief about Pregnancy Calculator : This WordPress Pregnancy Calculator plugin allows users to display an…

5 years ago

Interactive Service Plugin with Hover Effects VC

Introduction About this Service Plugin: This Visual Composer Service Plugin works as an addon to…

6 years ago

WP Dynamic Query String

Introduction of Dynamic Query String : This plugin allows user to set dynamic query string…

6 years ago

How to make scroll PDF in iFrame for iPAD Safari

There is an occasional bug for iPhone and iPad even they are designed as well and…

7 years ago