纯代码禁止WordPress某些用户评论

纯代码禁止WordPress某些用户评论

作者 : 资源客 发布时间: 2019-11-27

使用 wordpress 已经好几年了,但是我们发现带有会员注册的 wordpress 站点居然都没有禁言功能,如果我们网站在某些时候,个别会员用户的评论严重违反站点规定,那么我们就需对该会员用户进行禁言处理了以示惩罚。于是想折腾折腾看看有什么办法,于是上网搜索相关教程但是基本都是关于 WordPress 文章禁止评论的... 所以只能自己动手测试操作了,具体代码如下:

/**
 * WordPress 禁止某些用户评论
 */
//在资料页面添加选项
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 后,后台“编辑用户”时,您就会发现多了一个“禁止用户评论”的选项如下:

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

//阻止已禁止的用户评论
function lxtx_usercmt_authenticate_user( $commentdata ) {
    if( lxtx_usercmt_is_user_banned( $commentdata['user_ID'] ) ){
        err(__('由于严重违反龙笑天下相关规定,该账号已被禁言'));
    }else{
        return $commentdata;
    }
}
if( is_user_logged_in() ){
    add_filter( 'preprocess_comment' , 'lxtx_usercmt_authenticate_user' );
}

具体的禁言效果如下图:

是不是简单实用,如果感觉不错您就来照葫芦画瓢做一个吧!

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » 纯代码禁止WordPress某些用户评论