Pregunta

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;

    }
}
¿Fue útil?

Solución

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.

Otros consejos

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

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;

}}

Cleanest way is to add CSS to your own column. In this case wp_enqueue_style(...); with:

.column-purchased-leads {
    width: 74px;
    text-align: center
}

You'll get neat column as "Posts".

Licenciado bajo: CC-BY-SA con atribución
scroll top