简体   繁体   中英

Make User Email Lowercase on Wordpress User Registration

I am having some issues with user emails being saved to the database upon registration with capital letters. I know how to update the email field in SQL to LOWER all the values. However, it would be nice to catch this upon registration. I use Woocommerce to create accounts, and after some reading it seems that Woo uses the standard Wordpress user_register hook to create accounts ( got that from this post ).

Example : user registers with Myemail@email.com . I need this to save to their account and the user table as myemail@email.com . I tried to scrape together the following plugin to handle this, but after testing on my site it still results in Myemail@email.com upon registration.

Note : I have tried CSS tricks like style="text-transform:lowercase;" but these only mask the style of the input and don't affect the syntax of the email as it's saved to the database.

/* Update User Email on Register Account - LOWER CASE
------------------------------------------------------------------ */
add_action( 'user_register', 'lower_email_register_form', 10, 1 ); 

function lower_email_register_form( $user_id ) { 

    if ( isset( $_POST['user_email'] ) )
        update_user_meta($user_id, 'user_email', strtolower( $_POST['user_email']) );

}; 

Took some direction from the comments and found a filter that happens before the data is stored. pre_user_email did the trick. Here's my updated plugin that makes user emails lowercase upon registration:

/* Filter User Email on Registration to Ensure Lower Case
------------------------------------------------------------------ */
function filter_pre_user_email( $raw_user_email ) { 
    
    $raw_user_email = strtolower($raw_user_email);
    
    return $raw_user_email; 
}; 
         
// add the filter 
add_filter( 'pre_user_email', 'filter_pre_user_email', 10, 1 ); 

I found this visual guide to registration hooks helpful in identifying the right hook.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM