Method 1
In the wordpress background, all users are sorted by user name by default, and the user registration time is not displayed, so we want to see the user registration time and sort the user registration time in ascending or descending order. Next we will introduce the implementation method. For more information, see, hope to help you.
First, add a registration event column on the user list page.
The code is as follows: |
Copy code |
Add_filter ('manage _ users_columns ', 'My _ users_columns '); Function my_users_columns ($ columns ){ $ Columns ['registered'] = 'registration time '; Return $ columns; } Then return the value through the manage_users_custom_column hook Add_action ('manage _ users_custom_column ', 'output _ my_users_columns', 10, 3 ); Function output_my_users_columns ($ var, $ column_name, $ user_id ){ Switch ($ column_name ){ Case "last_activity ": Return get_user_meta ($ user_id, 'Last _ activity', true ); Case "registered ": Return get_user_by ('id', $ user_id)-> data-> user_registered; Case "sex ": Return get_user_meta ($ user_id, 'sex', true ); Case "occ ": Return get_user_meta ($ user_id, 'Oct', true ); Case "qq ": Return get_user_meta ($ user_id, 'QQ', true ); Break; } } Let's click the "registration time" we added, that is, add the link and there is a small arrow on the right. Add_filter ("manage_users_sortable_columns", 'wenshuo _ users_sortable_columns '); Function wenshuo_users_sortable_columns ($ sortable_columns ){ $ Sortable_columns ['registered'] = 'registered '; Return $ sortable_columns; } Finally, we reset the wordpress query function based on the browser url. Add_action ('pre _ user_query ', 'wenshuo _ users_search_order '); Function wenshuo_users_search_order ($ obj ){ If (! Isset ($ _ REQUEST ['orderby']) | $ _ REQUEST ['orderby'] = 'registered '){ If (! In_array ($ _ REQUEST ['order'], array ('asc ', 'desc '))){ $ _ REQUEST ['order'] = 'desc '; } $ Obj-> query_orderby = "order by user_registered". $ _ REQUEST ['order']. ""; } }
|
The above is all the code for adding the registration time and sorting by the registration time in the wordpress background user list. Add the above code to the functions. php file with all themes.
Method 2
The code is as follows: |
Copy code |
<? 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'] = 'registration 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']. ""; } } |
Save the above as a plug-in. After the upload is activated, you can view the registration time on the user interface and sort it by registration time.