Redirect after login depending on if it's a vendor or not

Hi,
I want to redirect to dashboard if a vendor is logged in and to home page otherwise, I tested debugging for a while with this code but it seems like not working

add_filter( 'hivepress/v1/forms/user_login',  'alter_user_login' , 10, 2 );

 function alter_user_login( $props, $form ) {
         $vendor = \Hivepress\Models\Vendor::query()->filter(
             [
                 'user' => get_current_user_id()
             ]
         )->get_first();

         if( $vendor ){
             $props['redirect'] = hivepress()->router->get_url( 'user_account_page' );
         }else{
            $props['redirect'] = get_site_url();
         }
         return $props;
}

The get_current_user_id() function seems not getting the id of the logged in user
please help

Yes, unfortunately it will not work this way, since this filter is used when the form is rendered and the current user ID is not available at this time. I recommend redirecting this form to a common URL (e.g. /?login_redirect=1). Then you can add another callback for template_redirect, check if $_GET['login_redirect'] is set and check if the user is a vendor there, and redirect them.

Thank you so much @ihor I will test it out

1 Like

I tested and it works like a charm, @ihor thank you very much again