wpmu_create_user example

if ( $action ) {

    switch ( $action ) {
        case 'newuser':
            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';

                        
$error_data = $error->get_error_data( $code );

                    if ( $error_data ) {
                        $error->add_data( $error_data$code );
                    }
                }
                return $error;
            }
        }

        if ( is_multisite() ) {
            $user_id = wpmu_create_user( $user->user_login, $user->user_pass, $user->user_email );

            if ( ! $user_id ) {
                return new WP_Error(
                    'rest_user_create',
                    __( 'Error creating new user.' ),
                    array( 'status' => 500 )
                );
            }

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

            

        do_action( 'pre_network_site_new_created_user', $email );

        $user_id = username_exists( $domain );
        if ( $user_id ) {
            wp_die( __( 'The domain or path entered conflicts with an existing username.' ) );
        }
        $password = wp_generate_password( 12, false );
        $user_id  = wpmu_create_user( $domain$password$email );
        if ( false === $user_id ) {
            wp_die( __( 'There was an error creating the user.' ) );
        }

        /** * Fires after a new user has been created via the network site-new.php page. * * @since 4.4.0 * * @param int $user_id ID of the newly created user. */
        
 else {
            return new WP_Error( 'already_active', __( 'The site is already active.' )$signup );
        }
    }

    $meta     = maybe_unserialize( $signup->meta );
    $password = wp_generate_password( 12, false );

    $user_id = username_exists( $signup->user_login );

    if ( ! $user_id ) {
        $user_id = wpmu_create_user( $signup->user_login, $password$signup->user_email );
    } else {
        $user_already_exists = true;
    }

    if ( ! $user_id ) {
        return new WP_Error( 'create_user', __( 'Could not create user' )$signup );
    }

    $now = current_time( 'mysql', true );

    if ( empty( $signup->domain ) ) {
        
wp_die( __( 'Cannot create an empty user.' ) );
    }

    $user = wp_unslash( $_POST['user'] );

    $user_details = wpmu_validate_user_signup( $user['username']$user['email'] );

    if ( is_wp_error( $user_details['errors'] ) && $user_details['errors']->has_errors() ) {
        $add_user_errors = $user_details['errors'];
    } else {
        $password = wp_generate_password( 12, false );
        $user_id  = wpmu_create_user( esc_html( strtolower( $user['username'] ) )$passwordsanitize_email( $user['email'] ) );

        if ( ! $user_id ) {
            $add_user_errors = new WP_Error( 'add_user_fail', __( 'Cannot add user.' ) );
        } else {
            /** * Fires after a new user has been created via the network user-new.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.