WordPress實(shí)現(xiàn)用戶(hù)注冊(cè)審核功能
WordPress默認(rèn)直接注冊(cè)登錄了,不需要任何驗(yàn)證,如果被批量注冊(cè)就麻煩了,所以添加一個(gè)審核功能比較好。
corepress主題支持設(shè)置用戶(hù)注冊(cè)審核。注冊(cè)用戶(hù)默認(rèn)需要手動(dòng)審核,審核以后才能登陸,開(kāi)啟審核,可以有效防止用戶(hù)批量注冊(cè)。
這兒講解一下如何通過(guò)WordPress過(guò)濾器來(lái)說(shuō)實(shí)現(xiàn)相關(guān)功能。
用戶(hù)列表界面設(shè)置
WordPress用戶(hù)頁(yè)面,點(diǎn)擊頂部用戶(hù)類(lèi)型,通過(guò)URL中status參數(shù)來(lái)過(guò)濾,那么添加一個(gè)參數(shù)為:unapproved
網(wǎng)址參數(shù)為:網(wǎng)址/wp-admin/users.php?status=unapproved
添加界面過(guò)濾器
add_filter('views_users', 'corepress_views_users');
function corepress_views_users($views)
{
global $wpdb;
if (!current_user_can('edit_users')) return $views;
$current = '';
if (isset($_REQUEST['status']) && $_REQUEST['status'] == 'unapproved') $current = 'class="current"';
$meta_key = 'corepress_approve';
$users = get_users(array(
'meta_query' => array(
array(
'key' => $meta_key,
'value' => '1',
'compare' => '='
)
)
));
$count = count($users);
$views['unapproved'] = '<a href="' . admin_url('users.php') . '?status=unapproved" ' . $current . '>' . '待審核' . ' <span class="count">(' . $count . ')</span></a>';
return $views;
}
這個(gè)時(shí)候,已經(jīng)能顯示待審核的用戶(hù)列表了
實(shí)現(xiàn)用戶(hù)查詢(xún)
通過(guò)user_meta,來(lái)查詢(xún),并返回給前臺(tái)
add_filter('pre_get_users', 'filter_users');
function corepress_filter_users($query)
{
global $pagenow;
if (is_admin() && 'users.php' == $pagenow) {
global $wpdb;
if (!isset($_GET['orderby'])) {
$query->set('orderby', 'registered');
$query->set('order', 'desc');
}
if (isset($_REQUEST['status']) && $_REQUEST['status'] == 'unapproved') {
$query->set('meta_query', array(
array(
'key' => 'corepress_approve',
'value' => '1',
'compare' => '='
)
));
}
}
return $query;
}
?實(shí)現(xiàn)批量修改用戶(hù)審核狀態(tài)
添加批量操作表項(xiàng)
add_filter('bulk_actions-users', 'corepress_add_userlist_approve');
function corepress_add_userlist_approve($actions)
{
if (current_user_can('edit_users')) {
$actions['approve'] = '審核用戶(hù)';
$actions['disapprove'] = '設(shè)置為未審核';
}
return $actions;
}
接管批量操作
add_filter('handle_bulk_actions-users', 'corepress_handle_users', 10, 3);
function corepress_handle_users($redirect_to, $doaction, $ids)
{
if (!$ids || !current_user_can('edit_users')) return $redirect_to;
if ($doaction == 'approve') {
foreach ($ids as $id) {
update_user_meta($id, 'corepress_approve', 0);
}
} else if ($doaction == 'disapprove') {
foreach ($ids as $id) {
update_user_meta($id, 'corepress_approve', 1);
}
}
return $redirect_to;
}
這個(gè)時(shí)候,一套體系已經(jīng)完成了,在注冊(cè)用戶(hù)的時(shí)候,為用戶(hù)添加額外字段,就能實(shí)現(xiàn)審核的功能了。
版權(quán)聲明:
作者:applek
鏈接:http://www.yydfqli.cn/wpuserfilter.html
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載。
THE END