I have a multi site installation using sub directories and would like to redirect Users to their primary site when they log in.
I know this can't be achieved using Toolset so I've researched this topic and found a code snippet I can add to my functions file which does achieve it.
Unfortunately, when a User is redirected to their primary site, they see content as though they are a Guest and not as the custom role I've set in Access.
Is there a reason why this would happen?
This is the code I added to my functions file:-
add_filter('login_redirect', function ( $redirect_to, $request_redirect_to, $user ) {
if ($user->ID != 0) {
$user_info = get_userdata($user->ID);
if ($user_info->primary_blog) {
$primary_url = get_blogaddress_by_id($user_info->primary_blog) . '/test/';
if ($primary_url) {
wp_redirect($primary_url);
die();
}
}
}
return $redirect_to;
}, 100, 3);
and this is the content of my test page:-
[toolset_access role="Guest" operator="allow"]I'm a Guest[/toolset_access][toolset_access role="Contributor" operator="allow"]I'm a Contributor[/toolset_access][toolset_access role="Custom" operator="allow"]My role is Custom[/toolset_access]
A User with role of 'Custom' sees I'm a Guest when they've logged in.
Are you able to shed any light on this please?
This should not be related to any redirect.
All you have is a page or post, and in there, you say:
[toolset_access role="Guest" operator="allow"]I'm a Guest[/toolset_access][toolset_access role="Contributor" operator="allow"]I'm a Contributor[/toolset_access][toolset_access role="Custom" operator="allow"]My role is Custom[/toolset_access]
So, whenever a Gues or whatever role logs in, no matter where he is redirected to, if on that very site the user has a specific Role, that will be respected.
I tested this and it works.
Of course, it will work on each subsite only, this means, an user of another subsite will eventually have another role in antoher site or not even be existing, right?
Hi Beda
In my test, the User role is respected without the code I added to my functions file but when I add it, the role is no longer respected.
Did you add the redirect code to your functions file? If so, why would it work for you but not me?
I cannot debug Custom Code.
That code is a simple redirect, it does not reset or manipulate the Role.
As such, I suspected a BUG in Access, but there is no issue on a WordPress install using no Custom Code or similar approaches.
And I also tested with a Redirect Custom Code.
This works properly on my test site.
The filter has to be used like this:
https://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect
$redirect_to should be populated with the Redirect URL.
You do not need to use wp_redirect as far I know.
Your goal can be achieved with this:
$redirect_to = 'url-to-redirect-to';
return $redirect_to
(Of course you can use other code to get the proper data and conditinals).
Hi again
After playing around with various redirect custom codes, I'm no further forward so I've removed the custom code to redirect to primary site. I've discovered though that even without any custom redirect code, there seems to be an issue with viewing content.
I have this scenario:-
Main site and 2 sites in sub directories. User has registered on site 3 via toolset register form which creates the user with Contributor role. The user hasn't been added to any other site. If that user logs in to site 3, they see Contributor content on that site but Guest content on site 1 and 2. If they log in via site 1 or 2, they see Guest content on ALL sites, even their own.
Wordpress gives Users Subscriber access on non primary sites so it looks like Access changes this to Guest but it's odd that they only see the correct content on their own site if they logged in on that site.
I do have another more pressing issue via another ticket at the moment and I want to check a few other things to make sure I don't have something conflicting somewhere so I'm going to park this temporarily but perhaps you could confirm whether I've misunderstood something somewhere please?
I will replicate a full stack multisite and test Access ShortCodes according to your above description.
I will then get back at you with the findings.
This is surely not related to the Custom Code, but it might be that with the particular approach you describe, I can see the BUG.
Let me stress test this again.
Thanks Beda, much appreciated.
I'll also do some more testing myself on a 'skeleton' install but I need Noman to look at my other ticket first as this relates to my test installation.
Hi Beda
I've had the time now to double check this. I'm pleased to say that if the User who registered on site 3 logs in via one of the other sites and then visits site 3, they DO see the right content for that/their own site.
However, despite having toolset access conditions for Guest, Subscriber & Contributor, they see Guest content on the other sites. From Toolset point of view, it sort of makes sense but if a User is logged in then they shouldn't see Guest content (which presumably is the logic behind the WordPress set up of automatically showing Subscriber content on 'other' sites).
Do you agree and have you been able to test this for yourself?
However, despite having toolset access conditions for Guest, Subscriber & Contributor, they see Guest content on the other sites.
That is expected, isn't it?
They are not member of those sites. As such, they are Guests.
So using the toolset access shortcode DOES overwrite the WordPress default Subscriber role on non primary sites?
I've realised why I'm struggling with this.
Wordpress automatically assigns Subscriber role on non primary sites but Toolset shows Guest content (because I'm using the access shortcodes). But a Subscriber is a 'logged-in' role so the two concepts don't fit together. Will this not lead to issues somewhere given certain conditions?
Can you see where I'm coming from here?
I am confused.
When I add a new user to the main site of my Install, all I can do is add the user as a Subscriber to the subsites, or any other role, but they are not automatically Subscribers there.
Is it possible to get access to your Sites?
I also have a Duplicator PRO copy, so I could even grab a copy of the entire site and deploy locally to make tests, if you allow me this.
I enabled a private reply.
Hi Beda
Christian is currently investigating something for me & has escalated the matter to 2nd tier support. They may need to access my test site. I think it would be advisable to wait until they've finished.
Would you like to re-enable the private box in readiness and I'll add the details you need as soon as my other ticket is closed?
In the meantime, the WordPress Codex (https://codex.wordpress.org/Multisite_Network_Administration) states:-
By design, all users who are added to your network will have subscriber access to all sites on your network.
I take this to mean:-
if a User registers on Site B and is given say Contributor role, they have Contributor role on Site B and AUTOMATICALLY have Subscriber role on all other sites on the network and will see Subscriber content on all other sites without having to be specifically added to any other sites.
Have I misunderstood the meaning of the quoted statement in the Codex?
Sometimes it's better I would read the Codex instead of relying on my tests, 🙁
I re-enabled the Private answer so it is ready for you once you are ready.
Thank you and I apologise for the hiccup in my previous statement.
That's correct, it says Guest.
But that User is not added to the Subsites, or I cannot see it.
If I go as Admin to the main site's wp-admin/users.php, I see 3 users, and Tempaccess is not there.
Also I cannot see any other site in the Dashboard, probably because I am not super admin.
Is it somehow possible to gain access to all the sites?
This would be required anyway to grab a Duplicator Snapshot, which I will need to debug this further, if possible.
I also discussed this with the Access Developer, and he calculates with the possibility of a BUG here, but we need to confirm it first.
I also already asked for a MultiSite Test Server, so to do that.
In case we are faster with your access, I could just grab a copy from there.
Thank you