• Resolved cLin

    (@clin)


    Hi, I just installed your plugin for my multisite and it works great (now when users sign up, they get automatically added to the user list on the subsite) but when I go to Users > Add Users in my dashboard and select a different from New User Default Role, it doesn’t add that role, instead it goes to the default one.

    I have other plugins installed but it seems like when I disable this plugin, I can add users correctly (but site registrations dont get added to the subsite, which seems to be how multisite was designed) but when I activate this plugin, subsite registration works the way I want but I can’t add users to the right role (they all are set to default role).

    Is it possible, so if I wanted to add an editor manually to be able to create a new user and add the correct role? I’m currently using the Add New user not the Add Existing User in /wp-admin/user-new.php. Currently I have to add user, then edit user and change their role.

    edit: to add more detail, I tried adding new users via the site administrator and network administrator, both add users to default role.

    https://www.ads-software.com/plugins/join-my-multisite/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter cLin

    (@clin)

    Ok, digging around the code, it seems like this section may be part of the problem?

    function join_site( ) {
          ....
            if( !is_user_member_of_blog() ) {
                add_user_to_blog($blog_id, $current_user->ID, $jmm_options['role']);
            }
        }

    Does it go off even when in site admin or only when they are viewing the non site admin portions?

    Plugin Author Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    I had to think about this…

    When you add users to the site, what page do they go to when they complete registration?

    Are these NEW users to the network or existing users?

    Also what options do you have on the JMM plugin? That is, are you auto-adding users to sites (when a logged in user visits, they have a role assigned) or what?

    I have a theory, but I need to know what your JMM settings are before I can reproduce it fully ??

    Thread Starter cLin

    (@clin)

    NEW users to the network. I have automatic membership.

    Plugin Author Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Ah, that would make sense then. The new users visit the site and get the default role, because that gets called via the automatic membership.

    If the users came to the site to log in AND confirm their account on the subsite, that may be getting triggered before/at the same time as the user is added to the site… I wonder if you could trip the same filter with https://www.ads-software.com/plugins/multisite-user-management/

    I’m going to have to play with this to sort out the order of things. I mean, I’m already checking if( !is_user_member_of_blog() ) so if that isn’t set yet, then this totally makes sense. JMM is running before WP sets the user to be a member of the site (or they may be running at the same time). Weird…

    Thread Starter cLin

    (@clin)

    Yea, I understand the automatic membership, that seems to work perfectly, I just don’t know why when adding from here no matter what role I choose, it still sets it as the default one. Once, I disable JMM though, the roles are set based off what I choose. Of course, I also lose the automatic membership.

    Since you add the action to “init” would that also be added on the user add section?

    Plugin Author Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    It happens because for SOME reason WP isn’t actaully adding the user to the site.

    So … here.

    1) You add a new user

    2) User comes to the site to activate account

    3) JMM adds the user as it’s default user role

    4) WP sees the user has a role and ignores what you picked in #1

    The thing is I don’t know why #4 happens last. It should be done at step 2 (the user comes to the site and activates account which gives it a role), but apparently WP adding the new user to the site is being triggered later than I’d thought.

    I’m going to have to walk through the code backwards and see why the creation+registration is not occurring at the same time with WP. I may have to add in a check to see if we’re registering a new user, instead of JUST checking if the user is a member of the site. Dunno how to do that yet!

    Thread Starter cLin

    (@clin)

    Also, in my particular case, I’ve just been bypassing activation by checking the Skip Confirmation Email box.

    is there a way to add a condition to fire the add_user_to_blog only if the user ID being added matches the current user? So in my case, since I’m adding someone else, add_user_to_blog won’t fire and WP will add the role.

    Plugin Author Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    I don’t think that’s the problem, since it’s doing this:

    add_user_to_blog($blog_id, $current_user->ID, $jmm_options['role']);

    If it was tiggering when YOU added the user, then Current User is you, and that would have already bailed on the check for !is_user_member_of_blog() (which you are… )

    Wait… is YOUR id the member of the site?

    And is this happening right away (like you add the user and BOOM they’re default-role) before they confirm the account?

    ETA: Also of note, join_site isn’t called automatically. It’s triggered via do_action on specific events.

    Thread Starter cLin

    (@clin)

    I just ended up fixing it by adding this:

    $role = (isset($_POST['role']) && isset($_POST['user_login'])) ? $_POST['role'] : get_site_option( 'default_user_role', 'subscriber' );
    	add_user_to_blog( $blog_id, $user_id, $role );

    I’m assuming the role name isn’t used anywhere else and just added user_login to narrow down the possibilities. Seems to put in the correct roles based off my testing, do you see any issues wrong with this method?

    Also, I put that under the jmm_activate_user function, not the join site function

    Plugin Author Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    You did it here? https://plugins.trac.www.ads-software.com/browser/join-my-multisite/trunk/lib/shortcode.php#L24

    So it became this?

    /* The registration magic */
    	function jmm_activate_user( $user_id, $password, $meta ) {
    	    global $blog_id;
                $role = (isset($_POST['role']) && isset($_POST['user_login'])) ? $_POST['role'] : get_site_option( 'default_user_role', 'subscriber' );
    	    add_user_to_blog( $blog_id, $user_id, $role );
    	}
    	add_action( 'wpmu_activate_user', 'jmm_activate_user', 10, 3 );

    Why get_site_option( 'default_user_role', 'subscriber' ); and not
    get_option( 'default_role' );

    That obviates the default role setting in the plugin.

    /* The registration magic */
    	function jmm_activate_user( $user_id, $password, $meta ) {
    	    global $blog_id;
                if ( isset($_POST['role']) && isset($_POST['user_login']) ) {
                    $role = $_POST['role'];
                } else {
                    get_option( 'default_role' );
                }
    	    add_user_to_blog( $blog_id, $user_id, $role );
    	}
    	add_action( 'wpmu_activate_user', 'jmm_activate_user', 10, 3 );

    (I prefer to break it out for readability when debugging later ?? )

    Thread Starter cLin

    (@clin)

    Yep ??

    Plugin Author Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Can you test my version that I posted? If that works, I’ll bang on it a little more for some stress testing, and then get in some sanitizing on the post.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Add users from Admin section always default role?’ is closed to new replies.