WordPress 防止冒充博主昵称或邮箱留言

2015-11-23 03:24 636 7 条评论 龙笑天下
Dragon主题购买

WordPress 一直以来都有个问题, 如果博主设置评论不需要审核批准就能发表, 那么有可能被人冒名顶替管理员进行留言。大家应该都知道,Wordpress 留言显示的头像是通过留言者的 Email 地址去匹配 Gravatar 通用头像进行显示的,这时候如果我们的邮箱和用户名被不坏好意的人知晓了(其实这个很容易就能得到),他就可以冒充管理员进行垃圾评论,甚至误导辱骂其他留言者回复等恶意行为。虽说管理员可以随后删除这些评论, 但是万一管理员长期不在线或者有人恶意留言那还是挺麻烦的。

下面就给大家分享 2 段有用的代码,用以避免不怀好意的人冒充管理员(Admin) 昵称或邮箱在 Wordpress 博客里面留言评论,保证 wordpress 安全。

方法 1

/**
 * 防止在 WordPress 别人冒充博主发表评论 - 龙笑天下
 * https://www.ilxtx.com/how-to-prevent-someone-posing-as-bloggers-in-wordpress.html
 */
add_filter( 'preprocess_comment', 'lxtx_check_comment_author' );
function lxtx_check_comment_author($incoming_comment) {
    if ( is_user_logged_in() ) return $incoming_comment;

    $isSpam = 0;

    // 将以下代码中的 lxtx 改成博主昵称
    if (trim($incoming_comment['comment_author']) == 'lxtx'){
        $isSpam = 1;
    }
    // 将以下代码中的 example#ilxtx.com 改成博主 Email
    if (trim($incoming_comment['comment_author_email']) == 'example#ilxtx.com'){
        $isSpam = 1;
    }

    if(!$isSpam) return $incoming_comment;

    wp_die('请勿冒充博主发表评论');
}

方法 2

/**
 * 防止在 WordPress 别人冒充博主发表评论 - 龙笑天下
 * https://www.ilxtx.com/how-to-prevent-someone-posing-as-bloggers-in-wordpress.html
 */
add_filter('preprocess_comment', 'lxtx_no_fake_blog_owner_comment');
function lxtx_no_fake_blog_owner_comment($incoming_comment){
    // 将以下代码中的 龙笑天 和 龙笑天下 改成博主昵称
    $names = array('龙笑天', '龙笑天下');
    // 将以下代码中的 example#ilxtx.com 改成博主 Email
    $emails = array('example#ilxtx.com');

    if( !is_super_admin() ){
        if( in_array(strtolower(trim($incoming_comment['comment_author'])), $names) || in_array(strtolower(trim($incoming_comment['comment_author_email'])), $emails) ){
            wp_die('请不要冒充博主发表评论!');
        }
    }

    return $incoming_comment;
}

以上 2 种方法任选其一,达到的效果是一样的:如果非登陆用户使用管理员昵称或邮箱进行留言回复行为,会直接弹出错误提示。

PS:如果出现某些问题,可以把其中的wp_die改为err试下。

「点点赞赏,手留余香」

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

2015-11-20

2015-12-14

发表评论

表情 格式 贴图 链接 私密 签到
Dragon主题购买阿里云特价云服务器1核2G低至86元,N4共享型服务器3年仅需799元腾讯云特价云服务器1核2G 88元/年 2核4G3M688元/3年,更有千元代金券礼包免费领!
评论
正在努力加载中...
扫一扫二维码分享
×
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies. Learn more