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
- How To Add Custom Form Fields To The User Profile Page?
- Where are available Roles Defined in the wp_ database?
- Site admin in a network install can’t edit users?
- How to use same email for multiple users
- Where can I find documentation on what characters are allowed in user names and why?
- How do I remove/delete an old super admin user?
- Is there a is_user_logged_in() for multisite?
- WordPress Multisite allow site admin to add user without email confirmation
- wordpress multisite, how to keep user on subdomain throughout registration process?
- Troubleshooting a “You do not have sufficient permissions to access this page” error
- Network not displaying all sites and users
- WordPress Multisite – When a user signs-up on main site, how to add the user to a subsite
- 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
- How to enable a site administrator to edit users in a WordPress network/ multisite setup?
- Is there any central control/access panel for several WordPress sites?
- Different back-end colour scheme for the different sites of a multisite
- Set up collaborative site
- 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?
- Add role across network in multisite
- Restrict users on multisite WordPress install
- Possible to make custom role in multisite that can add sites?
- How does WordPress connect sites and the wp_users table?
- Get first_name and last_name on user_register hook
- User registration on subsite in multisite configuration
- Lock one of WP Network sites for not registered users
- Allowing logged in users to comment without moderation across a multisite installation
- WordPress Multisite Layered Admins
- How to add multiple existing users to a multisite site?
- 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
- Can simple Admin save options in a Multisite?
- How do I use different domain for subdomains in WP Multisite?
- Deleting a user in multisite
- Identical members roles on all sites in a multisite
- How can I get multisite primary blog (url or path) for current user?
- how do I configure nginx for wordpress multisite, it occurs ERR_TOO_MANY_REDIRECTS
- Why can’t I delete original user in multisite? Options for manual removal
- Can I use multisite functions in a single-site installation?
- How to add edit users capability to a custom role?
- In MultiSite, can some users automatically have Site Admin rights on all sites, without granting them Network Admin access?
- create_users capabilities on a role on multisite
- 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?
- How do I list multisite users for the current site only
- Within the database, where is the flag which says that a user has Super Admin rights?
- Avoid having infinite loops
- Create custom role, multisite, add users/sites?
- 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
- Multisite – One user allowed access to all sites?
- 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?
- WordPress MU users – how are they organized?
- Get users from all/specific blog by user_role and current_user role
- How can I display all Multisite blogs where this user is administrator?
- Redirect authors from upload.php url to Home page in Multisite
- WordPress multi user registration sites
- Multisite and users being listed on network administration dashboard and not main site dashboard
- Using a number for limiting registering or banning on multisite
- Best way to convert two single WordPress sites into Multisite network with shared user base
- Advice on WP site structure using multisite
- Unsure of my options, Multi-blog?
- WordPress Multisite (Network) some site users added to main site users list as subscriber
- Trying to create site-wide Active Directory sync with Multisite
- WordPress Multisite restict user access
- Multisite Login Access Restrictions
- Change a subsite Admin role of a WordPress Multisite after 24 hours registering
- How to use 2 different databases but share the same user in wordpress
- Non-super-admin users cannot access CPT even though I have explicitly added the capabilities to the user role