WordPress 後臺用戶列表顯示用戶註冊時間

WordPress後臺的用戶列表默認是沒有顯示用戶註冊時間的,如果你要添加這個功能,將下面的代碼添加到當前主題的 functions.php 文件即可。

註:該方法不支援多站點模式,也隻能在 WP 3.1 以上版本使用。

2013-09-05更新:修正時間顯示相差8小時問題和時間顯示樣式(12-19行);解決與《WordPress 後臺用戶列表添加上次登錄時間》的沖突(43行)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 * WordPress 後臺用戶列表顯示註冊時間
 * https://www.wpdaxue.com/display-user-registerdate.html
 */
class RRHE {
	// Register the column - Registered
	public static function registerdate($columns) {
		$columns['registerdate'] = __('註冊時間', 'registerdate');
		return $columns;
	}
 
	// Display the column content	public static function registerdate_columns( $value, $column_name, $user_id ) {		if ( 'registerdate' != $column_name )			return $value;		$user = get_userdata( $user_id );		$registerdate = get_date_from_gmt($user->user_registered);		return $registerdate;	} 
	public static function registerdate_column_sortable($columns) {
		$custom = array(
		  // meta column id => sortby value used in query
			'registerdate'    => 'registered',
			);
		return wp_parse_args($custom, $columns);
	}
 
	public static function registerdate_column_orderby( $vars ) {
		if ( isset( $vars['orderby'] ) && 'registerdate' == $vars['orderby'] ) {
			$vars = array_merge( $vars, array(
				'meta_key' => 'registerdate',
				'orderby' => 'meta_value'
				) );
		}
		return $vars;
	}
 
}
 
// Actions
add_filter( 'manage_users_columns', array('RRHE','registerdate'));
add_action( 'manage_users_custom_column',  array('RRHE','registerdate_columns'), 15, 3);add_filter( 'manage_users_sortable_columns', array('RRHE','registerdate_column_sortable') );
add_filter( 'request', array('RRHE','registerdate_column_orderby') );

/**
* WordPress 後臺用戶列表顯示註冊時間
* https://www.wpdaxue.com/display-user-registerdate.html
*/
class RRHE {
// Register the column – Registered
public static function registerdate($columns) {
$columns[‘registerdate’] = __(‘註冊時間’, ‘registerdate’);
return $columns;
}
// Display the column content
public static function registerdate_columns( $value, $column_name, $user_id ) {
if ( ‘registerdate’ != $column_name )
return $value;
$user = get_userdata( $user_id );
$registerdate = get_date_from_gmt($user->user_registered);
return $registerdate;
}
public static function registerdate_column_sortable($columns) {
$custom = array(
// meta column id => sortby value used in query
‘registerdate’ => ‘registered’,
);
return wp_parse_args($custom, $columns);
}
public static function registerdate_column_orderby( $vars ) {
if ( isset( $vars[‘orderby’] ) && ‘registerdate’ == $vars[‘orderby’] ) {
$vars = array_merge( $vars, array(
‘meta_key’ => ‘registerdate’,
‘orderby’ => ‘meta_value’
) );
}
return $vars;
}
}
// Actions
add_filter( ‘manage_users_columns’, array(‘RRHE’,’registerdate’));
add_action( ‘manage_users_custom_column’, array(‘RRHE’,’registerdate_columns’), 15, 3);
add_filter( ‘manage_users_sortable_columns’, array(‘RRHE’,’registerdate_column_sortable’) );
add_filter( ‘request’, array(‘RRHE’,’registerdate_column_orderby’) );

最終效果如下

registerdate-wpdaxue_com

發佈留言