Structure
The code that displays the table on the admin page is scattered across WordPress' codebase. Each table has a different PHP file responsible for it. They're found in wp-admin/includes/
.
class-wp-posts-list-table.php
- for posts and pages.class-wp-media-list-table.php
- for media and attachments.class-wp-terms-list-table.php
- for categories and tags.class-wp-comments-list-table.php
- for comments.
There are others. You can look at the source code yourself if you want to learn more about them.
On each file, you'll find a function called get_columns()
responsible for returning an associative array of columns for display. This function builds the default columns array, then lets us customize the output with filters before returning it.
Removing Columns from Posts and Pages Tables
WordPress uses the filters manage_pages_columns
and manage_posts_columns
to filter pages and posts' columns respectively. Additionally, it also uses the filter manage_{$post_type}_posts_columns
, so you can also filter columns if you have a custom post type.
The default columns are:
cb
- checkbox.title
- post title.author
- post author.categories
- post categories.tags
- post tags.taxonomy-{$taxonomy_slug}
- for custom taxonomies.comments
- comment count indicator.date
- published date.
All we need to do is attach a filter, modify the array to remove the key we don't want, then return it.
For example, let's say I want to remove the author column, because I'm the only author, so it's just going to be my name every time wasting horizontal space. The code we would use would be this:
<?php
// removes the author column from an array of columns
function remove_author_column__manage_columns($columns) {
// unset doesn't error if the key is missing or already removed.
// see: https://www.php.net/manual/en/function.unset.php
unset($columns['author']);
return $columns;
}
// removes author column from posts table
add_filter('manage_posts_columns', 'remove_author_column__manage_columns', 10, 1);
// removes author column from pages table
add_filter('manage_pages_columns', 'remove_author_column__manage_columns', 10, 1);
Removing Columns from Media Table
The columns in the media table are:
cb
- checkbox.title
- title.author
- author.categories
- categories.tags
- tags.taxonomy-{$taxonomy_slug}
- custom taxonomies.parent
- what post the attachment is attached to.date
- published date.
In this case the filter is manage_media_columns
. It has the same signature as the other ones, so we can just the same remove_author_column__manage_columns
function that we created above.
<?php
// removes author column from media table
add_filter('manage_media_columns', 'remove_author_column__manage_columns', 10, 1);
Other Tables
The terms and comments table don't seem to use a filter, so I don't know if it's possible to remove the columns using PHP. You might still be able to do it with CSS, though.
The columns in the terms table are:
cb
- checkbox.name
- the name of the term.description
- the description of the term.slug
- the term's slug.links
- but this appears only if the taxonomy islink_category
(presumably the number of links).posts
- number of posts.
The columns in the comments table are:
cb
- checkbox.author
- author.comment
- the message.response
- whom was it in response to.date
- submitted date.
Leave a Reply