Skip to content
This repository has been archived by the owner. It is now read-only.

Multisite user creation does not add user to any sites #2327

Closed
amv opened this issue Feb 29, 2016 · 5 comments

Comments

8 participants
@amv
Copy link

commented Feb 29, 2016

I modified the plugin by adding this after the user has been created correctly:

$blog_id = get_current_blog_id();
add_user_to_blog( $blog_id, $user_id, 'member' );

This seems to work for users who have never registered to any blogs on the MU site, and the community role of "member" is hard-coded.

However if the user has already registered to one of the existing sites, the code never reaches this point.

My current hack for that is the following, which probably is far from ideal (and I am not sure if it is always the expected action to add an existing user to a new site using this call, so you might want to add proper API support for site member administration):

if ( is_multisite() ) {
    $ret = wpmu_validate_user_signup( $user->user_login, $user->user_email );
    if ( is_wp_error( $ret['errors'] ) && ! empty( $ret['errors']->errors ) ) {
        $old_user = get_user_by( 'email', $user->user_email );
        if ( $old_user ) {
            $blog_id = get_current_blog_id();
            add_user_to_blog( $blog_id, $old_user->ID, 'member' );
            array_map( array( $old_user, 'add_role' ), $request['roles'] );
            @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
            return array( 'result' => 'Old user added to site.' );
        }
        else {
            return $ret['errors'];
        }
    }
}
@danielbachhuber

This comment has been minimized.

Copy link
Member

commented Mar 7, 2016

Multisite user creation does not add user to any sites

Can you share the request you're making, the response you receive, and the response you'd expect to receive?

Based on your report, it appears you want to assign a user to a site if they already exist. This isn't a use case WP-API currently supports (related #1922). Until it does, a better approach would be to create your own endpoint for user assignment.

My current hack for that is the following, which probably is far from ideal (and I am not sure if it is always the expected action to add an existing user to a new site using this call, so you might want to add proper API support for site member administration):

We strongly discourage modifying plugin files because doing so prevents you from easily updating. As I mentioned before, a more reliable approach is to create your own endpoint for this need.

@amv

This comment has been minimized.

Copy link
Author

commented Mar 7, 2016

I am calling the following URL

POST https://my-multisite-subdomain.maindomain.com/wp-json/wp/v2/users

This creates an user to the main domain. There is no way for me to add that user to the network of my-multisite-subdomain using the API.

What I would expect is that as I am calling the API using my multisite subdomain, the user would be automatically added to the network of the site specified by that subdomain. Currently it just gets added to the users of the whole multiuser wordpress thingy, so the blog at my-multisite-subdomain.maindomain.com does not see this user in it's network.

I would expect the call to return a redirect to the created user, as I think it does.. Currently there is a curious situation of you being able to fetch the user, but not being able to see the user in the listing of users (as the listing lists only users within the subdomain you are using the API from). I have not verified this, but I expect this to be the case.

To retain a proper REST api design, you might be correct that it is not a good idea to use this POST request to add an existing user to the current subdomain. A better approach might be to just return an error in this case when you are trying to add an user whose username or email is already taken in the site. The coder can then decide to use a separate API endpoint to add that particular user to the site (when such an API endpoint exists).

However I still think that it would be a good idea to have fresh users be automatically added to the site of the subdomain where the api is called from.

@rmccue rmccue added Bug and removed Reporter Feedback labels Sep 22, 2016

@kadamwhite kadamwhite added this to the 2.0 milestone Sep 22, 2016

@joehoyle joehoyle self-assigned this Sep 22, 2016

@rmccue rmccue added Enhancement and removed Bug labels Oct 25, 2016

@rachelbaker

This comment has been minimized.

Copy link
Member

commented Oct 25, 2016

This will be part of a concentrated expansion of multi-site capabilities that we should tackle w/ the multi-site team

@rachelbaker rachelbaker modified the milestones: 2.0, 2.1 Oct 25, 2016

@richardtape

This comment has been minimized.

Copy link

commented Oct 27, 2016

Just throwing my hat into the ring for when it comes to the multisite expansion. It's about time I dived into the REST API and it seems appropriate that it would be for MS stuff. Is there a plan for this already in place (if so where can I find it) or is there a meeting coming up (if so when) and if neither, would you REST API folk want someone to run with that? Naturally, Jeremy would be the most ideal person, but if he doesn't want to or can't dedicate the time, I'm happy to put my hand up. As always, thanks so much for your work on this :)

@nylen

This comment has been minimized.

Copy link
Member

commented Jan 7, 2017

This is on Trac at https://core.trac.wordpress.org/ticket/38526, it's closed as fixed but let's continue discussion there if needed.

@nylen nylen closed this Jan 7, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.