0

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;
}
  • 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 Nov 7 '13 at 6:23
1

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

1

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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