WordPress 注册/重置密码/更改密码钩子

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' );

我这里直接禁止,如果想过滤,可以参考上面的过滤代码

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值