adding multiple custom columns to users.php

I’m trying to add two columns to the users.php file. I’m not sure how to add both $total_time_display and $logged_in_amount_display. How could I return both values?

Here is my code:

add_filter('manage_users_columns', 'freeman_add_user_minutes_column');
function freeman_add_user_minutes_column($columns) {
    $columns['total_time'] = 'Total Minutes';
    $columns['logged_in_amount'] = 'Number of Logins';
    return $columns;
}

add_action('manage_users_custom_column',  'freeman_show_user_minutes_column_content', 10, 3);
function freeman_show_user_minutes_column_content($value, $column_name, $user_id) {
    $user = get_userdata( $user_id );
    if ( 'total_time' == $column_name )
        $total_time_display = ($user->total_time);
        return $total_time_display;
    if ( 'logged_in_amount' == $column_name )
        $logged_in_amount_display = ($user->logged_in_amount);
        return $logged_in_amount_display;
    return $value;
}

Related posts

3 comments

  1. Correct to use echo not return. This is action, not the filter. And you can delete return $value; from your callback.

  2. Guessing at what your meta fields are calling, implementing Oleg’s suggestions would mean updating your freeman_show_user_minutes_column_content() function like so:

    add_action('manage_users_custom_column',  'freeman_show_user_minutes_column_content', 10, 3);
    function freeman_show_user_minutes_column_content($value, $column_name, $user_id) {
        $user = get_userdata( $user_id );
        if ( 'total_time' == $column_name )
            echo get_user_meta( $user_id, 'total_time', true );
        if ( 'logged_in_amount' == $column_name )
            echo get_user_meta( $user_id, 'logged_in_amount' );
    }
    

    Your custom meta isn’t part of the $user object, so instead, you have to use get_user_meta().

  3. Thanks for the help buys but I ended up figuring it out. Just echoing the code did not give me the results I wanted but returning it did.

    Here is the code:

    add_filter('manage_users_columns', 'freeman_add_user_minutes_column');
    function freeman_add_user_minutes_column($columns) {
        $columns['total_time'] = 'Total Minutes';
        $columns['logged_in_amount'] = 'Number of Logins';
        return $columns;
    }
    
    add_action('manage_users_custom_column',  'freeman_show_user_minutes_column_content', 10, 3);
    function freeman_show_user_minutes_column_content($value, $column_name, $user_id) {
        $user = get_userdata( $user_id );
        if ( 'total_time' == $column_name )
            $output .= ($user->total_time);
        if ( 'logged_in_amount' == $column_name )
            $output .= ($user->logged_in_amount);
        return $output;
    }
    

Comments are closed.