wordpress在提供郵件提醒的地方都留了hook,方便讓開發者自定義。最新在添加第三方登錄時遇到虛擬郵箱發信問題,為了防止給指定郵件地址后綴發信,可以利用如下wordpress提供的鉤子來實現。
//https://www.wwttl.com/101.html
//禁止用戶注冊時發送電子郵件給管理員
add_filter( 'wp_new_user_notification_email_admin', '__return_false' );// 禁止用戶重置修改密碼時發送電子郵件給管理員
add_filter( 'wp_password_change_notification_email', '__return_false' );// 禁止用戶注冊時發送電子郵件給注冊者
add_filter( 'wp_new_user_notification_email', '__return_false' );// 禁止郵箱地址改變時發送郵件給注冊者
add_filter( 'send_email_change_email', '__return_false' );// 禁止更改密碼時發送電子郵件給注冊者
add_filter( 'send_password_change_email', '__return_false' );
注冊時過濾
//https://www.wwttl.com/101.html
// 注冊時過濾有@oauth.com郵箱地址發送注冊郵件提醒
function filter_email_recipient( $recipient, $user ) {$email = $user->user_email;$allowed_domains = array( 'test.com' );$email_parts = explode( '@', $email );$domain = end( $email_parts );if (!in_array( $domain, $allowed_domains ) ) {return $recipient;}return '';
}
add_filter( 'wp_new_user_notification_email', 'filter_email_recipient', 10, 2 );
評論時過濾
//https://www.wwttl.com/101.html
//過濾評論發送郵件地址
function custom_comment_email_filter( $emails, $comment_id ) {$blacklisted_domains = array( 'test.com' ); $comment = get_comment( $comment_id );$comment_author_email = $comment->comment_author_email;$email_parts = explode( '@', $comment_author_email );$domain = end( $email_parts );if ( in_array( $domain, $blacklisted_domains ) ) {$key = array_search( $comment_author_email, $emails );if ( false !== $key ) {unset( $emails[ $key ] );}}return $emails;
}
add_filter( 'comment_notification_recipients', 'custom_comment_email_filter', 10, 2 );
將代碼中的test.com
換成你需要過濾的郵件地址后綴即可。
修改郵箱時過濾
//https://www.wwttl.com/101.html
// 禁止修改郵箱地址時發送確認郵件
add_filter( 'send_email_change_email', '__return_false' );
我這里直接禁止,如果想過濾,可以參考上面的過濾代碼
修改密碼時過濾
//https://www.wwttl.com/101.html
// 禁止修改密碼時發送密碼重置郵件
add_filter( 'send_password_change_email', '__return_false' );
我這里直接禁止,如果想過濾,可以參考上面的過濾代碼