WordPress后台用户列表默认是按名称来排序的,这样我们查看最近注册的用户就很不方便,那么如何按注册时间来排序呢?
将下面代码加到主题的functions.php里:
add_filter( 'manage_users_sortable_columns', 'erphpdown_modify_user_table_row_sortable' );
function erphpdown_modify_user_table_row_sortable( $columns ) {
return wp_parse_args( array( 'reg' => 'registered' ), $columns );
}
function erphpdown_modify_user_table( $column ) {
$column['reg'] = '注册时间';
return $column;
}
add_filter( 'manage_users_columns', 'erphpdown_modify_user_table' );
function erphpdown_modify_user_table_row( $val, $column_name, $user_id ) {
switch ($column_name) {
case 'reg':
$user = get_user_by("ID",$user_id);
return get_date_from_gmt($user->user_registered);
break;
}
return $val;
}
add_filter( 'manage_users_custom_column', 'erphpdown_modify_user_table_row', 10, 3 );
add_action( 'pre_user_query', 'erphpdown_users_search_order' );
function erphpdown_users_search_order($obj){
if(!isset($_REQUEST['orderby']) || $_REQUEST['orderby']=='reg' ){
if( !in_array($_REQUEST['order'],array('asc','desc')) ){
$_REQUEST['order'] = 'desc';
}
$obj->query_orderby = "ORDER BY user_registered ".$_REQUEST['order']."";
}
}