AI 智能摘要
主题铺为大家分享一款专为 WordPress子比主题(Zibll)开发的轻量级“作者拉黑”插件。它允许文章或帖子的作者,直接在评论区将讨厌的用户拉黑。被拉黑的用户将无法在该作者的任何内容下继续留言,而其他作者的内容则不受影响。
在运营社区或论坛类型的网站时,我们经常会遇到一些喜欢“口嗨”或者恶意刷屏的用户。如果单纯依赖管理员去封禁账号,不仅效率低下,还可能误伤。
最好的解决办法是:把“拉黑权”交给内容创作者自己。
今天,主题铺为大家分享一款专为 WordPress子比主题(Zibll)开发的轻量级“作者拉黑”插件。它允许文章或帖子的作者,直接在评论区将讨厌的用户拉黑。被拉黑的用户将无法在该作者的任何内容下继续留言,而其他作者的内容则不受影响。
插件功能亮点
- 轻量级开发:代码精简,没有任何多余的数据库表,完全依赖 WordPress 原生机制。
- 即点即用:集成在评论区的操作按钮,作者看到恶意评论,点一下即可拉黑,无需跳转后台。
- 精准权限控制:这与全站封禁不同。它仅仅禁止该用户在你的文章下留言,实现了社区自治。
- 智能适配:后台可选择开启的文章类型(如文章、论坛帖子),灵活度高。
效果演示
1. 后台设置简单明了
插件启用后,在后台“设置”中会多出一个选项。你可以决定哪些类型的文章开启拉黑功能(比如只在论坛开启)。
![图片[1]-Zibll Author Blacklist 2.4版本-WordPress子比主题专属“作者拉黑”插件下载-主题铺](https://cdn.zhutipu.com/wp-content/uploads/2026/01/20260124104249163.png/ztp)
2. 评论区操作体验
当作者查看自己文章下的评论时,会看到一个额外的“拉黑”按钮。
- 正常状态:显示灰色的“拉黑”按钮。
- 已拉黑状态:显示红色的“解除”按钮,一眼即可识别。
![图片[2]-Zibll Author Blacklist 2.4版本-WordPress子比主题专属“作者拉黑”插件下载-主题铺](https://cdn.zhutipu.com/wp-content/uploads/2026/01/20260124104331207.png/ztp)
(点击拉黑后)
![图片[3]-Zibll Author Blacklist 2.4版本-WordPress子比主题专属“作者拉黑”插件下载-主题铺](https://cdn.zhutipu.com/wp-content/uploads/2026/01/20260124104403496.png/ztp)
3. 被拉黑用户的体验
当被拉黑的用户试图再次留言时,会收到如下提示,无法提交评论。
![图片[4]-Zibll Author Blacklist 2.4版本-WordPress子比主题专属“作者拉黑”插件下载-主题铺](https://cdn.zhutipu.com/wp-content/uploads/2026/01/20260124104411641.png/ztp)
插件代码与安装教程
你可以直接将以下代码保存为 .php 文件(例如 zibll-author-blacklist.php),压缩成 .zip 包上传安装;或者直接丢到 wp-content/plugins/ 目录下启用。或者直接从本站下载。
<?php
/**
* Plugin Name: Zibll Author Blacklist
* Description: 子比主题专属作者拉黑插件,净化评论区神器
* Version: 2.4.0
* Author: Dev (主题铺整理优化)
* Text Domain: zibll-blacklist
*/
if (!defined('ABSPATH')) {
exit;
}
class Zibll_Author_Blacklist {
const META_KEY = 'zibll_blocked_users';
const OPTION_KEY = 'zibll_blacklist_config';
private $options;
public function __construct() {
$this->options = get_option(self::OPTION_KEY, [
'enable_global' => '1',
'allowed_types' => ['post', 'forum_post']
]);
add_action('admin_menu', [$this, 'add_admin_menu']);
add_action('admin_init', [$this, 'register_settings']);
add_filter('preprocess_comment', [$this, 'check_comment_permission']);
add_action('wp_ajax_zibll_toggle_block_user', [$this, 'ajax_toggle_block_user']);
add_filter('comment_footer_info', [$this, 'add_blacklist_button_to_footer'], 10, 3);
add_action('wp_footer', [$this, 'load_click_handler_js']);
}
/**
* 渲染按钮:去除图标,调整颜色类
*/
public function add_blacklist_button_to_footer($html, $comment, $depth) {
$post_id = $comment->comment_post_ID;
if (!$this->is_enabled_context($post_id)) return $html;
$current_user_id = get_current_user_id();
$post_author_id = get_post_field('post_author', $post_id);
// 只有文章作者本人登录时才显示按钮
if (!$current_user_id || $current_user_id != $post_author_id) return $html;
$target_user_id = $comment->user_id;
// 不能拉黑游客或自己
if (!$target_user_id || $target_user_id == $current_user_id) return $html;
$blocked_users = get_user_meta($current_user_id, self::META_KEY, true);
if (!is_array($blocked_users)) $blocked_users = [];
$is_blocked = in_array($target_user_id, $blocked_users);
// 状态定义:已拉黑(红色解除) / 未拉黑(灰色拉黑)
$btn_text = $is_blocked ? '解除' : '拉黑';
$btn_class = $is_blocked ? 'c-red' : 'zibll-gray-text';
$btn_html = '<a href="javascript:;" class="zibll-block-btn ' . $btn_class . '" data-userid="' . $target_user_id . '" style="margin-left:10px; font-size:12px;">';
$btn_html .= '<span class="text">' . $btn_text . '</span>';
$btn_html .= '</a>';
return $html . $btn_html;
}
/**
* JS 交互逻辑
*/
public function load_click_handler_js() {
if (!is_singular()) return;
?>
<style>
.zibll-gray-text { color: #999999 !important; }
.zibll-gray-text:hover { color: #666666 !important; }
.zibll-block-btn { transition: all 0.2s; }
</style>
<script type="text/javascript">
jQuery(document).ready(function($) {
var ajaxUrl = '<?php echo admin_url('admin-ajax.php'); ?>';
var nonce = '<?php echo wp_create_nonce('zibll_blacklist_nonce'); ?>';
$('body').on('click', '.zibll-block-btn', function(e) {
e.preventDefault();
var $btn = $(this);
if($btn.hasClass('is-processing')) return;
var targetId = $btn.data('userid');
var $textSpan = $btn.find('.text');
var originalText = $textSpan.text();
$btn.addClass('is-processing').css('opacity', '0.5');
$textSpan.text('处理中');
$.ajax({
url: ajaxUrl,
type: 'POST',
dataType: 'json',
data: { action: 'zibll_toggle_block_user', target_id: targetId, nonce: nonce },
success: function(res) {
$btn.removeClass('is-processing').css('opacity', '1');
if (res.success) {
var $allBtns = $('.zibll-block-btn[data-userid="'+targetId+'"]');
if (res.data.is_blocked) {
$allBtns.removeClass('zibll-gray-text').addClass('c-red');
$allBtns.find('.text').text('解除');
} else {
$allBtns.removeClass('c-red').addClass('zibll-gray-text');
$allBtns.find('.text').text('拉黑');
}
} else {
if (window.tbnotice) tbnotice(res.data.msg, 'error');
else alert(res.data.msg);
$textSpan.text(originalText);
}
},
error: function() {
$btn.removeClass('is-processing').css('opacity', '1');
if (window.tbnotice) tbnotice('网络请求失败', 'error');
$textSpan.text(originalText);
}
});
});
});
</script>
<?php
}
public function add_admin_menu() {
add_options_page('拉黑功能设置', '作者拉黑设置', 'manage_options', 'zibll-blacklist-setting', [$this, 'render_settings_page']);
}
public function register_settings() {
register_setting('zibll_blacklist_group', self::OPTION_KEY);
add_settings_section('zibll_blacklist_main_section', '基础设置', null, 'zibll-blacklist-setting');
add_settings_field('enable_global', '全局开关', [$this, 'field_enable_global_cb'], 'zibll-blacklist-setting', 'zibll_blacklist_main_section');
add_settings_field('allowed_types', '应用范围', [$this, 'field_allowed_types_cb'], 'zibll-blacklist-setting', 'zibll_blacklist_main_section');
}
public function field_enable_global_cb() {
$val = isset($this->options['enable_global']) ? $this->options['enable_global'] : '0';
echo '<label><input type="checkbox" name="' . self::OPTION_KEY . '[enable_global]" value="1" ' . checked(1, $val, false) . ' /> 开启功能</label>';
}
public function field_allowed_types_cb() {
$post_types = get_post_types(['public' => true], 'objects');
$saved_types = isset($this->options['allowed_types']) ? $this->options['allowed_types'] : [];
echo '<fieldset>';
foreach ($post_types as $type) {
if ($type->name === 'attachment') continue;
$checked = in_array($type->name, $saved_types) ? 'checked="checked"' : '';
echo '<label style="display:inline-block; margin-right: 15px;"><input type="checkbox" name="' . self::OPTION_KEY . '[allowed_types][]" value="' . esc_attr($type->name) . '" ' . $checked . '> ' . esc_html($type->label) . '</label>';
}
echo '</fieldset>';
}
public function render_settings_page() {
echo '<div class="wrap"><h1>作者拉黑功能设置</h1><form method="post" action="options.php">';
settings_fields('zibll_blacklist_group');
do_settings_sections('zibll-blacklist-setting');
submit_button();
echo '</form></div>';
}
private function is_enabled_context($post_id) {
if (empty($this->options['enable_global'])) return false;
$post_type = get_post_type($post_id);
$allowed_types = isset($this->options['allowed_types']) ? $this->options['allowed_types'] : [];
return in_array($post_type, $allowed_types);
}
public function check_comment_permission($commentdata) {
if (!$this->is_enabled_context($commentdata['comment_post_ID'])) return $commentdata;
$user_id = $commentdata['user_id'];
if (empty($user_id)) return $commentdata;
$post_author_id = get_post_field('post_author', $commentdata['comment_post_ID']);
if ($post_author_id == $user_id) return $commentdata;
$blocked_users = get_user_meta($post_author_id, self::META_KEY, true);
if (!empty($blocked_users) && is_array($blocked_users) && in_array($user_id, $blocked_users)) {
$error_msg = '抱歉,您已被该内容作者加入黑名单,无法回复。';
if (wp_doing_ajax()) { wp_send_json(['error' => 1, 'msg' => $error_msg]); exit; }
else { wp_die($error_msg); }
}
return $commentdata;
}
public function ajax_toggle_block_user() {
check_ajax_referer('zibll_blacklist_nonce', 'nonce');
if (!is_user_logged_in()) wp_send_json_error(['msg' => '请先登录']);
$current_user_id = get_current_user_id();
$target_user_id = isset($_POST['target_id']) ? intval($_POST['target_id']) : 0;
$blocked_users = get_user_meta($current_user_id, self::META_KEY, true);
if (!is_array($blocked_users)) $blocked_users = [];
$is_blocked = false;
if (in_array($target_user_id, $blocked_users)) {
$blocked_users = array_diff($blocked_users, [$target_user_id]);
$msg = '已解除拉黑';
$is_blocked = false;
} else {
$blocked_users[] = $target_user_id;
$msg = '已拉黑该用户';
$is_blocked = true;
}
update_user_meta($current_user_id, self::META_KEY, array_unique($blocked_users));
wp_send_json_success(['msg' => $msg, 'is_blocked' => $is_blocked]);
}
}
new Zibll_Author_Blacklist();主题铺点评
这款插件非常适合 UGC(用户生成内容)属性较强的网站。它赋予了内容创作者维护自己评论区氛围的权利,能有效提升核心用户的积极性。
如果你的动手能力强,可以尝试修改 CSS,将按钮隐藏到子比主题的“更多操作”下拉菜单中,那样界面会更加整洁。不过目前的直接展示方式,对于震慑恶意评论者来说,或许效果更好。
© 版权声明
THE END

















暂无评论内容