logo

$40
Display Custom User Fields on Edit Users Screen

I would like to add a column to the edit users screen, listing a custom field I've added to the users.

In this case I'm using Register Plus to add the custom user field, and would like to display it on the administrative screen where you see a list of your users. By default it shows Username, Name, E-mail, Role, and Posts.

I would also like to be able to select to display the field via the screen options tab at the top of the screen.

Ryan Imel | 12/21/09 at 5:00am | Edit


(2) Possible Answers Submitted...

  • avatar
    Last edited:
    12/21/09
    7:18am
    Max says:

    If you feel ok, you could hack the wordpress core files.
    I don't know how to do this in terms of plugin or hooks.

    In wp-admin/includes/template.php, function get_column_headers(), find and add this:

    $_wp_column_headers[$page] = array(
    'cb' => '<input type="checkbox" />',
    'username' => __('Username'),
    'name' => __('Name'),
    'email' => __('E-mail'),
    'role' => __('Role'),
    'posts' => __('Posts'),
    'custom' => 'Custom' /* Add here your custom field Id and Caption */
    );
    break;


    Then find the function user_row() in the same file and add this in the switch-case after the posts case:

    case 'posts':
    $attributes = 'class="posts column-posts num"' . $style;
    $r .= "<td $attributes>";
    if ( $numposts > 0 ) {
    $r .= "<a href='edit.php?author=$user_object->ID' title='" . __( 'View posts by this author' ) . "' class='edit'>";
    $r .= $numposts;
    $r .= '</a>';
    } else {
    $r .= 0;
    }
    $r .= "</td>";
    break;
    /*
    Your field
    */
    case 'custom':
    $r .="<td $attributes>Your Values</td>"; /* Here you should retrieve the custom field value associated to the $user_object->ID */
    break;


    For retrieving the custom field value for each user you can make a raw MySQL query in the custom case
    You could use the variable $user_object->ID accessible in the switch-case to get the associated value that your plugin stored in some table.

    Previous versions of this answer: 12/21/09 at 7:18am

  • avatar
    Last edited:
    01/07/10
    9:55pm
    Oliver Schlöbe says:

    Here's a basic working example plugin, that works in WordPress 2.8 and above.

    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: &lt;strong&gt;WordPress 2.8+ only.&lt;/strong&gt; Display Custom User Fields on Edit Users Screen
    Author: Oliver Schlöbe
    Author URI: http://wpseek.com/
    */

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

    function imel_custom_column_userfield($value, $column_name, $id) {
    if( $column_name == 'imel-usercolumn-userfield' ) {
    //if ( current_user_can('edit_users') ) // uncomment this to add a capability check
    return get_usermeta($id, 'age');
    }
    }

    add_action('manage_users_custom_column', 'imel_custom_column_userfield', 15, 3);
    add_filter('manage_users_columns', 'imel_column_userfield', 15, 1);
    ?>

    This adds a column "Age" to the user listing page displaying a custom user meta key 'age'.

    Previous versions of this answer: 12/21/09 at 8:41am | 12/21/09 at 8:41am | 12/21/09 at 8:42am | 12/21/09 at 9:06am | 12/21/09 at 9:09am

This question has expired.





Current status of this question: Completed