Skip Navigation

[Resolved] Role access redirect

This thread is resolved. Here is a description of the problem and solution.

Problem:

I used the custom PHP code below to redirect on the user logging in, But it is not being picked up as access control is activating when going to a specific page:

https://toolset.com/forums/topic/role-access-redirect/#post-1304451

Solution:

I have tried your custom codes in my localhost, it works fine, it does redirect to different URLs on different user role.

But there is one issue should be notice, you can not redirect users to external website URLs by default, see similar thread here:

https://wordpress.stackexchange.com/questions/226794/custom-login-form-redirect-to-external-site

Relevant Documentation:

This support ticket is created 4 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by Mario 4 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#1304451

Hi Christian
I decided to try using the role

I used the code below to redirect on the user logging in.

But it is not being picked up as access control is activating when going to a specific page. Is there away to by pass the redirect.

function my_login_redirect( $redirect_to, $request, $user ) {
  
    if( !isset( $user->user_login ) ){ // we only want this to run when credentials have been supplied
  
        return $redirect_to;
  
    }
  
    //is there a user to check?
  
    if (isset($user->roles) && is_array($user->roles)) {
  
        //check for sellers
  
        if (in_array('seller', $user->roles)) {
  
            // redirect to seller my-account page
  
            $redirect_to =  '<em><u>hidden link</u></em>';
  
      }
  
    //check for buyers
  
    if (in_array('buyer', $user->roles)) {
  
  
  
        // redirect to my-buyer-account page
  
        $redirect_to = '<em><u>hidden link</u></em>';
  
      }
  }
  
return $redirect_to;
}
 
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );


#1304545

Hello,

Please elaborate the question with more details:
But it is not being picked up as access control is activating when going to a specific page.

How do you want to bypass the redirect?

I suggest you follow WordPress document to setup your custom PHP codes:
https://developer.wordpress.org/reference/hooks/login_redirect/

#1305487

Hi Luio,

I have two user types, which are buyer and seller.

When they login I want to redirect them to the correct page using the user role.

the sign page is lunched using Access control

The redirect is then handled by the function.

#1305653

I changed the roles to lower case. (I had it as uppercase Seller).
Now when a user logs in there are redirected to the wordpress dashboard. Not the role specific page.

#1306235

I have tried your custom codes in my localhost, it works fine, it does redirect to different URLs on different user role.

But there is one issue should be notice, you can not redirect users to external website URLs by default, see similar thread here:
https://wordpress.stackexchange.com/questions/226794/custom-login-form-redirect-to-external-site

#1306617

Thank you for your help.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.