admin管理员组

文章数量:1410705

I'm looking for a way to save all email addresses to user meta as lowercase, ideally without validating and asking the user to change it but instead just saving it as lowercase. I've got as far as the following but can't get it to work:

add_action( 'update_user_meta', 'meta_email_tolowercase', 10, 4 );
function meta_email_tolowercase( $meta_id, $object_id, $meta_key, $_meta_value ) {

    if( strpos( $_meta_value, '@' ) !== false && ! ctype_lower( $_meta_value ) ):

        $result = update_user_meta( get_current_user_id(), $meta_key, strtolower( $_meta_value ) );

    endif;
}

I'm looking for a way to save all email addresses to user meta as lowercase, ideally without validating and asking the user to change it but instead just saving it as lowercase. I've got as far as the following but can't get it to work:

add_action( 'update_user_meta', 'meta_email_tolowercase', 10, 4 );
function meta_email_tolowercase( $meta_id, $object_id, $meta_key, $_meta_value ) {

    if( strpos( $_meta_value, '@' ) !== false && ! ctype_lower( $_meta_value ) ):

        $result = update_user_meta( get_current_user_id(), $meta_key, strtolower( $_meta_value ) );

    endif;
}
Share Improve this question asked Dec 12, 2019 at 13:31 Kevin NugentKevin Nugent 5631 gold badge8 silver badges20 bronze badges 1
  • As far as I am aware, there is no such action hook update_user_meta. Where/how is this being applied? If you're doing this on a profile update or something, just apply strtolower() directly to the specific meta field you are targeting. – butlerblog Commented Dec 12, 2019 at 13:43
Add a comment  | 

1 Answer 1

Reset to default 0

you can use this filter to do that

add_filter("sanitize_email", function ($sanitized_email, $email, $message) {

    $sanitized_email = strtolower($sanitized_email);

    return $sanitized_email;

}, 10, 3);

but I am not sure if using "strtolower" is a good idea, does another readers know if it's better to use "mb_strtolower" to handle multibyte characters ?
https://www.php/manual/en/ref.mbstring.php

本文标签: hooksSave User Meta Email Address in Lowercase