Role based redirection

A lot of online store owners and service providers asks if users can be redirected to a specific page after successfully logging in,  in order to increase sales of their respective online stores and services on WordPress. This feature makes sense since studies have shown that user is more likely to purchase products or services if relevant products and services are displayed which they are interested in.

Separate pages/posts can be created for the users with different roles and soon after logging in, they get redirected to products or services they might be looking for.

If you are already using Easy Registration Forms, the good news is that you are well equipped with user role-based redirection on WordPress and you don’t have to write custom code or install and manage a separate plugin.

Configuring  user redirection for all users to a custom page after login with Easy Registration Forms

Configuring a common redirection for all users

  1. Go to Global Settings->Login Options under ERForms menu.
  2. Here you can configure the URL in After Login Redirection

Configuring redirection by user role

  1. Go to Global Settings->Login Options under ERForms menu.
  2. Check the option Role Based Login Redirection, Here you can provide redirection URL for each user role.

User Role Based Redirection

Redirection can also be done by custom code. Below is a code snippet for redirection

/**
* Redirect user after successful login.
*
* @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
*/

function my_login_redirect( $redirect_to, $request, $user ) {
   //is there a user to check?
   if (isset($user->roles) && is_array($user->roles)) {
       //check for subscribers
       if (in_array('subscriber', $user->roles)) {
           // redirect them to another URL, in this case, the homepage 
           $redirect_to =  home_url();
       }
   }

   return $redirect_to;
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

add_filter( 'login_redirect', function( $url, $query, $user ) {
return home_url();
}, 10, 3 );

 

As you can see this requires coding knowledge to implement and also needs a timely update if roles or redirections are to be changed.

A drawback of custom code approach is that every time a role is added, the code is to be updated if custom redirection is to be set for the new role or any redirection is to be changed for a role.

User role-based redirection saves complexity of messing up with the code and maintaining it timely. All you need is to update the URL in respect of user roles and everything else will be handled by Easy Registration Forms.

2 Replies to “Role based redirection”

  1. Hello

    ive got custom roles setup. Ive found that logging in with a custom rile account…doesnt redirect ..it tried to load the profile page, which fails because ive blocked the backend view of the website.

    Can you advise if possible how i can en sure my custom role will redirect properly?

Leave a Reply

Your email address will not be published. Required fields are marked *