B2 WordPress插件:监控评论并屏蔽包含指定关键词的评论,将发表该类评论的用户加入小黑屋24小时

WordPress评论监控插件

使用方法:

在插件目录新建文件夹comment-monitor,然后在该文件夹下新建文件comment-monitor.php

插入如下代码:

<?php
/* 
Plugin Name: WordPress评论监控插件
Description: 监控评论并屏蔽包含指定关键词的评论。将发表该类评论的用户加入小黑屋24小时。
Author: Bcloud
Version: 1.0
*/
/获取屏蔽关键词
$banned_words = get_option( 'banned_words' ); 
//评论过滤
add_filter( 'preprocess_comment', 'check_for_banned_words' ); 
function check_for_banned_words( $comment ) {
global $banned_words;
if (stristr($comment['comment_content'], $banned_words ) !== false ) {
$user = get_user_by( 'id', $comment['user_id'] );
require_once(ABSPATH . 'wp-includes/pluggable.php'); 
wp_set_user_role( $user->ID, 'none' ); 
wp_schedule_single_event(time() + 86400, 'promote_user', array($user->ID) ); 
$comment['comment_approved'] = 0; 
return $comment;
}
}
//24小时后提升用户角色 
add_action( 'promote_user', 'promote_user_to_contributor' ); 
function promote_user_to_contributor( $user_id ) { 
$role = get_option( 'default_role' ); 
wp_set_user_role( $user_id, $role ); 
} 
//管理后台设置页面
add_options_page('Comment Monitor Options', 'Comment Monitor');
function comment_monitor_options() {
?>
<form method="post" action="options.php">
<input type="text" name="<?php echo Comment_Monitor::OPTIONS; ?>[banned_words]" value="<?php echo esc_attr( get_option(Comment_Monitor::OPTIONS . '[banned_words]') ); ?>" />
<p><input type="submit" class="button-primary" value="<?php _e( 'Save Changes' ); ?>" /></p>
</form>
<?php
}
//保存设置 
add_action( 'admin_init', 'comment_monitor_options_init' );
function comment_monitor_options_init(){ 
register_setting( 'comment_monitor_options', Comment_Monitor::OPTIONS );
}
//创建选项 
add_option( Comment_Monitor::OPTIONS ); 
?>

该插件实现了评论内容过滤和用户小黑屋功能。

在后台可以配置要屏蔽的关键词,发布评论时会自动检查内容,一旦发现违规内容,插件会将评论标记为未批准并将用户加入小黑屋24小时。24小时后用户会自动解封。

1. 从选项中获取屏蔽关键词,而不是直接设置

2. 添加了一个后台设置页面,用于管理屏蔽关键词选项

3. 在admin_init钩子中注册了设置,并创建了一个选项用于存储设置

4. 保存设置后,屏蔽关键词将从该选项获取这样,你就可以在后台随时修改和更新屏蔽关键词了。

管理员可以根据网站需要灵活配置,不再局限于插件预设的关键词。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容