AI Search
Classic Search
 Search Phrase:
 Search Type:
Advanced search options
 Search in Forums:
 Search in date period:

 Sort Search Results by:

AI Assistant
Notifications
Clear all

Prevent users from registering with spaces in their name

3 Posts
2 Users
0 Reactions
160 Views
Posts: 14
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
(@codings)
Eminent Member
Joined: 3 years ago
[#73019]

Is there a way to prevent users from using spaces in their username?


2 Replies
Sofy
Posts: 5824
 Sofy
Admin
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
(@sofy)
Support Team
Joined: 8 years ago

Hi codings,

Great question! Based on the forum discussions here, it's important to understand that username validation in wpForo is actually handled by WordPress core functions rather than wpForo itself. Since WordPress natively allows spaces in usernames by default, you'll need to implement a custom solution at the WordPress level to restrict this.

Here's a step-by-step approach to prevent spaces in usernames:

  1. Use the sanitize_user filter: WordPress provides a filter called sanitize_user that you can hook into to modify username sanitization. Add this code to your theme's functions.php file or create a custom plugin:
    <?php
    add_filter( 'sanitize_user', 'prevent_spaces_in_username', 10, 3 );
    
    function prevent_spaces_in_username( $username, $raw_username, $strict ) {
        if ( strpos( $username, ' ' ) !== false ) {
            return '';
        }
        return $username;
    }
    ?>
  2. Add client-side validation: While server-side validation is essential, also add JavaScript validation to the registration form to provide immediate feedback to users. This improves user experience by catching the issue before form submission.
  3. Use the registration_errors filter: For more robust validation, you can also hook into registration_errors to display a custom error message if someone tries to register with spaces:
    <?php
    add_filter( 'registration_errors', 'check_username_spaces', 10, 3 );
    
    function check_username_spaces( $errors, $sanitized_user_login, $user_email ) {
        if ( isset( $_POST['user_login'] ) && strpos( $_POST['user_login'], ' ' ) !== false ) {
            $errors->add( 'username_spaces', __( 'Usernames cannot contain spaces.', 'your-text-domain' ) );
        }
        return $errors;
    }
    ?>
  4. Test thoroughly: After implementing these filters, test the registration process to ensure spaces are properly rejected and users receive clear feedback about the requirement.

Since these are WordPress core functions, this approach will work with wpForo's registration form as well. Have you already tried implementing custom filters in your WordPress installation, or would you prefer a plugin-based solution instead?


Reply
Posts: 14
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
(@codings)
Eminent Member
Joined: 3 years ago

Thank you! It might be nice to have this as a setting in Forum Settings > Login & Registration. Also maybe a username character limit setting. It's a WordPress core function, but the main use cases that will care about username formatting are community sites using plugins like wpForo. 


Reply
Share: