Last active
May 22, 2019 11:27
-
-
Save chriskoelle/dc67c7eb020d90909fc84f6e56d85499 to your computer and use it in GitHub Desktop.
WordPress - Add Registration Date as a sortable column for users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add Registration Date as a sortable column for users | |
*/ | |
class NH_User_Registration_Column { | |
function __construct() { | |
add_filter( 'manage_users_columns', array($this, 'manage_users_columns') ); | |
add_filter( 'manage_users_custom_column', array($this, 'manage_users_custom_column'), 10, 3 ); | |
add_filter( 'manage_users_sortable_columns', array($this, 'manage_users_sortable_columns') ); | |
} | |
/* | |
* Create a column | |
* @param array $columns Array of all user table columns {column ID} => {column Name} | |
*/ | |
public function manage_users_columns($columns) { | |
$new_columns = array(); | |
foreach($columns as $key => $name): | |
if($key == 'posts') $new_columns['registration_date'] = 'Registration date'; | |
// Add new column before posts column for better formatting | |
$new_columns[$key] = $name; | |
endforeach; | |
return $new_columns; | |
} | |
/* | |
* Fill our new column with the registration dates of the users | |
* @param string $row_output text/HTML output of a table cell | |
* @param string $column_id column ID | |
* @param int $user user ID (in fact - table row ID) | |
*/ | |
public function manage_users_custom_column($row_output, $column_id, $user) { | |
$date_format = 'M j, Y'; | |
switch ( $column_id ) { | |
case 'registration_date' : | |
return date( $date_format, strtotime( get_the_author_meta( 'registered', $user ) ) ); | |
break; | |
default: | |
} | |
return $row_output; | |
} | |
/* | |
* Make our "Registration date" column sortable | |
* @param array $columns Array of all user sortable columns {column ID} => {orderby GET-param} | |
*/ | |
public function manage_users_sortable_columns($columns) { | |
return wp_parse_args( array( 'registration_date' => 'registered' ), $columns ); | |
} | |
} | |
new NH_User_Registration_Column; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment