此代码将添加可排序用户注册日期列,并将按注册日期处理用户表的默认排序。
此代码的大部分来自Mika Epstein\'s Recently Registered plugin. 我添加了位来处理默认的排序顺序,这是来自this post.
/**
* Registers users by date registered by default. When user clicks
* other sortable column headers, those will take effect instead.
*/
add_action( \'pre_user_query\', \'wpse209591_order_users_by_date_registered_by_default\' );
function wpse209591_order_users_by_date_registered_by_default( $query ) {
global $pagenow;
if ( ! is_admin() || \'users.php\' !== $pagenow || isset( $_GET[\'orderby\'] ) ) {
return;
}
$query->query_orderby = \'ORDER BY user_registered ASC\';
}
/**
* Registers column for display
*/
add_filter( \'manage_users_columns\', \'wpse209591_users_columns\');
function wpse209591_users_columns( $columns ) {
$columns[\'registerdate\'] = _x( \'Registered\', \'user\', \'your-text-domain\' );
return $columns;
}
/**
* Handles the registered date column output.
*
* This uses the same code as column_registered, which is why
* the date isn\'t filterable.
*
* @global string $mode
*/
add_action( \'manage_users_custom_column\', \'wpse209591_users_custom_column\', 10, 3);
function wpse209591_users_custom_column( $value, $column_name, $user_id ) {
global $mode;
$mode = empty( $_REQUEST[\'mode\'] ) ? \'list\' : $_REQUEST[\'mode\'];
if ( \'registerdate\' != $column_name ) {
return $value;
} else {
$user = get_userdata( $user_id );
if ( is_multisite() && ( \'list\' == $mode ) ) {
$formated_date = __( \'Y/m/d\' );
} else {
$formated_date = __( \'Y/m/d g:i:s a\' );
}
$registered = strtotime( get_date_from_gmt( $user->user_registered ) );
$registerdate = \'<span>\'. date_i18n( $formated_date, $registered ) .\'</span>\' ;
return $registerdate;
}
}
/**
* Makes the column sortable
*/
add_filter( \'manage_users_sortable_columns\', \'wpse209591_users_sortable_columns\' );
function wpse209591_users_sortable_columns( $columns ) {
$custom = array(
// meta column id => sortby value used in query
\'registerdate\' => \'registered\',
);
return wp_parse_args( $custom, $columns );
}
/**
* Calculate the order if we sort by date.
*
*/
add_filter( \'request\', \'wpse209591_users_orderby_column\' );
function wpse209591_users_orderby_column( $vars ) {
if ( isset( $vars[\'orderby\'] ) && \'registerdate\' == $vars[\'orderby\'] ) {
$vars = array_merge( $vars, array(
\'meta_key\' => \'registerdate\',
\'orderby\' => \'meta_value\'
) );
}
return $vars;
}