admin管理员组

文章数量:1122832

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;
}

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;
}
Share Improve this question asked Nov 7, 2013 at 5:41 graphical_forcegraphical_force 1941 gold badge3 silver badges18 bronze badges 1
  • 1 You are not defining the total_time or the logged_in_amount. Neither of these are valid objects of the get_userdata() method. You will need to create the function for each of the values you are trying to ascertain. – josh Commented Nov 7, 2013 at 6:23
Add a comment  | 

3 Answers 3

Reset to default 2

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_filter('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;
}

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().

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

本文标签: customizationadding multiple custom columns to usersphp