Skip to:
Content
Pages
Categories
Search
Top
Bottom

Create an automated numeric Username during registration


  • bastibln
    Participant

    @bastibln

    Hey guys,

    I have been trying a lot but cannot find a working solution for my problem:

    During registration, I don’t want the visitors to choose their own usernames. Instead I want them to have standardized ones, like 8-digit numbers so they all look like “24021515”,”24021516″, etc.

    The reason behind this is the usage of the username while chatting on my buddypress site. The topic of the website is quite formal and I don’t want “crazy” usernames to be seen there.

    Long story short: I have been trying to add functions in the bp_core_signup_user() but without success.

    function custom_bp_core_signup_user($user_id, $user_login, $user_password, $user_email, $usermeta) {
        // Check if user is now registered
        if ($user_id) {
            // Create some dynamic ID
            $dynamic_id = '';
            for ($i = 0; $i < 7; $i++) {
                $dynamic_id .= rand(0, 9);
            }
            // I want all the usernames to start with a "1" and then add 7 random digits
            $new_username = '1' . $dynamic_id;
    
            // Update the user
            wp_update_user(array('ID' => $user_id, 'user_login' => $new_username));
        }
    }
    
    add_action('bp_core_signup_user', 'custom_bp_core_signup_user', 10, 5);

    I have been trying this with the functions.php and with some selfmade plugin. Can anyone help?

    Thanks a lot !

  • You must be logged in to reply to this topic.
Skip to toolbar