Created
February 14, 2018 14:12
-
-
Save lenasterg/0b578e8fb57fafefa87acecc4bbb43e7 to your computer and use it in GitHub Desktop.
Sometimes it is useful to find the user_id of eache user you have in your WordPress site. In order to have that kind of functionality, you can add the following code in your theme’s functions.php
This file contains hidden or 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
| /** | |
| * Add the user id column after the user's checkbox | |
| * @param array $array | |
| * @param int $index | |
| * @param array $insert | |
| * @return array | |
| */ | |
| function ls_array_insert($array, $index, $insert) { | |
| return array_slice($array, 0, $index, true) + $insert + array_slice($array, $index, count($array) - $index, true); | |
| } | |
| add_filter('manage_users_columns', function ( $columns ) { | |
| return ls_array_insert($columns, 1, array('user_id' => 'ID')); | |
| }); | |
| /* | |
| * User id Column content | |
| */ | |
| function ls_user_id_column_content($value, $column_name, $user_id) { | |
| if ('user_id' == $column_name) { | |
| return $user_id; | |
| } | |
| return $value; | |
| } | |
| add_action('manage_users_custom_column', 'ls_user_id_column_content', 10, 3); | |
| /** | |
| * Make user id column sortable. | |
| * | |
| * @param Array $columns The original columns | |
| * @return Array $columns The filtered columns | |
| */ | |
| function ls_user_id_column_sortable($columns) { | |
| // Add our columns to $columns array | |
| $columns['user_id'] = 'ID'; | |
| return $columns; | |
| } | |
| add_filter('manage_users_sortable_columns', 'ls_user_id_column_sortable'); | |
| /** | |
| * Set column width | |
| */ | |
| function ls_user_id_column_style() { | |
| echo "<style>.column-user_id{width: 3%}</style>"; | |
| } | |
| add_action('admin_head-users.php', 'ls_user_id_column_style'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment