To set is the connected user belongs to the blog, you need to do that on hook wp_login
that means the user is connected :
const ROLE_ADMINISTRATOR = "administrator";
const ROLE_CONTRIBUTOR = "contributor";
add_action("wp_login", function ($user_login, \WP_User $user) {
if ( !is_multisite()
|| is_user_member_of_blog()
) {
return;
}
$usersAdministrator = get_users([
"role" => ROLE_ADMINISTRATOR,
]);
if (count($usersAdministrator) > 0) {
$newRole = ROLE_CONTRIBUTOR;
} else {
$newRole = ROLE_ADMINISTRATOR;
}
add_user_to_blog(
get_current_blog_id()
, $user->ID
, $newRole
);
}, 10, 2);
Related Posts:
- How to change user starting role in WordPress MultiSite?
- Easily adding multiple existing users to a multisite site
- Why are my roles not visible in a Multi-site/Network?
- Prevent user creating new users with specific roles
- Can you have multi-site WP and keep users separate?
- Can wp-cli display all users and their roles across all sites in a multisite instance?
- WPMU – new users are automatically subscribed to the main blog – how to prevent that?
- Update User Role Across Network when Main Site User is Updated
- Get the User ID Who Owns a Given Blog ID in Multisite
- Make a user administrator to a sub directory site and a contributor to main site in multisite network
- Subsite access without being a member of the subsite in wp multisite network
- Create Custom Multisite User Role to Reduce Capabilities
- Copy user role on multisite so the user can access subsites with same role
- WordPress Multisite Add User
- WordPress multisite, allow non super admins to create sites
- can’t create user without email for an author after converting single site to multisite
- A different role for each site in a multisite
- How to create child/sub user under parent user
- How create a multisite setup with “phantom” accounts and passwords?
- Is there any way to give all users access to one blog in a multisite network without using a plugin?
- How to give “author” user role appropriate capabilities to add PollDaddy polls? WordPress multisite
- Where are available Roles Defined in the wp_ database?
- Is there a is_user_logged_in() for multisite?
- WordPress Multisite allow site admin to add user without email confirmation
- Network not displaying all sites and users
- How to use alias domain for multisite installation?
- Issue with Custom Roles in Multisite
- Give to site admin the option to “skip confirmation email” when adding new user
- Delete sites in wordpress multisite (not suspend)
- How can I display all Multisite blogs where this user is administrator?
- How to synchronize user profile fields in multisite?
- Restrict users on multisite WordPress install
- Possible to make custom role in multisite that can add sites?
- Get first_name and last_name on user_register hook
- User registration on subsite in multisite configuration
- Allowing logged in users to comment without moderation across a multisite installation
- Multisite Independent User Base
- How To Remove Dashboard Menu Items for Multisite Users?
- How to get a user role of a specific blog in multisite?
- the blog owner multisite
- Deleting a user in multisite
- How can I get multisite primary blog (url or path) for current user?
- Can I use multisite functions in a single-site installation?
- How to add edit users capability to a custom role?
- How can I delete a user from entire multisite nework
- delete_user_meta : how to delete all the metadata of a given user (witout SQL)
- How to search users globally on a multisite install?
- Within the database, where is the flag which says that a user has Super Admin rights?
- Displaying a message upon user registration
- Query users by capability – uninstall/deactivate callback
- access the plugins for each role in WordPress multisite
- What is meant by Multisite in WordPress?
- Allow Author on Site A capability to upload files on Site B in Multi Site
- Add a user to a specific blog when they register?
- How do you disable account activation in WPMU and then log the user in right away?
- Add menu items/actions for multisite users who are registered on the network but do not have a role or capability in any sites
- Multisite User Sync Question about blog authorship
- Preferred method of setting user role only works on Main Site in Network
- Good way to block users within a multisite setup without deleting them?
- Multisite user roles – capabilities not working
- single sign on for wordpress multi site
- Obtaining user table for one site on multisite set up
- WordPress stuck in deleting user
- Restrict Capability of Administrator to Create, Edit and Delete Pages in Multisite
- Multi-Site Subfolder User Permission Issue
- WordPress Multisite – Load users using custom query
- Understanding State in WordPress Multisites
- How to change a user role after registering in multisite?
- How to sync roles across Multisite?
- Delete user from multisite when removed from subsite
- Combining user database tables while keeping all other data in seperate for multiple sites?
- How to fix that new users show up again in subsite of a Multisite?
- Multisite network admin – User’s role not showing up
- Sharing users across multiple sites, each on their own database
- User registration is currently not allowed
- Give users acces to admin a single post or set up WP network?
- How to clone homepage for multi region site?
- Language per user role, how can I achieve this?
- WordPress API – sign-up to multiple Multisites from one single form
- Redirect a user roll to a page after login in mutlisite network
- Possible to have duplicate usernames on different two multisites
- Multisite – each site with it’s own set of users
- Modify new user email notification in network admin screen wp multisite
- Users getting linked unwanted to main mu in WordPress multisite (WPMU)
- Multisite – site user limited only for this site
- Importing Posts into New Website with Same User ID’s
- WordPress automatic Login on other page?
- How to automate adduser (on linux) when add new user site in multisite
- Activation of new Registered site fails on multisite
- Is Multisite the RIGHT option for my case?
- Should I use MultiSite for a subdomain based wp site?
- Multisite “Skip Confirmation Email” Log Out Problem
- Multisite vs Role Scoper
- How can I display all Multisite blogs where this user is administrator?
- WordPress multi user registration sites
- Multisite and users being listed on network administration dashboard and not main site dashboard
- Best way to convert two single WordPress sites into Multisite network with shared user base
- Advice on WP site structure using multisite
- WordPress Multisite restict user access
- Change a subsite Admin role of a WordPress Multisite after 24 hours registering