Display Custom User Fields on Edit Users Screen

The problem

Anyone who would like to add a column to the edit users screen in the admin panel, listing a custom field like the age or membership status of a user, take a look.

The solution

Here’s a basic working example plugin. Just add a folder ‘user-column’ to the plugins folder, put a file ‘user-column.php’ into this folder and paste the following code into the file:

<?php
/*
Plugin Name: Custom User Column
Version: 0.7
Plugin URI: http://www.wpquestions.com/question/show/id/74
Description: WordPress 2.8+ only - Display Custom User Fields on Edit Users Screen
Author: Oliver Schlöbe
Author URI: http://wpseek.com/
*/

function cuc_column_userfield( $defaults ) {
  $defaults['cuc-usercolumn-userfield'] = __('Age', 'user-column');
  return $defaults;
}

function cuc_custom_column_userfield($value, $column_name, $id) {
  if( $column_name == 'cuc-usercolumn-userfield' ) {
    //if ( current_user_can('edit_users') ) // uncomment this to add a capability check
      return get_usermeta($id, 'age'); // 'age' is the custom meta key
  }
}

add_action('manage_users_custom_column', 'cuc_custom_column_userfield', 15, 3);
add_filter('manage_users_columns', 'cuc_column_userfield', 15, 1);
?>

What does it do?

The plugin adds a column “Age” to the user listing page displaying a custom user meta key ‘age’. Change it for whatever custom meta key you like.

Requirements

  • WordPress 2.8

Tags: , , , , ,

Leave a comment