Help with: Redirect User (Role) from specific URL to another.
-
Greetings!
I have searched high and low for plugins that would do such work and haven’t found anything “exact”.
I tried some code in the functions.php as well, but no luck. (Code below)What I’m looking to get accomplished:
I have a products page that can only be viewed if you are logged in.
That product page can be reached via the nav menu, or via a button in the user dash.I will be having multiple duplicated product pagevariations in hopes that I can redirect a customer to a specific variation of that page depending on what “Role” they are assigned to.
I have tried adding the following to my functions.php while replacing “ROLE_NAME” with the role, and TARGET_URL with the target URL but I don’t know how to add the from/to URL logic:
function role_redirect($user_login, $user) { if( in_array( 'ROLE_NAME',$user->roles ) ){ exit( wp_redirect('https://www.TARGET_URL.com/' ) ); } } add_action( 'wp_login', 'role_redirect', 10, 2);
I also tried something like this but have the same issue where I don’t know where to add the from/to logic:
function role_redirect() { $loggedin_user = wp_get_current_user(); if (!is_user_logged_in() || !in_array( 'ROLE_NAME', (array) $loggedin_user->roles )) { $location1 = 'https://www.TARGET_URL.com/'; header('Location: '.$location1.''); } } add_action('wp_head', 'role_redirect');
I am running:
WordPress 5.9.2
WooCommerce 6.2.2
PHP 7.4Any guidance would be truly appreciated!
- The topic ‘Help with: Redirect User (Role) from specific URL to another.’ is closed to new replies.