WordPress按用户注册时间排序

在 WordPress 后台,用户是按照用户名排序的,并且没有显示注册时间,如果我们希望能够在后台看到用户的注册时间,并且按照注册时间排序,可以通过在当前主题的 functions.php 文件添加如下面的代码即可:

add_filter('manage_users_columns','wpjam_add_users_column_reg_time');
function wpjam_add_users_column_reg_time($column_headers){
	$column_headers['reg_time'] = '注册时间';
	return $column_headers;
}

add_filter('manage_users_custom_column', 'wpjam_show_users_column_reg_time',11,3);
function wpjam_show_users_column_reg_time($value, $column_name, $user_id){
	if($column_name=='reg_time'){
		$user = get_userdata($user_id);
		return get_date_from_gmt($user->user_registered);
	}else{
		return $value;
	}
}

add_filter( "manage_users_sortable_columns", 'wpjam_users_sortable_columns' );
function ys_users_sortable_columns($sortable_columns){
	$sortable_columns['reg_time'] = 'reg_time';
	return $sortable_columns;
}
add_action( 'pre_user_query', 'wpjam_users_search_order' );
function wpjam_users_search_order($obj){
	if(!isset($_REQUEST['orderby']) || $_REQUEST['orderby']=='reg_time' ){
		if( !in_array($_REQUEST['order'],array('asc','desc')) ){
			$_REQUEST['order'] = 'desc';
		}
		$obj->query_orderby = "ORDER BY user_registered ".$_REQUEST['order']."";
	}
}