Redirect after Portal Login?
Hey folks, I've had a look through the docs and here, and can't see any info on this - but I'd like to have the site redirect to a specific URL 'after' the user logs in - any ideas?
I've tried using standard WP filter but that doesn't work
Cheers!
Hi, have you checked out the FluentAuth plugin? It has customizable redirects there after login.
Thanks Artur AbakarovΒ , gave it a quick try but no dice as yet - have you had this working?
Jakson A I have unfortunately postponed introducing this feature, but was planning to do it with the FluentAuth plugin.
Check out the documentation https://fluentauth.com/docs/login-redirects/
Hey Jakson A,
Check out these two nifty functions I use with FluentSnippets.
The first one handles logins through the community form, while the second one deals with WordPress logins.
I'm currently running a challenge for our paying customers in the 'powerpoint-wow' space. Every new member who logs in gets redirected straight to this space.
Cheers!
<?php
// If users are members of the 'powerpoint-wow' space (slug), redirect them there after login
add_filter('fluent_community/auth/after_login_redirect_url', function($redirectUrl, $user) {
// Default redirection URL
$customRedirectUrl = $redirectUrl;
// Get the spaces the user is a member of
$spaces = \FluentCommunity\App\Services\Helper::getUserSpaces($user->Id); // this will return Collection of Space objects
// Find the 'powerpoint-wow' space
$space = $spaces->first(function ($space) {
return $space->slug == 'powerpoint-wow';
});
// If the user is a member of the 'powerpoint-wow' space, set the custom redirection URL to that space's home page
if ($space) {
$url = '/space/' . $space->slug . '/home';
$customRedirectUrl = \FluentCommunity\App\Services\Helper::baseUrl($url);
/*
// Log the space data for debugging purposes (commented out)
$logData = json_encode($space, JSON_PRETTY_PRINT);
file_put_contents('/PATH_TO_LOGFILE/debug.log', $logData);
*/
}
// Return the redirection URL
return $customRedirectUrl;
}, 10, 2);
/*
* Redirects members to the members area after login
*/
function pcl_login_redirect( $redirect_to, $request, $user ) {
// Check if the user has roles and is not an administrator
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if ( !in_array( 'administrator', $user->roles ) ) {
// Get the spaces the user is a member of
$spaces = \FluentCommunity\App\Services\Helper::getUserSpaces($user->ID); // this will return Collection of Space objects
// Find the 'powerpoint-wow' space
$space = $spaces->first(function ($space) {
return $space->slug == 'powerpoint-wow';
});
// If the user is a member of the 'powerpoint-wow' space, set the redirection URL to that space's home page
if ($space) {
$url = '/space/' . $space->slug . '/home';
$redirect_to = \FluentCommunity\App\Services\Helper::baseUrl($url);
/*
// Log the space data for debugging purposes (commented out)
$logData = json_encode($space, JSON_PRETTY_PRINT);
file_put_contents('/PATH_TO_LOGFILE/debug.log', $logData);
*/
}
}
}
return $redirect_to;
}
// Add filter to redirect users after login based on their roles and membership in 'powerpoint-wow' space
add_filter( 'login_redirect', 'pcl_login_redirect', 10, 3 );
?>
Hey Peter Claus LamprechtΒ - this is exactly what I'm after! - thank's a zillion !! π
Jakson A,Β I'm chuffed I could help!
It took me a while to gather everything, but M365 Copilot helped me piece it all together.
Next step: I'll be looking into using an .env file (or something similar) to manage which space should be the redirection target.
Peter Claus LamprechtΒ
Interesting when you want information to be noticed.
With your permission, Iβll copy your code.
D@vid, yes, please copy the code. No warranty β¦ (it works like a charm for me).