Customizing the WordPress admin panel can contribute significantly to efficiency and usability, especially when managing a large amount of content or custom post types. One of the most effective ways to achieve this is by adding and customizing admin columns for post types, pages, users or custom records.
In this blog post, I’ll show you how to add your own columns to the WordPress admin area using PHP and customize them to your liking.
Why should you customize admin columns? #
The standard columns that WordPress displays in the admin area are often not sufficient to show all the information you want to have a quick overview of. By adding additional columns, you can display important data such as custom fields, post statistics or metadata.
Steps to customize the admin columns in WordPress #
Add columns #
You can use WordPress hooks such as manage_posts_columns
or manage_${post_type}_posts_columns
to customize the standard columns.
For example, 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 called “Author” to the admin panel of the custom post type “Books”.
Show column content #
After you have added the column, you must fill the contents 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-defined field book_author
, which is saved for each post, is displayed in the new “Author” column.
Add sortable columns #
It is also possible to make columns sortable, which is particularly useful if you want to sort by a specific author or date, for example.
To do this, add the filter 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 “Author” column sortable.
Please note, however, that an additional pre_get_posts
hook is required for sorting 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 additional code ensures that the user-defined field book_author
is sorted.
Adjust column width (optional) #
If you want to change the width of the columns, you can do this with CSS.
You can extend the WordPress 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 “Author” column is set to 20 % here.
Example: Custom columns for a plugin or theme #
A common use case is adding custom columns for a custom plugin or theme. Here’s another example of how you could add the rating and date of the review for a custom 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 example, you add two new columns — “Rating” and “Review date” — for the post type “Reviews” and fill them with custom fields.
Conclusion #
Adding and customizing admin columns in WordPress is a great way to improve the overview of your content and optimize workflows. With the PHP examples shown here, you can display custom information in the admin list views of your website.
By adding sortable columns, dynamically filling column content and making adjustments such as the width of the columns, you can tailor the backend of your WordPress installation to your needs.
Comments