How to Customize Username Format in WP SMS Pro for Mobile Registration

Updated on Oct 08, 2024

The WP SMS Pro allows users to register using their mobile numbers. By default, it creates usernames in the format phone_XXXX, where XXXX represents the sanitized mobile number. However, you may want to customize this format to fit your website’s needs better.

For example, instead of phone_XXXX, you might prefer usernames like newuser_XXXX. This can be done easily by using a built-in filter.

Customizing the Username Format

To change the username format to newuser_XXXX, add the following code snippet to your theme’s functions.php file:

function sms_registration_username_format($username, $mobileNumber) {
    $new_username = 'newuser_' . str_replace('+', '', $mobileNumber); // Change the "newuser_" with the prefix of your choice

    return $new_username;
}
add_filter('wp_sms_registration_username', 'sms_registration_username_format', 10, 2);

After adding this code, any user registering via mobile number through the WP SMS plugin will have a username in the format newuser_XXXX.

Additionally, you can implement custom logic to generate usernames. For instance, you could create unique usernames by modifying the mobile number or incorporating other elements to suit your specific needs. As an example, you can hash the mobile number for better privacy:

function sms_registration_username_format($username, $mobileNumber) {
    $hashed_mobile = substr(wp_hash($mobileNumber), 0, 8); // Hash the mobile number using wp_hash and shorten it
    $new_username = 'user_' . $hashed_mobile; // Change the "user_" with the prefix of your choice

    return $new_username;
}
add_filter('wp_sms_registration_username', 'sms_registration_username_format', 10, 2);

Conclusion

The WP SMS plugin is a powerful tool for adding SMS functionality to your WordPress site. It allows users to register using their mobile numbers, making the registration process more streamlined and secure. With its customizable features, such as modifying the default username format, the plugin helps you align the user experience with your site’s branding.