How to invalidate username with spaces in BuddyPress

To invalidate usernames with spaces in BuddyPress, you can use the bp_core_validate_user_signup filter. This filter allows you to modify the validation error messages for user signup fields.

Here’s an example of how you can use the bp_core_validate_user_signup filter to invalidate usernames with spaces:

// Invalidate username with spaces in BuddyPress
// More snippets at wpunplugged.com 

/**
 * @param array $result The validation result array.
 * @return array The modified validation result array.
 */
function my_bp_validate_username( $result ) {
    // Get the username
    $username = $result['user_name'];

    // Check if the username contains spaces
    if ( false !== strpos( $username, ' ' ) ) {
        // Add an error message
        $result['errors']->add( 'user_name', __( 'Usernames cannot contain spaces.', 'my-text-domain' ) );
    }

    return $result;
}
add_filter( 'bp_core_validate_user_signup', 'my_bp_validate_username' );

This code will add an error message to the validation result array if the username contains spaces. The error message will be displayed to the user if they try to sign up with a username that contains spaces.

To add this code to your site, you can add it to your theme’s functions.php file.

Share this post

You might also like...

WooCommerce for WordPress Review
Ecommerce

WooCommerce for WordPress Review

WooCommerce for WordPress is a great choice for anyone looking to create an online store. Its user-friendly interface makes setup and customization simple, while its

Read More »

Comment

Your email address will not be published. Required fields are marked *