Add your custom columns to WordPress admin panel tables

What? What did you said on the title? Well, I am not sure this is an appropriate title. But I am going to explain it to you.

Lets say, you are making a theme or a WordPress plugin and you want to show something in the admin panel like this:Custom Post Column

We will see how can you add those columns to the admin panel tables and manipulate data on the table rows. Well, this is very easy to do, because WP has a really nice API to work with.

Now we will add those two columns to our admin panel’s post listing table. Here is the code:

<?php
function test_modify_post_table( $column ) {
    $column['test_budget'] = 'Budget';
    $column['test_expires'] = 'Expires';

    return $column;
}
add_filter( 'manage_posts_columns', 'test_modify_post_table' );


We are adding a filter on "manage_posts_columns". It passes the column names of the post table as an array. We simple add our custom columns with a key-value pair in that array and return it. If you run this code, you can see how easy that was.

Now we have the columns, we need to fill those columns with actual data. We’ll use post custom fields to fill those space. Lets look at the code:

<?php
function test_modify_post_table_row( $column_name, $post_id ) {

    $custom_fields = get_post_custom( $post_id );

    switch ($column_name) {
        case 'test_budget' :
            echo $custom_fields['cf_budget'][0] . ' USD';
            break;

        case 'test_expires' :
        echo $custom_fields['cf_expires'][0] . ' days';
            break;

        default:
    }
}

add_action( 'manage_posts_custom_column', 'test_modify_post_table_row', 10, 2 );

We are adding another action at "manage_posts_custom_column" with having two arguments. One is the column name and another is the post id. Then we are grabbing all the custom fields of the post and showing them according to the column name. The code is self explanatory. These columns will be visible to all of your "Post Type" listings.

For Custom Post Type:

For custom post types, the above filter will be manage_{$post_type}_posts_columns and the action will be manage_{$post->post_type}_posts_custom_column.

So if you have a custom post type named tutorial, for registering the column, use this filter manage_tutorial_posts_columns. To show the value in the column, use this action hook manage_tutorial_posts_custom_column.

For Pages:

For pages, those filter will be "manage_pages_columns" and action is “manage_pages_custom_column“. Simple enough.

User List table?

What if you want to change the user list table? It’s the same process as previous. We can add our custom columns like we did before. But the first filter name will be changed to manage_users_columns. Here there is a slight change in the second filter. We passed two parameter for the posts, but here we will pass THREE paramater. Here is the sample code to add a column "URL" to show their website addresses:

<?php
function test_modify_user_table( $column ) {
    $column['url'] = 'URL';

    return $column;
}

add_filter( 'manage_users_columns', 'test_modify_user_table' );

function test_modify_user_table_row( $val, $column_name, $user_id ) {
    $user = get_userdata( $user_id );

    switch ($column_name) {
        case 'url' :
            return $user->user_url;
            break;

        default:
    }

    return $return;
}

add_filter( 'manage_users_custom_column', 'test_modify_user_table_row', 10, 3 );

user custom column

Hope you enjoyed :D

44 thoughts on “Add your custom columns to WordPress admin panel tables

  1. Great information. Looks like just what I was looking for.

    I would like to add the url to my user list table but not sure how to do it. I am new to coding and do not want to mess anything up. :)

    Could you email me some detailed instructions on what, where and how to do this, or direct me to more information.

    Thanks

  2. My 2 cents: First, the custom column row hook is an action, not a filter and second, to use that action into custom post types you should hook it to ‘manage_{post_type}_posts_custom_column’.

    Hope it helps!

  3. This is great but I want to add a column to the links table and not sure how to modify the code to do this. I’ve tried the code below and it doesn’t work, can anyone help?

  4. function test_modify_links_table( $column ) {
    $column[‘date’] = ‘Date Added';

    return $column;
    }
    add_filter( ‘manage_links_columns’, ‘test_modify_links_table’ );

    function test_modify_links_table_row( $column_name, $link_id ) {

    $custom_fields = get_bookmark( $link_id );

    switch ($column_name) {
    case ‘date’ :
    echo $custom_fields[‘cf_date’][0];
    break;

    default:
    }
    }

    add_filter( ‘manage_links_custom_column’, ‘test_modify_links_table_row’, 10, 2 );

    • After checking the `/wp-admin/includes/class-wp-links-list-table.php`, I don’t think that they provide any filter to modify the links list table

  5. Hi,

    thanks for the code! Works quit nice! Except for one thing:

    I use the following code to add the users appartment number to the user table.

    function test_modify_user_table ($column) {
    $column['hnumber'] = __('House number', 'coolhaven');
    return $column;
    }

    add_filter('manage_users_columns', 'test_modify_user_table');

    function test_modify_user_table_row ($val, $column_name, $user_id) {
    $user = get_userdata($user_id);
    if ($column_name == 'hnumber') {
    return $user->number;
    }
    }

    add_filter('manage_users_custom_column', 'test_modify_user_table_row', 10, 3);

    The code does create a new collumn, but does not put any text in the appropriate fields. I am sure the right data is collected because when I print_r(); the $user->number, it displays the number somewhere on the page.

    Could you please help me figure out what’s wrong?

    Best regards,
    Irian

  6. Thanks for this post.
    I one tables created by plugin I want retrieve data from this table and show in admin column.
    Do you know how can I do it?

  7. I don’t know Is that a perfect place to ask or not, but I need to know. Suppose I have a table for logging my_site. And wp has wp-user table. How can I manage so that If user login to my_site , he does not need to log into my wp_site ! Is it possible or not !

    • I think that’s possible. WordPress uses cookies for logging-in a user, so check wp_generate_auth_cookie() in WordPress. You could manipulate a cookie like that and let logging-in the user.

  8. Thanks, I have one question tho. How to change the order of the columns? Because now the date column is first and my custom columns are after that. I want my custom columns first and the date column as last one (which is the default).

      • So thats the only way?

        I have one more question, is there any difference in using hooks like: (Im currently using these first ones:)

        add_filter(‘manage_edit-slider_columns’, ‘rm_add_slider_columns’);
        add_action(‘manage_posts_custom_column’, ‘rm_slider_columns’, 10, 2);

        vs

        add_filter(‘manage_posts_columns’, ‘rm_add_slider_columns’);
        add_action(‘manage_{custom-post-type}_posts_custom_column’, ‘rm_slider_columns’, 10, 2);

        Or which is the correct way to do this?

        • Seems like manage_edit-{$post_type}_columns is gone from core. Since I posted this tutorial, some name has been changed. I’ve just updated the names. Check the posts and custom post type action and filter names again.

          • For me it works well with the manage-edit-….

            The action Im using is the same as in your article:
            add_action(‘manage_posts_custom_column’, ‘rm_slider_columns’, 10, 2);

            But the filter works with manage-edit-.. I was looking for more tutorials and I’ve found that both cases are used. Do you know which is better to use?

          • Thanks for the links, I now edited the code like this and it seems to work well:

            add_filter(‘manage_slider_posts_columns’, ‘…’);
            add_action(‘manage_slider_posts_custom_column’, ‘…’, 10, 2);

  9. Hi Tareq,

    Thx for the tutorial – very helpful.

    I ultimately decided to go with the plug in /codepress-admin-columns/ instead of customizing too much on my own – I found it thx to you!

Leave a Reply