WordPress 禁止某些用户评论

2018-05-20 19:52 1,444 68 条评论 龙笑天下
Dragon主题购买

WordPress 禁止某些用户评论

前几天看到老古说知乎账号被禁言,我就想了下,貌似我们带有会员注册的 wordpress 站点都没有禁言方面的功能,而且在某些时候,如果某用户的评论严重违规,我们就需对该用户进行禁言处理了。于是想折腾折腾,上网搜索相关教程结果基本都是关于文章禁止评论的... 只能自己动手操作了。

联想到之前发过的一篇关于禁止某用户登录的文章:

WordPress 禁止某些用户登录-BG
WordPress 禁止某些用户登录

WordPress 禁止某些用户登录

在某些特殊情况下,比如某些用户损害了网站的利益,你可能就需要禁止他们登录网站。 要实现这样的功能,可以直接下载安装 Disable Users 或者 BAN Users 插件。当然,本文介绍...

我就依葫芦画瓢的得到了下面的方法:

/**
 * WordPress 禁止某些用户评论 - 龙笑天下
 * https://www.ilxtx.com/wp-ban-user-comment.html
 */
//在资料页面添加选项
function lxtx_usercmt_admin_init(){ 
	// 编辑用户资料
	add_action( 'edit_user_profile', 'lxtx_usercmt_edit_user_profile' );
	add_action( 'edit_user_profile_update', 'lxtx_usercmt_user_profile_update' ); 
}
add_action('admin_init', 'lxtx_usercmt_admin_init' );
//在个人资料页面添加一个复选框
function lxtx_usercmt_edit_user_profile() {
	if ( !current_user_can( 'edit_users' ) ) {
		return;
	} 
	global $user_id; 
	// 用户不能禁止自己
	$current_user = wp_get_current_user();
	$current_user_id = $current_user->ID;
	if ( $current_user_id == $user_id ) {
		return;
	}
	?>
	<!-- <h3>权限设置</h3> -->
	<table class="form-table">
	<tr>
		<th scope="row">禁止用户评论</th>
		<td><label for="lxtx_usercmt_ban"><input name="lxtx_usercmt_ban" type="checkbox" id="lxtx_usercmt_ban" 
		<?php if (lxtx_usercmt_is_user_banned( $user_id )){echo 'checked="checked"';} ?> /> 请谨慎操作,选中则禁止!</label></td>
	</tr>
	</table>
	<?php
}
//添加一个函数来将这个选项的值保存到数据库中
function lxtx_usercmt_user_profile_update() { 
	if ( !current_user_can( 'edit_users' ) ) {
		return;
	} 
	global $user_id; 
	// 用户不能禁止自己
	$current_user    = wp_get_current_user();
	$current_user_id = $current_user->ID;
	if ( $current_user_id == $user_id ) {
		return;
	} 
	// 锁定
	if( isset( $_POST['lxtx_usercmt_ban'] ) && $_POST['lxtx_usercmt_ban'] = 'on' ) {
		lxtx_usercmt_ban_user( $user_id );
	} else { // 解锁
		lxtx_usercmt_unban_user( $user_id );
	} 
}
//禁止用户
function lxtx_usercmt_ban_user( $user_id ) { 
	$old_status = lxtx_usercmt_is_user_banned( $user_id ); 
	// 更新状态
	if ( !$old_status ) {
		update_user_option( $user_id, 'lxtx_usercmt_banned', true, false );
	}
}
//解禁用户
function lxtx_usercmt_unban_user( $user_id ) { 
	$old_status = lxtx_usercmt_is_user_banned( $user_id ); 
	// 更新状态
	if ( $old_status ) {
		update_user_option( $user_id, 'lxtx_usercmt_banned', false, false );
	}
}
//判断用户是否被禁止
function lxtx_usercmt_is_user_banned( $user_id ) {
	return get_user_option( 'lxtx_usercmt_banned', $user_id, false );
}

上面代码加入 functions.php 后,后台“编辑用户”时,会发现多了一个“禁止用户评论”的选项,如下:

WordPress 禁止某些用户评论

当然,上面只是实现了禁言的方法,下面就来把禁言的功能真正使用起来。PS:登录回复可见短代码一直没用过,这次就来用上试试效果~

当前内容已被隐藏,您需要登录并评论才能查看

具体的禁言效果如下图~

WordPress 禁止某些用户评论

另外,本站 Dragon 主题已经自带集成了此功能,更加方便您的使用,欢迎购买体验。

「点点赞赏,手留余香」

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

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

2018-03-12

2018-06-20

发表评论

表情 格式 贴图 链接 私密 签到
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