Adding and customizing admin columns in WordPress

Marc Wagner, September 6, 2024

Cus­to­mi­zing the Word­Press admin panel can con­tri­bu­te signi­fi­cant­ly to effi­ci­en­cy and usa­bi­li­ty, espe­ci­al­ly when mana­ging a lar­ge amount of con­tent or cus­tom post types. One of the most effec­ti­ve ways to achie­ve this is by adding and cus­to­mi­zing admin colum­ns for post types, pages, users or cus­tom records.

In this blog post, I’ll show you how to add your own colum­ns to the Word­Press admin area using PHP and cus­to­mi­ze them to your liking.

Why should you customize admin columns? #

The stan­dard colum­ns that Word­Press dis­plays in the admin area are often not suf­fi­ci­ent to show all the infor­ma­ti­on you want to have a quick over­view of. By adding addi­tio­nal colum­ns, you can dis­play important data such as cus­tom fields, post sta­tis­tics or meta­da­ta.

Steps to customize the admin columns in WordPress #

Add columns #

You can use Word­Press hooks such as manage_posts_columns or manage_${post_type}_posts_columns to cus­to­mi­ze the stan­dard colum­ns. For exam­p­le, to add a new column for the post type “Books”, the code looks like this:

function my_custom_book_columns($columns) {
    // Neue Spalte hinzufügen
    $columns['book_author'] = __('Autor');
    return $columns;
}
add_filter('manage_book_posts_columns', 'my_custom_book_columns');

This code adds a new column cal­led “Aut­hor” to the admin panel of the cus­tom post type “Books”.

Show column content #

After you have added the column, you must fill the con­tents of this column for each row in the list of posts. You can do this with the manage_posts_custom_column hook:

function my_custom_book_column_content($column, $post_id) {
    if ($column == 'book_author') {
        $author = get_post_meta($post_id, 'book_author', true);
        echo esc_html($author);
    }
}
add_action('manage_book_posts_custom_column', 'my_custom_book_column_content', 10, 2);

Here, the value of the user-defi­ned field book_author, which is saved for each post, is dis­play­ed in the new “Aut­hor” column.

Add sortable columns #

It is also pos­si­ble to make colum­ns sor­ta­ble, which is par­ti­cu­lar­ly useful if you want to sort by a spe­ci­fic aut­hor or date, for exam­p­le. To do this, add the fil­ter manage_edit-${post_type}_sortable_columns:

function my_sortable_book_columns($columns) {
    $columns['book_author'] = 'book_author';
    return $columns;
}
add_filter('manage_edit-book_sortable_columns', 'my_sortable_book_columns');

This code makes the “Aut­hor” column sor­ta­ble. Plea­se note, howe­ver, that an addi­tio­nal pre_get_posts hook is requi­red for sort­ing to work.

function my_book_column_orderby($query) {
    if (!is_admin()) {
        return;
    }

    $orderby = $query->get('orderby');
    if ('book_author' == $orderby) {
        $query->set('meta_key', 'book_author');
        $query->set('orderby', 'meta_value');
    }
}
add_action('pre_get_posts', 'my_book_column_orderby');

This addi­tio­nal code ensu­res that the user-defi­ned field book_author is sor­ted.

Adjust column width (optional) #

If you want to chan­ge the width of the colum­ns, you can do this with CSS. You can extend the Word­Press admin area with the admin_head hook:

function my_custom_column_width() {
    echo '<style>
        .column-book_author { width: 20%; }
    </style>';
}
add_action('admin_head', 'my_custom_column_width');

The width of the “Aut­hor” column is set to 20 % here.

Example: Custom columns for a plugin or theme #

A com­mon use case is adding cus­tom colum­ns for a cus­tom plug­in or the­me. Here’s ano­ther exam­p­le of how you could add the rating and date of the review for a cus­tom post type “Reviews”:

function my_custom_review_columns($columns) {
    $columns['review_rating'] = __('Bewertung');
    $columns['review_date'] = __('Rezensionsdatum');
    return $columns;
}
add_filter('manage_review_posts_columns', 'my_custom_review_columns');

function my_custom_review_column_content($column, $post_id) {
    switch ($column) {
        case 'review_rating':
            $rating = get_post_meta($post_id, 'review_rating', true);
            echo esc_html($rating);
            break;
        case 'review_date':
            $date = get_post_meta($post_id, 'review_date', true);
            echo esc_html($date);
            break;
    }
}
add_action('manage_review_posts_custom_column', 'my_custom_review_column_content', 10, 2);

In this exam­p­le, you add two new colum­ns — “Rating” and “Review date” — for the post type “Reviews” and fill them with cus­tom fields.

Conclusion #

Adding and cus­to­mi­zing admin colum­ns in Word­Press is a gre­at way to impro­ve the over­view of your con­tent and opti­mi­ze work­flows. With the PHP examp­les shown here, you can dis­play cus­tom infor­ma­ti­on in the admin list views of your web­site.

By adding sor­ta­ble colum­ns, dyna­mi­cal­ly fil­ling column con­tent and making adjus­t­ments such as the width of the colum­ns, you can tail­or the backend of your Word­Press instal­la­ti­on to your needs.

Avatar of Marc Wagner
Marc Wagner

Hi Marc here. I'm the founder of Forge12 Interactive and have been passionate about building websites, online stores, applications and SaaS solutions for businesses for over 20 years. Before founding the company, I already worked in publicly listed companies and acquired all kinds of knowledge. Now I want to pass this knowledge on to my customers.

Similar Topics

Comments

Leave A Comment