add_action hook is not working on user_register in wordpress

60 Views Asked by At

I have a wordpress site, and my registration url is Registration URL.

I have registered a hook to modify user_login after registration if username is more than 10 digits.

if user given mobile number with country code. (12 digits) the function should remove first 2 digits making it national number (10 digits only).

for that i have tried with below code. but it is not working. storing in db full number.

function modify_user_login_on_registration($user_id) {
    // Get the user object
    $user = get_user_by('ID', $user_id);

    // Check if the user object is valid
    if ($user) {
        // Get the user_login (mobile number with country code)
        $user_login = $user->user_login;

        // Check if the user_login is longer than 10 digits
        if (strlen($user_login) > 10) {
            // Extract the last 10 digits as the national number
            $national_number = substr($user_login, -10);

            // Update the user_login with the national number
            wp_update_user(array('ID' => $user_id, 'user_login' => $national_number));
        }
    }
}
add_action('user_register', 'modify_user_login_on_registration');

0

There are 0 best solutions below