默认的WordPress后台里,对于用户列表的展现,可能趋于国外化,即主要以用户名和姓名的形式来展现,而到了国内的站点,大多数是希望展现昵称,也只有通过昵称方可知晓该账号是哪位;但在默认的WordPress后台是无昵称一栏显示的,则需要我们稍微修改一下规则;
将下面的代码添加到主题的 functions.php 即可
add_filter('manage_users_columns', 'add_user_nickname_column');
function add_user_nickname_column($columns) {
$columns['user_nickname'] = '昵称';
unset($columns['name']);
return $columns;
}
add_action('manage_users_custom_column', 'show_user_nickname_column_content', 20, 3);
function show_user_nickname_column_content($value, $column_name, $user_id) {
$user = get_userdata( $user_id );
$user_nickname = $user->nickname;
if ( 'user_nickname' == $column_name )
return $user_nickname;
return $value;
}
并且支持在后台以昵称进行搜索账号,代码如下
//支持后台昵称搜索
function wpkj_extend_user_search( $u_query ){
// 确保代码仅应用于用户搜索
if ( $u_query->query_vars['search'] ){
$search_query = trim( $u_query->query_vars['search'], '*' );
if ( $_REQUEST['s'] == $search_query ){
global $wpdb;
// 添加昵称搜索查询语句
$u_query->query_from .= " JOIN {$wpdb->usermeta} fname ON fname.user_id = {$wpdb->users}.ID AND fname.meta_key = 'nickname'";
// 设置可搜索的字段
$search_by = array( 'user_login', 'user_email', 'fname.meta_value' );
// 应用到搜索
$u_query->query_where = 'WHERE 1=1' . $u_query->get_search_sql( $search_query, $search_by, 'both' );
}
}
}
add_action('pre_user_query', 'wpkj_extend_user_search');