Adding a custom column to the WordPress admin area can be useful when you want to display additional information about your posts or custom post types. Here’s how you can add a custom column:
function wpsnippets_custom_columns_header( $columns ) {
$columns['custom_column'] = __( 'Custom Column', 'wpsnippets' );
return $columns;
}
add_filter( 'manage_posts_columns', 'wpsnippets_custom_columns_header' );
function wpsnippets_custom_columns_content( $column_name, $post_id ) {
if ( $column_name == 'custom_column' ) {
// Add your custom column content here.
echo 'Custom Column Content';
}
}
add_action( 'manage_posts_custom_column', 'wpsnippets_custom_columns_content', 10, 2 );
This code adds a custom column with the header “Custom Column” to the post edit screen. The manage_posts_columns
filter adds the custom column to the columns array, and the manage_posts_custom_column
action is used to display the content of the custom column.
- Modify the code to display the content you want in the custom column.
- Save the file.
That’s it! Now, when you view the post edit screen, you should see the custom column with your content.
Need to add admin columns to a custom post type? See see this snippet: Add Admin Column to Custom Post Type.
Note: If you need to add admin columns to a custom post type, you can replace manage_posts_columns
and manage_posts_custom_column
with manage_{$post_type}_posts_columns
and manage_{$post_type}_posts_custom_column
, respectively, where $post_type
is the name of your custom post type. For some examples, see this snippet: Add Admin Column to Custom Post Type.