为 WordPress 增加封号功能

在当前 functions.php 文件中插入如下代码:

//在资料页面添加选项
function lxtx_rc_admin_init(){
// 编辑用户资料
add_action( 'edit_user_profile', 'lxtx_rc_edit_user_profile' );
add_action( 'edit_user_profile_update', 'lxtx_rc_edit_user_profile_update' );
}
add_action('admin_init', 'lxtx_rc_admin_init' );

//在个人资料页面添加一个复选框
function lxtx_rc_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_rc_ban"><input name="lxtx_rc_ban" type="checkbox" id="lxtx_rc_ban"
<?php if (lxtx_rc_is_user_banned( $user_id )){echo 'checked="checked"';} ?> /> 请谨慎操作,选中则禁止!</label></td>
</tr>
</table>
<?php
}

//添加一个函数来将这个选项的值保存到数据库中
function lxtx_rc_edit_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_rc_ban'] ) && $_POST['lxtx_rc_ban'] = 'on' ) {
lxtx_rc_ban_user( $user_id );
} else { // 解锁
lxtx_rc_unban_user( $user_id );
}
}

//禁止用户
function lxtx_rc_ban_user( $user_id ) {
$old_status = lxtx_rc_is_user_banned( $user_id );
// 更新状态
if ( !$old_status ) {
update_user_option( $user_id, 'lxtx_rc_banned', true, false );
}
}

//解禁用户
function lxtx_rc_unban_user( $user_id ) {
$old_status = lxtx_rc_is_user_banned( $user_id );
// 更新状态
if ( $old_status ) {
update_user_option( $user_id, 'lxtx_rc_banned', false, false );
}
}

//判断用户是否被禁止
function lxtx_rc_is_user_banned( $user_id ) {
return get_user_option( 'lxtx_rc_banned', $user_id, false );
}

//阻止已禁止的用户登录
function lxtx_rc_authenticate_user( $user ) {
if ( is_wp_error( $user ) ) {
return $user;
}
// 如果用户被禁止,则返回错误提示
$banned = get_user_option( 'lxtx_rc_banned', $user->ID, false );
if ( $banned ) {
return new WP_Error( 'lxtx_rc_banned', __('抱歉,该用户被禁止登录!请联系站长解禁。', 'rc') );
}
return $user;
}
//将该函数挂载到 wp_authenticate_user 钩子
add_filter( 'wp_authenticate_user', 'lxtx_rc_authenticate_user', 1 );

如果你想的话,还可以禁止他评论。

//在资料页面添加选项

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 );

}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇