Allow 3 letter usernames in BuddyPress

Is there a way to allow 3 letter usernames in BuddyPress?

On my multisite, I use the Network Username Restrictions Override plugin, which does a great job, but I can't see anyway of doing it in BuddyPress not connected to a network.

  • Predrag Dubajic

    Hi Moonworks,

    Not sure why that happened, I don't remember seeing it before, I'll try replicating it though :slight_smile:

    As for the character limit, you may want to try with adding this in mu-plugin:

    add_filter( 'wpmu_validate_user_signup', 'wpmudev_wpmu_validate_user_signup_ext', 10, 1 );
    function wpmudev_wpmu_validate_user_signup_ext( $result ){
        $user_name = $result['user_name'];
        $user_email = $result['user_email'];
        $errors = new WP_Error();
        if ( empty( $user_name ) ) {
                $errors->add( 'user_name', __( 'Please enter a username', 'buddypress' ) );
            }
            // User name can't be on the blacklist.
            $illegal_names = get_site_option( 'illegal_names' );
            if ( in_array( $user_name, (array) $illegal_names ) ) {
                $errors->add( 'user_name', __( 'That username is not allowed', 'buddypress' ) );
            }
            // User name must pass WP's validity check.
            if ( ! validate_username( $user_name ) ) {
                $errors->add( 'user_name', __( 'Usernames can contain only letters, numbers, ., -, and @', 'buddypress' ) );
            }
            // Minimum of 4 characters.
            if ( strlen( $user_name ) < 3 ) {
                $errors->add( 'user_name',  __( 'Username must be at least 6 characters', 'buddypress' ) );
            }
            // No underscores. @todo Why not?
            if ( false !== strpos( ' ' . $user_name, '_' ) ) {
                $errors->add( 'user_name', __( 'Sorry, usernames may not contain the character "_"!', 'buddypress' ) );
            }
            // No usernames that are all numeric. @todo Why?
            $match = array();
            preg_match( '/[0-9]*/', $user_name, $match );
            if ( $match[0] == $user_name ) {
                $errors->add( 'user_name', __( 'Sorry, usernames must have letters too!', 'buddypress' ) );
            }
            // Check into signups.
            $signups = BP_Signup::get( array(
                'user_login' => $user_name,
            ) );
            $signup = isset( $signups['signups'] ) && ! empty( $signups['signups'][0] ) ? $signups['signups'][0] : false;
            // Check if the username has been used already.
            if ( username_exists( $user_name ) || ! empty( $signup ) ) {
                $errors->add( 'user_name', __( 'Sorry, that username already exists!', 'buddypress' ) );
            }
            // Validate the email address and process the validation results into
            // error messages.
            $validate_email = bp_core_validate_email_address( $user_email );
            bp_core_add_validation_error_messages( $errors, $validate_email );
            // Assemble the return array.
            $result = array(
                'user_name'  => $user_name,
                'user_email' => $user_email,
                'errors'     => $errors,
            );
            return $result;
    }

    Best regards,
    Predrag

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.