В общем для вывода дополнительных колонок (полей) пользователей в админке wordpress и добавления сортировки по этим полям, код выглядит так (удаление полей этим скриптом не получается, как это сделать читаем тут):
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 46 47 48 49 50 51 52 53 54 55 56 57 |
//add&remove field from user profiles - sorce:http://davidwalsh.name/add-profile-fields function modify_contact_methods($profile_fields) { // Add new fields $profile_fields['company'] = 'Company'; // Remove old fields //unset($profile_fields['aim']); return $profile_fields; } add_filter('user_contactmethods', 'modify_contact_methods'); //Add the colum to the user backend function user_sortable_columns( $columns ) { $columns['company'] = 'Company'; return $columns; } add_filter( 'manage_users_sortable_columns', 'user_sortable_columns' ); //alter the user query and sorts whole list by company function status_order_in_user_query($query){ if('Company'==$query->query_vars['orderby']) { $query->query_from .= " LEFT JOIN wp_usermeta ON wp_users.ID = wp_usermeta.user_id AND meta_key = 'company'"; $query->query_orderby = " ORDER BY wp_usermeta.meta_value ".($query->query_vars["order"] == "ASC" ? "asc " : "desc ");//set sort order } } add_action('pre_user_query', 'status_order_in_user_query'); //makes the user meta searchable and returen results function extended_user_search( $user_query ){ // Make sure this is only applied to user search if ( $user_query->query_vars['search'] ){ $search = trim( $user_query->query_vars['search'], '*' ); if ( $_REQUEST['s'] == $search ){ global $wpdb; $user_query->query_from .= " JOIN wp_usermeta ON wp_users.ID = wp_usermeta.user_id"; $user_query->query_where = 'WHERE 1=1' . $user_query->get_search_sql( $search, array( 'user_login', 'user_email', 'user_nicename', 'meta_value' ), 'both' ); } } } add_action( 'pre_user_query', 'extended_user_search' ); //add columns to User panel list page function add_user_columns( $defaults ) { $defaults['company'] = __('Company', 'user-column'); return $defaults; } add_filter('manage_users_columns', 'add_user_columns', 15, 1); //Print the user data in the new column function add_custom_user_columns($value, $column_name, $id) { if( $column_name == 'company' ) { return get_the_author_meta( 'company', $id ); } } add_action('manage_users_custom_column', 'add_custom_user_columns', 15, 3); |
Благодарочка: https://gist.github.com/TimBHowe/