Hello,
Apologies if I’m not in the rightful place.I need help.
I’m running a WordPress multisite.
I would like to have one of my child sites to be completely private. Access to this child site should only be by ROLE. ONLY a specified ROLE, ‘administrator,’ ‘contributor,’ ‘editor,’ ‘student,’ and all should be able to access any part of this Child Site at any given time.I know very little about code, but I tried something like “if ( is_user_logged_in() && ! current_user_can( ‘administrator’ . ‘staff’ . ‘editot’ ) ),” but I ended up breaking my site, so I reverted it all back.
Your help is highly appreciated.Hi there,
Thank you for posting.before I post the code, i want to clarify one thing, except the “Network administrator(SuperAdmin)” all other roles are specific to the site. So a user who is Editor on site 1 may not have the same role on site 2.
For the code part.
/** * Redirect users to main site home page if they are logged in and don't have the required role on the sub site */ function buddydev_check_redirect_roles_on_subsite() { if ( is_main_site() || ! is_user_logged_in() ) { return ;//no need to do anything on the main site or if the user is not logged in? } //this is incorrect way to do things, instead of role, please use the $roles = wp_get_current_user()->roles; $allowed_roles = array( 'administrator', 'editor' ); $found = array_intersect( $allowed_roles, $roles ); if ( empty( $found ) ) { wp_redirect( network_home_url('/') ); exit( 0 ); } } add_action( 'template_redirect', 'buddydev_check_redirect_roles_on_subsite', 0 );
You can put it in your functions.php or if using BuddyPress, you can put it in your bp-custom.php
Hope that helps.
Regards
BrajeshOh wow!
You’re awesome. Thank you. Thank you so much.
Your code has worked just like that..
thank you so much..On my multisite, I have a ‘Forums,’ ‘Online Store,’ and a ‘School.’
The idea is to have all users to access the ‘Forums’ with one email & pass, without issues.
but, the ‘school’ should not be accessed by ‘Online Store – Customer’ users, and vice versa. hence my request for the awesome code you just provided.I love the ‘redirect’ part of the above code. Now, just thinking outside the box, is it possible to ‘redirect’ users based on there role?….for example, when a ‘customer’ visits an online school, is it possible to redirect them back to the shop?…and when a ‘student’ visits a ‘wrong child site,’ is it possible to redirect them back to there ‘school site?’
I’m just tryna think outside the box. If it’s not possible, I totally understand.
Really Appreciate your time.
Thank you.
You must be logged in to reply to this topic.