一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

WordPress禁止没有Gravatar头像的邮箱提交评论

时间:2014-08-22 编辑:简简单单 来源:一聚教程网

最近被垃圾评论弄烦了,有些目测是人工评论,但是带着广告链接,看着恶心,大部分没有Gravatar头像,于是本博决定阻止掉没有头像的访客正常提交评论,
编辑所用主题的functions.php文件,加入下面的代码:

 代码如下 复制代码
/*
    * @author:vfhky 2013年09月11日20:23
    * @param string $email 用户提交的表单中的email字段
    * @return int 0:无gravatar头像; 1:有gravatar头像
    **/
function vfhky_checkgravatar($email) {
    $email_hash = md5(strtolower(trim($email)));
    $check_uri = 'http://www.gravatar.com/avatar/'.$email_hash.'?d=404';
    $headers = @get_headers($check_uri);
    if (!preg_match("|200|", $headers[0])) {
        return 0;
    } else {
        return 1;
    }
}

本博客使用了Willin Kan大神的ajax提交评论,编辑comments-ajax.php,找到下面的代码:

 代码如下 复制代码

if ( get_option('require_name_email') && !$user->ID ) {
 if ( 6 > strlen($comment_author_email) || '' == $comment_author )
  err( __('Error: please fill the required fields (name, email).') ); // ? wp_die 改?殄e?提示
 elseif ( !is_email($comment_author_email))
  err( __('Error: please enter a valid email address.') ); // ? wp_die 改?殄e?提示
}

修改为:

if ( get_option('require_name_email') && !$user->ID ) {
 if ( 6 > strlen($comment_author_email) || '' == $comment_author )
  err( __('错误:请必须填写昵称以及邮箱。') ); // ? wp_die 改?殄e?提示
 elseif ( !is_email($comment_author_email))
  err( __('错误:请输入一个有效的电子邮箱地址。') ); // ? wp_die 改?殄e?提示
 elseif (vfhky_checkgravatar($comment_author_email) == 0)
  err( __('错误:请使用注册有Gravatar头像的邮箱留言。') );
}

热门栏目