wp_sms_registration_username
Customize the username format for mobile number registrations.
The wp_sms_registration_username filter allows you to customize the username format when users register using their mobile numbers. By default, WSMS creates usernames in the format phone_XXXX, where XXXX is the sanitized mobile number.
Syntax
add_filter('wp_sms_registration_username', 'your_callback', 10, 2);
Parameters
| Parameter | Type | Description |
|---|---|---|
$username | string | The default generated username |
$mobileNumber | string | The user’s mobile number |
Return Value
Returns the modified username string.
Examples
Custom Prefix
Change the username format to newuser_XXXX:
function sms_registration_username_format($username, $mobileNumber) {
$new_username = 'newuser_' . str_replace('+', '', $mobileNumber);
return $new_username;
}
add_filter('wp_sms_registration_username', 'sms_registration_username_format', 10, 2);
Hashed Username for Privacy
Generate a privacy-friendly username using a hashed mobile number:
function sms_registration_username_format($username, $mobileNumber) {
$hashed_mobile = substr(wp_hash($mobileNumber), 0, 8);
$new_username = 'user_' . $hashed_mobile;
return $new_username;
}
add_filter('wp_sms_registration_username', 'sms_registration_username_format', 10, 2);
Random Username
Generate a random username with a prefix:
function sms_registration_username_format($username, $mobileNumber) {
$random_suffix = wp_generate_password(6, false);
$new_username = 'member_' . $random_suffix;
return $new_username;
}
add_filter('wp_sms_registration_username', 'sms_registration_username_format', 10, 2);
Where to Add This Code
Add the code snippet to one of these locations:
- Your theme’s
functions.phpfile - A custom plugin
- A code snippets plugin
Related
- wp_sms_add_subscriber - Action hook for new subscribers
Last updated: December 23, 2024