WordPress评论邮箱白名单功能简单实现教程

2018-04-0111:07:27网站建设与开发1 3,605 views字数 2257阅读模式

WordPress评论邮箱白名单功能简单实现教程文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/2969.html

关于这个让人头疼让人吐槽、也让部分博主喜欢的评论白名单功能,看似很铁面无私,但其实它就几行代码便可以搞定。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/2969.html

一、白名单列表

在 functions.php 文件扔进以下代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/2969.html

  1. /*
  2. *定义白名单
  3. *龙砚庭 https://loomob.com
  4. */
  5. function lyt_white_list_check($email) {//用邮箱作为判断依据
  6.     $white_list = array_filterexplode( ',', trim(get_option('lyt_white_list'), ',' ) ) );//请根据自用主题的后台选项修改 get_option
  7.     if ( !empty($white_list) && in_array($email$white_list) )
  8.       return true;
  9.     return false;
  10.   }

这里有两个地方需要注意,一是$email,二是 get_option。请根据自用主题的实际情况来修改,这里不做细表。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/2969.html

二、执行机制

同样还是扔到 functions..php 文件中。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/2969.html

  1. /*
  2. *白名单内指定 url 可评论,且指定某页面与文章过滤此规则。
  3. *龙砚庭 https://loomob.com
  4. */
  5. function white_list_spam($comment) {
  6.     $postsid = get_option('lyt_whitelist_id'); //自定义过滤的页面、文章 ID
  7.     define('ALLOW_PAGES', ''.$postsid.'');
  8.     $post_ID = $comment["comment_post_ID"];
  9.     $allow_posts = ALLOW_PAGES ? explode(',', ALLOW_PAGES) : array();
  10.     if(!in_array($post_ID,$allow_posts)){
  11.     if (!lyt_white_list_check($comment['comment_author_email'])) {//通过邮箱判断
  12.         err(__('对不起,您的网站不在龙砚庭博客的评论白名单之内,请邮件联系站长'));//err 或者 wp-die
  13.     }
  14.     }
  15.         return $comment;
  16. }
  17. add_filter('preprocess_comment','white_list_spam');

三、后台选项函数

为了可视化,便于后台直接操作,我们还需要给后台主题选项写一个白名单列表函数,以及过滤页面、文章 ID 的函数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/2969.html

lyt_white_list文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/2969.html

lyt_whitelist_id文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/2969.html

如果你的主题后台选项框架使用的是类似轻量级的 Git 主题主题框架的,那么请直接采用以下代码,放到 theme-options.php 里面合适的位置。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/2969.html

  1. array(
  2.     'name'  => '评论白名单列表',
  3.     'desc'  => '请输入邮箱,并以英文逗号隔开',
  4.     'id'    => 'lyt_white_list',
  5.     'type'  => 'textarea',
  6. ),
  7. array(
  8.     'name'  => '评论白名单过滤 ID',
  9.     'desc'  => '请输要过滤的页面、文章 ID,并以英文逗号隔开',
  10.     'id'    => 'lyt_whitelist_id',
  11.     'type'  => 'textarea',
  12. ),

教程到此结束,剩下的就是在日常运维的时候,慢慢扩充白名单列表。当然,以上白名单列表的实现方式是目前我觉得最简单、便捷的代码,至于更复杂的代码我就不贴了,因为能够实现功能就行,不信你试试!文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/2969.html

题外教程

在老古(boke112)的个人博客,看到他贴的一段代码,发现只需要对其进行简单的修改,应该也能实现评论白名单功能。那么如何修改呢?请将以下代码扔到 functions(具体运行的结果如何,我还没试过,回头我本地扔着玩试试。)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/2969.html

  1. /*
  2. *评论白名单内的邮箱地址无需审核
  3. *龙砚庭 https://loomob.com
  4. */
  5. $white_list = get_option('lyt_white_list');
  6. define('ALLOW_EMAILS', ''.$white_list.'');
  7. function lyt_approved_comment($approved,$commentdata){
  8.     $email_ID = $commentdata['comment_author_email'];
  9.     $approv_emails = ALLOW_EMAILS ? explode(',', ALLOW_EMAILS) : array();
  10.     if (!$approved) {
  11.         if (in_array($email_ID,$approv_emails)) {
  12.             $approved = 1;
  13.         }
  14.     }
  15.     return $approved;
  16. }
  17. add_action('pre_comment_approved', 'lyt_approved_comment', 10, 2);

然后跟步骤三一样,写一个后台选项的函数。日常使用的话,请参照以下范例:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/2969.html

aa@bb.com,cc@dd.com文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/2969.html

效果如何?我也不知道!别问为什么,因为我懒!文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/2969.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/cms/2969.html
Comments  1  访客  1
    • 测试 0

      测试下

    Comment

    匿名网友 填写信息

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    确定