Created
January 7, 2021 08:52
-
-
Save fumikito/b410d91a301864c5a9d7372882893854 to your computer and use it in GitHub Desktop.
WordPress' update user email confirmation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WP Core logic to change user email. | |
* | |
* This method is monolithic because it depends on admin ui. | |
* So if you need custom user page, | |
* you need re-implement these features. | |
* | |
* @see https://github.com/WordPress/WordPress/blob/master/wp-admin/user-edit.php#L100-L116 | |
* @see https://github.com/WordPress/WordPress/blob/master/wp-includes/user.php#L2994-L3102 | |
*/ | |
// If a user changes their email address, | |
// 1. WordPress store new one as `_new_email` usermeta | |
// 2. Send confirmation email with link trailing hashed email. | |
// 3. The user clicks email and user_email will be updated. | |
/** | |
* Check if hash is appropriate for user. | |
* | |
* @return bool | |
*/ | |
function my_confirm_user_hash( $hash, $user_id ) { | |
$new_email = get_user_meta( $user_id, '_new_email', true ); | |
if ( ! $new_email ) { | |
return false; | |
} | |
return $hash_equals( $new_email['hash'], $hash ); | |
} | |
/** | |
* Customize confirmation mail. | |
* | |
* @param string $email_text Some string should be included. | |
* @param string[] $new_email Array with [ 'hash', 'email' ] | |
* @return string | |
*/ | |
add_filter( 'new_user_email_content', function( $email_text, $new_user_email ){ | |
// Here, you can customize email string. | |
return $email_text; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment