Skip to content

Instantly share code, notes, and snippets.

@pbrocks
Created August 4, 2022 13:51
Show Gist options
  • Save pbrocks/275596ac1c8a447e6bfc5e7edab9de22 to your computer and use it in GitHub Desktop.
Save pbrocks/275596ac1c8a447e6bfc5e7edab9de22 to your computer and use it in GitHub Desktop.
How to add filters to alter columns of WP_Users_List_Table.
<?php
/**
* Adjustments to /wp-admin/users.php screen.
*
* @package WP_List_Table
*/
add_filter( 'manage_users_columns', 'add_id_any_position_of_users_columns' );
add_filter( 'manage_users_columns', 'nickname_manage_users_columns', 10, 1 );
add_filter( 'manage_users_custom_column', 'nickname_manage_users_custom_column', 10, 3 );
add_filter( 'manage_users_sortable_columns', 'add_sortable_user_columns' );
add_action( 'admin_head-users.php', 'add_user_id_column_style' );
if ( is_admin() ) {
add_action( 'pre_user_query', 'nickname_user_query' );
}
/**
* Tap into the query to alter sort order of custom column.
*
* @param Object $user_query WP_User_Query Object
*
* @return Void
*/
function nickname_user_query( $user_query ) {
// This will show the default query
// echo '<pre style="margin-left:21%;">Line ' . __LINE__ . ' ' . print_r( $user_query, true ) . '</pre>';
if ( 'nickname' == $user_query->query_vars['orderby'] ) {
global $wpdb;
$user_query->query_from .= "
LEFT OUTER JOIN $wpdb->usermeta AS alias
ON ($wpdb->users.ID = alias.user_id) ";
$user_query->query_where .= " AND alias.meta_key = 'nickname' ";
$user_query->query_orderby = ' ORDER BY alias.meta_value ' . ( $user_query->query_vars['order'] === 'ASC' ? 'asc ' : 'desc ' );
}
// This will show the modified query
// echo '<pre style="margin-left:21%;">Line ' . __LINE__ . ' ' . print_r( $user_query, true ) . '</pre>';
}
/**
* Add column to admin > users
*
* @param Array $columns The column headings for Users admin.
*
* @return Array $columns The modified column headings for Users admin.
*/
function nickname_manage_users_columns( $columns ) {
$columns['nickname'] = 'Nickname';
return $columns;
}
/**
* Retrieving data for created columns.
*
* @param String $output Custom column output. Default empty.
* @param String $column_key Column name.
* @param Integer $user_id ID of the currently-listed user.
*
* @return Void
*/
function nickname_manage_users_custom_column( $output, $column_key, $user_id ) {
switch ( $column_key ) {
case 'nickname':
$output = get_user_meta( $user_id, 'nickname', true );
return $output;
break;
case 'user_id':
$output = $user_id;
return $output;
break;
default:
break;
}
// Default $output is empty.
// echo '<pre style="margin-left:20%;">Line ' . __LINE__ . ' ' . print_r( $output, true ) . '</pre>';
}
/**
* Spe
*
* @param [type] $columns [description]
*/
/**
* Specify what position in the table you want the column.
*
* @param Array $columns The original columns.
*
* @return Array $columns The filtered columns.
*/
function add_id_any_position_of_users_columns( $columns ) {
$insert = [ 'user_id' => 'ID' ];
$index = 1;
return array_slice( $columns, 0, $index, true ) + $insert + array_slice( $columns, $index, count( $columns ) - $index, true );
}
/**
* Make user id column sortable.
*
* @param Array $columns The original columns.
*
* @return Array $columns The filtered columns.
*/
function add_sortable_user_columns( $columns ) {
$columns['user_id'] = 'ID';
$columns['role'] = 'role';
$columns['nickname'] = 'nickname';
return $columns;
}
/**
* Set column width
*/
function add_user_id_column_style() {
echo '<style>.column-user_id{width:5%}.column-username{width:11%}</style>';
}
// add_filter( 'pre_user_query', 'set_user_column_orderby_args' );
// add_filter( 'users_list_table_query_args', 'set_user_column_orderby_args' );
// add_filter( 'request', 'set_user_column_orderby' );
// add_filter( 'pre_user_query', 'set_user_column_orderby' );
/**
* Doesn't work
*
* [set_user_column_orderby description]
*
* @param [type] $args [description]
*/
function set_user_column_orderby_args( $args ) {
global $pagenow;
echo '<pre style="margin-left:21%;">' . __FILE__ . '</pre>';
// if ( empty( $args['orderby'] ) ) {
// $args['orderby'] = 'ID';
// }
// if ( empty( $args['order'] ) ) {
// $args['order'] = 'ASC';
// }
// echo '<pre style="margin-left:21%;">' . print_r( $args, true ) . '</pre>';
// if ( isset( $args['orderby'] ) && 'nickname' == $args['orderby'] ) {
// $args = array_merge( $args, array(
// 'meta_key' => 'nickname',
// 'orderby' => 'meta_value'
// ) );
// }
return $args;
}
/**
* [set_user_column_orderby description]
*
* @param [type] $query [description]
*/
function set_user_column_orderby( $query ) {
global $pagenow;
if ( is_admin() && 'users.php' === $pagenow ) {
$section = $_GET['nickname'][0] ?? $_GET['nickname'][1] ?? null;
if ( null !== $section ) {
$meta_query = array(
array(
'key' => 'nickname',
'value' => $section,
),
);
$query->set( 'meta_key', 'nickname' );
$query->set( 'meta_query', $meta_query );
}
}
// [query_fields] => SQL_CALC_FOUND_ROWS wp_users.ID
// [query_from] => FROM wp_users INNER JOIN wp_usermeta ON ( wp_users.ID = wp_usermeta.user_id )
// [query_where] => WHERE 1=1 AND (
// (
// ( wp_usermeta.meta_key = 'wp_capabilities' AND wp_usermeta.meta_value LIKE '{c538d0e57c7ae080cca2d791cad651e8f321d15353e993cea0d421d64988e415}\"isd\"{c538d0e57c7ae080cca2d791cad651e8f321d15353e993cea0d421d64988e415}' )
// )
// )
// [query_orderby] => ORDER BY user_email ASC
// if ( 'nickname' === $query->get( 'orderby' ) ) {
// $query->set( 'orderby', 'meta_value_num' );
// $query->set( 'meta_key', 'nickname' );
// }
// echo '<pre style="margin-left:21%;">' . print_r( $query, true ) . '</pre>';
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment