add_user_to_blog example

    populate_site_meta( $site->id, $args['meta'] );

    // Remove all permissions that may exist for the site.     $table_prefix = $wpdb->get_blog_prefix();
    delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true );   // Delete all.     delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // Delete all.
    // Install default site content.     wp_install_defaults( $args['user_id'] );

    // Set the site administrator.     add_user_to_blog( $site->id, $args['user_id'], 'administrator' );
    if ( ! user_can( $args['user_id'], 'manage_network' ) && ! get_user_meta( $args['user_id'], 'primary_blog', true ) ) {
        update_user_meta( $args['user_id'], 'primary_blog', $site->id );
    }

    if ( $switch ) {
        restore_current_blog();
    }

    wp_installing( $orig_installing );

    return true;
}
$primary_blog = get_user_meta( $user_id, 'primary_blog', true );
    $first_blog   = current( $blogs );
    if ( false !== $primary_blog ) {
        if ( ! isset( $blogs[ $primary_blog ] ) ) {
            update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
            $primary = get_site( $first_blog->userblog_id );
        } else {
            $primary = get_site( $primary_blog );
        }
    } else {
        // TODO: Review this call to add_user_to_blog too - to get here the user must have a role on this blog?         $result = add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' );

        if ( ! is_wp_error( $result ) ) {
            update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
            $primary = $first_blog;
        }
    }

    if ( ( ! is_object( $primary ) ) || ( 1 == $primary->archived || 1 == $primary->spam || 1 == $primary->deleted ) ) {
        $blogs = get_blogs_of_user( $user_id, true ); // If a user's primary blog is shut down, check their other blogs.         $ret   = false;
        if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
            
array( 'status' => 500 )
                );
            }

            $user->ID = $user_id;
            $user_id  = wp_update_user( wp_slash( (array) $user ) );

            if ( is_wp_error( $user_id ) ) {
                return $user_id;
            }

            $result = add_user_to_blog( get_site()->id, $user_id, '' );
            if ( is_wp_error( $result ) ) {
                return $result;
            }
        } else {
            $user_id = wp_insert_user( wp_slash( (array) $user ) );

            if ( is_wp_error( $user_id ) ) {
                return $user_id;
            }
        }

        
check_admin_referer( 'add-user', '_wpnonce_add-new-user' );
            $user = $_POST['user'];
            if ( ! is_array( $_POST['user'] ) || empty( $user['username'] ) || empty( $user['email'] ) ) {
                $update = 'err_new';
            } else {
                $password = wp_generate_password( 12, false );
                $user_id  = wpmu_create_user( esc_html( strtolower( $user['username'] ) )$passwordesc_html( $user['email'] ) );

                if ( false === $user_id ) {
                    $update = 'err_new_dup';
                } else {
                    $result = add_user_to_blog( $id$user_id$_POST['new_role'] );

                    if ( is_wp_error( $result ) ) {
                        $update = 'err_add_fail';
                    } else {
                        $update = 'newuser';

                        /** * Fires after a user has been created via the network site-users.php page. * * @since 4.4.0 * * @param int $user_id ID of the newly created user. */
Home | Imprint | This part of the site doesn't use cookies.