Can new site owners get added to Primary Site with custom role instead of Subscriber?

When a user goes to my Primary Site and creates their own site using Pro Sites they are automatically added to the Primary Site as a Subscriber. Is there a way to automatically add them as a different role by default?

I have a custom role named "Site Owner" I would like them to receive by default on the Primary Site, instead of the Subscriber role.

To clarify I am not looking to add all users that register to my site with the "Site Owner" role, only users that are automatically added to my site when they create their own site using Pro Sites.

    Adam Czajczyk

    Hello Robert,

    I hope you're well today and thank you for your question!

    The simplest way to assign a role different than "subscriber" would be to set a different default role in main site's settings. That, however, would affect all registering users, not only those that sign up for their own sites.

    That said, I took a look at Pro Sites code in order to see if we could "hook up" to some action to make it happen but it also doesn't seem like a good way to go. That's because Pro Sites actually doesn't create user "on its own" but it's just using core WP hooks/functions to do this. So, if we hooked up to any of them, the callback function would again affect all registrations.

    I'm thinking of a bit different solution then. We could hook up to user login instead: check if a user has any blog or not and if he/she does check his/her user-role on the main site and then, if necessary, update it to your custom role. Would that work for you? Let me know please and I think I might be able to help you with that

    Best regards,
    Adam

    Robert

    I see what you're saying. Too bad there's not a way to trigger this at registration.

    OK I think this would work if this action was triggered at login. Looking forward to your solution Adam.

    I'm curious about a couple of things.

    1) Will this action always happen at login and reset their role to this "Site Owner"? Let's say a Pro Site user has logged in and has their role updated to "Site Owner". After a week I update their role to another custom role, let's say "Site Owner Pro". But what happens if they log in again after I make that change. Will this action be triggered at every login and reset their role back to "Site Owner"?

    2) Do I have to worry about caching interfering? I mean will the cache plugin remember what role a user is or is that not something cache stores?

    Adam Czajczyk

    Hello, Robert!

    1) Will this action always happen at login and reset their role to this "Site Owner"? Let's say a Pro Site user has logged in and has their role updated to "Site Owner". After a week I update their role to another custom role, let's say "Site Owner Pro". But what happens if they log in again after I make that change. Will this action be triggered at every login and reset their role back to "Site Owner"?

    That actually depends on how we want that to work. I can see a couple of ways here to choose from:

    a) it can just "blind update" user-role upon each update
    b) it can do this only upon first login
    c) or it can do as follows:
    - upon each login check current user role
    - compare it against some "exclusion list"
    - if it's already set to "Site Owner" or it's on "exclusion list" - do nothing
    - otherwise update it to "Site Owner".

    Personally, I think option c) would make the most sense here. What do you think?

    2) Do I have to worry about caching interfering? I mean will the cache plugin remember what role a user is or is that not something cache stores?

    I wouldn't worry about it. Cache should not interfere here, just like it doesn't interfere with i.e you updating the user-role manually

    Best regards,
    Adam

    Mahlamusa

    Hello Robert.

    I hope you are doing great today.

    Here is a rough draft of the code you should use. It is a draft and may not work as expected but should give you a foundation for what you are trying to achieve.

    function update_user_role( $user_login, $user ) {
        $user_id = $user->ID;
    	$role = 'Siteowner';
    
    	$exclude_user_list = array(1,2,3,4); //exclude user ID from being updated. you should get this from the database
    	$exclude_roles_list = array('Siteowner Pro');
    
    	$user_roles = $user->roles; //an array of the user's roles
    
    	//exclude role from being updated. If not 'Siteowner Pro', update to
    	if ( !in_array($role, $exclude_roles_list) ) {
    		$user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $role ) );
    	}
    
    	//you can check against $user_roles which is an array of all the roles of the user in the site.
    
    	//or exclude specific users. Skip role change if the specific user is in the list
    	if ( !in_array($user_id, $exclude_user_list) ){
    		$user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $role ) );
    	}
    }
    add_action('wp_login', 'update_user_role', 10, 2);

    This will hook the code to the 'wp_login' hook which runs after a successful login and should work if you are using wordpress default login or a plugin that uses core WordPress function to login users.

    The '$user' parameter in the function, is the WP_User object from which you can get the user ID, roles and other data you may need, in the example I got the '$user->roles' and '$user->ID'. You can modify the code to do what you want to do with it, the code as it stands is a rough draft of solution c) suggested by Adam.

    Unfortunately, it is beyond the purpose of the support forum to provide a custom working solution so I cannot take a lot of time trying to make this work or provide a full solution.

    I hope this helps, let us know if you have questions and we will be glad to help you in any way possible.

    I hope you enjoy your day.

    Cheers,
    Mahlamusa