2

I've added a role of "Customer" and I am working on customizing the Users Screen for that role. What I'd like to do is customize columns based on that particular role.

Note:

For example, take a look at the screenshot. How would I remove the reference to the posts column for only the customer role?

Example of current "Customer" screen

3
  • Are you wanting to keep the column, but remove the number listed there for 'Customer' users? It wouldn't make sense to remove the column entirely, right? I say that because you could easily be viewing a list of users, where some are 'Customers' and others are some other role, and would thus need that column. Jun 8, 2011 at 18:28
  • @tnorthcutt - Note that I am looking at the "Customer" filter (see the Customer in bold in the screenshot). I was able to get it working (see my answer below), I'm just wanting to make sure I've got as good a practice as I can get. Jun 8, 2011 at 19:13
  • Gotcha - I noticed the filter, but didn't realize you'd only want it on that page. Jun 8, 2011 at 21:53

3 Answers 3

1

Manage Columns

It's pretty straight forward using the manage_{post-type-name}_columns filter: Just switch per $capability and unset what you don't need in the $post_columns array.

function wpse19435_manage_columns( $posts_columns )
{
    // First role: add a column - take a look at the second function
    if ( current_user_can( $capability_admin ) )
    {
        $posts_columns['some_column_name'] = _x( 'Whatever', 'column name' );
    }
    // second role: remove a column
    elseif ( current_user_can( $capability_other_role ) )
    {
        unset( $posts_columns['comments'] );
    }
    // default
    else
    {
        // do stuff for all other roles
    }

    return $posts_columns;
}
add_filter( 'manage_{post-type-name}_columns', 'wpse19435_manage_columns' );

Add a column

function wpse19435_manage_single_column( $column_name, $id ) 
{
    switch( $column_name ) 
    {
        case 'some_column_name' :
            // do stuff
            break;

        default :
            // do stuff
            break;
    }

}
add_action('manage_{post-type-name}_custom_column', 'wpse19435_manage_single_column', 10, 2);
3
  • Kaiser, thanks for this. I'll experiment with it and update the question accordingly. Jun 8, 2011 at 1:57
  • 1
    Kaiser, I took a closer look at the code and it is possible that you may have misunderstood the question? I'm actually wanting to add/remove columns to the Users Screen, rather than adding and removing columns to a post type screen. Take a look at the screenshot and updated example copy. Jun 8, 2011 at 17:12
  • @Jonathan Wold No, haven't missunderstood it, but Q/A over here are for a broader community than just the questioner (it's not a usual forum). So: It's a more general answer, but you won#t have to exchange anything than the hook name. Sry for not mentioning it, thought you got around it with 176 rep points...
    – kaiser
    Jun 9, 2011 at 0:06
1

Thanks to Mike23 for the tip. Here's the code that I'm using to add a column to only the "customer" role:

if( $_GET['role'] == "customer" ) { 

add_filter('manage_users_columns', 'add_ecommerce_column');
add_filter('manage_users_custom_column', 'manage_ecommerce_column', 10, 3);


function add_ecommerce_column($columns) {
        $columns['ecommerce'] = 'Ecommerce';
    return $columns;

}

function manage_ecommerce_column($empty='', $column_name, $id) {
    if( $column_name == 'ecommerce' ) {
        return $column_name.$id;    
        }
}
}

Any ideas or suggestions for improvement are very welcomed.

2
  • So just to be clear, you're customizing this interface, for you the administrator? Because if you're doing that with the customer in mind (what does the customer see when he visits this page) then it's better to use current_user_can
    – mike23
    Jun 8, 2011 at 18:37
  • Mike, the customer won't see this page. It is only for administrators or other roles that have the appropriate access. Jun 8, 2011 at 19:11
0

Have a look at current_user_can. With that you can filter your code by roles, and then do whatever you want to customize the interface.

Quick and dirty, with CSS. Put this in your functions.php :

function add_custom_admin_styles() {
    /* Customers */
    if(current_user_can('customer')){
        echo '
        <style type="text/css">
            .column-posts{display:none!important;}  
        </style>';
    }
}
add_action('admin_head', 'add_custom_admin_styles');
5
  • Mike, thanks for the thought. The difference here, at least as I see it, is that I'm trying to customize the view of a selection of users (in this case, "customer"), irrespective of the role of the user viewing the selection. Jun 7, 2011 at 18:44
  • Ok, I see, in that case perhaps you can try something like if( $_GET['role'] == "customer" ) etc. as the role variable is present in the URL when you visit the users.php page in the admin.
    – mike23
    Jun 7, 2011 at 19:12
  • Mike, that's an interesting, thought - thank you! I may give that a try.. Jun 8, 2011 at 17:13
  • display:none is almost never the right way to do something like this, fwiw. I'd bet (but don't know for sure) that there's a better way. Jun 8, 2011 at 18:28
  • @tnorthcutt, true that's why I said "quick and dirty". If he (the admin), is the only one having access to this screen it should be enough though.
    – mike23
    Jun 8, 2011 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.