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

Perdonad , podéis poner el fragmento completo resuelto?
Es que no soy experto y me gustarĂ­a poner en mi web ese cĂłdigo
Gracias


Excuse me, can you put the complete fragment resolved?
I’m not an expert and I would like to put that code on my website
Thank you

Can you please help me with the question I asked you?

Thanks a lot

Hi @APORTODAS
Sorry for the delay,
Here is my adapted solution :

// After signing in, send all to the custom redirection url
add_filter( 'hivepress/v1/forms/user_login',	alter_user_login', 200 );
function alter_user_login( $form ) {
	$form['button']['label'] = 'Login';
	$form['fields']['username_or_email']['label'] = 'Email';
    $form['redirect'] = get_site_url() . '?login_redirect=1';
	return $form;
}

// Decide whether to redirect to the dashboard or not depending on is it's a vendor or not
add_action( 'init',  'redirection_after_login' );
function redirection_after_login(){
	if( isset($_GET['login_redirect']) && $_GET['login_redirect'] == 1 ){
		$vendor = \Hivepress\Models\Vendor::query()->filter(
			[
				'user' => get_current_user_id()
			]
		)->get_first();

		if( $vendor ){
			wp_safe_redirect( hivepress()->router->get_url( 'user_account_page' ) );
			exit;
		}
	}
}

I hope that helps

2 Likes

Thank you very much Jamel for being so kind.

And thanks to the help provided by the hivepress team.

I’m just asking you one last favor. I don’t know about programming and I have a hard time changing things.

Your fragment is great but I need you to redirect the user who registers for the first time and is not yet a seller to the screen of becoming a seller (just like when you hit the publish a listing button)

But I don’t know how to do it.

I think it’s adding some line or putting an “Else” but I don’t know how to do it.

Thank you, if you can help me jamel, ihor, andrii, yevhen.

With pleasure @APORTODAS
I’m a developer and I’m also new to Hivepress, your need looks specific and I’m afraid that I couldn’t help right away but maybe in a couple of days or more since I’m super busy with a project, that’s why I suggest you to hire some one from Fiverr for example to do things much faster
Regards

I also thank you very much.

I’ll try to look it up on the internet.

Thanks a lot

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.