1

I have customized my admin users column to show the purchased leads of a role "lead buyer". The value of the column is a decimal (the number of leads).

The "posts" column of the users table has a class "num" so the <th> and the <td> are styled with text-align: center;.

I want to add a the "num" class to my custom column too.

Does anyone know in which document you can find the "manage_users_columns" and "manage_users_custom_columns" functions to see if there is a possibility to add the class "num"?

Purchased leads column needs to be aligned center

The class num on the "posts" column

// Add users table header columns
add_filter( 'manage_users_columns', 'gtp_users_table_columns' );
function gtp_users_table_columns( $defaults ) {
    $defaults['purchased-leads'] = __( 'Purchased leads', 'gtp_translate' );
    return $defaults;
}

// Add users table lead purchase column content
add_action( 'manage_users_custom_column', 'gtp_users_table_content', 10, 3 );
function gtp_users_table_content( $value, $column_name, $user_id ) {
    $leads = gtp_get_leads_by_buyer( $user_id );
    switch( $column_name ) {
        case 'purchased-leads' : 
            return $leads->found_posts;
            break;

    }
}
0

The solution is simple. Add the word num when defining the column like this:

// Add users table header columns
add_filter( 'manage_users_columns', 'gtp_users_table_columns' );
function gtp_users_table_columns( $defaults ) {
    $defaults['purchased-leads num'] = __( 'Purchased leads', 'gtp_translate' );
    return $defaults;
}

// Add users table lead purchase column content
add_action( 'manage_users_custom_column', 'gtp_users_table_content', 10, 3 );
function gtp_users_table_content( $value, $column_name, $user_id ) {
    $leads = gtp_get_leads_by_buyer( $user_id );
    switch( $column_name ) {
        case 'purchased-leads num' : 
            return $leads->found_posts;
            break;
    }
}

There are only two changes to your original code:

  1. changed $defaults['purchased-leads'] to $defaults['purchased-leads num'] in the first function
  2. changed case 'purchased-leads' to case 'purchased-leads num' in the second function

WordPress will then assign the num class to both the header and the column. There is no need for any additional div or other element. This will work also for any other custom classes that you would like to add.

1
+50

This is not possible, since the output of your gtp_users_table_content function for the manage_users_custom_column action hook is printed within predefined <td></td> elements. However, you can put a simple div with a class around your output:

// Add users table lead purchase column content
add_action( 'manage_users_custom_column', 'gtp_users_table_content', 10, 3 );
function gtp_users_table_content( $value, $column_name, $user_id ) {
    $leads = gtp_get_leads_by_buyer( $user_id );
    switch( $column_name ) {
        case 'purchased-leads' : 
            return '<div class="num">' . $leads->found_posts . '</div>';
            break;

    }

    return '';
}

Note that I added another return statement at the end of your function, to keep the return statements consistent.

Edit: added table header adjustments

To center the table header cells too:

// Add users table header columns
    add_filter( 'manage_users_columns', 'gtp_users_table_columns' );
    function gtp_users_table_columns( $defaults ) {
        $defaults['purchased-leads'] = '<div class="num">' . __( 'Purchased leads', 'gtp_translate' ) . '</div>';
        return $defaults;
    }
  • Thanks, I did some research in my Wordpress core files and found out that this is not possible indeed. I will gonna try your way and if it works I accept your answer. – Robbert Apr 6 '15 at 12:35
  • I have tried your answer and it works half. The <th> element now does not have the num class so they are not aligned center.. – Robbert Apr 7 '15 at 8:47
  • You can use the same with the table header action hook. I've updated my answer. – redelschaap Apr 7 '15 at 9:29
0

Add Custom Class In Custom Column custom posttype / post/page/ and user list Table .

This is a Simple Please Try This Add column name like "purchased-leads num " space between for separate class. Add Add "num" Class in Column And Auto Text align Center like Post count , no need Any Other css .

add_filter( 'manage_users_columns', 'gtp_users_table_columns' );
function gtp_users_table_columns( $defaults ) {
    $defaults['purchased-leads num'] = __( 'Purchased leads', 'gtp_translate' );
    return $defaults;
}

// Add users table lead purchase column content add_action( 'manage_users_custom_column', 'gtp_users_table_content', 10, 3 ); function gtp_users_table_content( $value, $column_name, $user_id ) { $leads = gtp_get_leads_by_buyer( $user_id ); switch( $column_name ) { case 'purchased-leads num' : return $leads->found_posts; break;

}}
  • 1
    Please add some proper explanation to your code. Please try this is not a proper description of what your code does and how it should be used – Pieter Goosen Apr 7 '15 at 13:20
  • Please Deafult argument Add Something like this $defaults['purchased-leads num'] And switch case add Some this 'purchased-leads num' : – Bhavik Patel Apr 7 '15 at 13:23
  • That is still not a proper description, and you should add your explanation to your answer, not comments. File an edit – Pieter Goosen Apr 7 '15 at 13:25

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.