問題描述
我只是想知道為什麼每次使用者更改他/她的電子郵件地址時,wordpress 都不會傳送確認郵件。
我們如何知道電子郵件地址不是假的或錯誤的?
任何人都可以給我一些程式碼段來實現這個功能?
更新:
這是想法。
-
使用者更改他/她的郵件
-
我們傳送確認電子郵件。
-
如果使用者透過點選確認連結在 X 天內確認該電子郵件,則應更改電子郵件。否則我們應該使用現有的電子郵件。
最佳解決方案
像 SickHippie 釋出的這個功能是 WordPress 原生的,但只適用於多站點設定,所以這裡是您需要的兩個功能,使其在單個站點設定上工作,主要是從核心/wp-admin/user-edit.php file
function custom_send_confirmation_on_profile_email() {
global $errors, $wpdb;
$current_user = wp_get_current_user();
if ( ! is_object($errors) )
$errors = new WP_Error();
if ( $current_user->ID != $_POST['user_id'] )
return false;
if ( $current_user->user_email != $_POST['email'] ) {
if ( !is_email( $_POST['email'] ) ) {
$errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address isn't correct." ), array( 'form-field' => 'email' ) );
return;
}
if ( email_exists( $_POST['email'] ) ) {
$errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address is already used." ), array( 'form-field' => 'email' ) );
delete_user_meta( $current_user->ID . '_new_email' );
return;
}
$hash = md5( $_POST['email'] . time() . mt_rand() );
$new_user_email = array(
'hash' => $hash,
'newemail' => $_POST['email']
);
update_user_meta( $current_user->ID . '_new_email', $new_user_email );
$content = apply_filters( 'new_user_email_content', __( "Dear user,
You recently requested to have the email address on your account changed.
If this is correct, please click on the following link to change it:
###ADMIN_URL###
You can safely ignore and delete this email if you do not want to
take this action.
This email has been sent to ###EMAIL###
Regards,
All at ###SITENAME###
###SITEURL###" ), $new_user_email );
$content = str_replace( '###ADMIN_URL###', esc_url( admin_url( 'profile.php?newuseremail='.$hash ) ), $content );
$content = str_replace( '###EMAIL###', $_POST['email'], $content);
$content = str_replace( '###SITENAME###', get_site_option( 'site_name' ), $content );
$content = str_replace( '###SITEURL###', home_url(), $content );
wp_mail( $_POST['email'], sprintf( __( '[%s] New Email Address' ), get_option( 'blogname' ) ), $content );
$_POST['email'] = $current_user->user_email;
}
}
add_action( 'personal_options_update', 'custom_send_confirmation_on_profile_email' );
// Execute confirmed email change. See send_confirmation_on_profile_email().
function verify_email_change(){
global $errors, $wpdb;
$current_user = wp_get_current_user();
if (in_array($GLOBALS['pagenow'], array('profile.php')) && $current_user->ID > 0) {
if (isset( $_GET[ 'newuseremail' ] ) && $current_user->ID ) {
$new_email = get_user_meta( $current_user->ID . '_new_email' );
if ( $new_email[ 'hash' ] == $_GET[ 'newuseremail' ] ) {
$user->ID = $current_user->ID;
$user->user_email = esc_html( trim( $new_email[ 'newemail' ] ) );
if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->users} WHERE user_login = %s", $current_user->user_login ) ) )
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_email = %s WHERE user_login = %s", $user->user_email, $current_user->user_login ) );
wp_update_user( get_object_vars( $user ) );
delete_user_meta( $current_user->ID . '_new_email' );
wp_redirect( add_query_arg( array('updated' => 'true'), self_admin_url( 'profile.php' ) ) );
die();
}
} elseif ( !empty( $_GET['dismiss'] ) && $current_user->ID . '_new_email' == $_GET['dismiss'] ) {
delete_user_meta( $current_user->ID . '_new_email' );
wp_redirect( add_query_arg( array('updated' => 'true'), self_admin_url( 'profile.php' ) ) );
die();
}
}
}
add_action('plugins_loaded','verify_email_change');
次佳解決方案
這是一個奇怪的’feature’ 。該功能實際上可用於 WordPress(WordPress.com 已啟用其託管部落格服務),但它僅限於多站點。如果你看看/wp-admin/includes/ms.php,你會發現處理這一行的函式 239 send_confirmation_on_profile_email()。
大概你可以把這個函式轉換成你的 functions.php 或者插入一個外掛,以獲得這個功能,可能有一點調整讓它正常工作。它不回答”why”,但這個主題 here 也沒有。
ETA:進一步瞭解,還有一些其他功能,您可能需要複製 – new_user_email_admin_notice()和 update_option_new_admin_email()跳出作為潛在的必要。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。