Manage Columns
The Manage_Columns
class provides a flexible solution to manage and customize the columns displayed in the WordPress admin for specific post types. By passing a configuration object, you can easily add, rename, and define new columns, making it straightforward to adapt post type views in the admin panel to match project requirements.
This class can be instantiated within the Custom_Post_Type class itself, receiving the ‘columns’ argument from $this->args[‘terra_manage_columns’]. Alternatively, it can be used outside of this context and will work for both custom post types and native pages.
Key Features
- Dynamic Configuration: Adjust columns for specific post types by passing an array of settings.
- ACF Integration: Supports columns using Advanced Custom Fields (ACF) for custom data display.
- Native Column Management: Allows customization of native columns, such as featured images or taxonomy terms.
You can find the full class implementation here.
Example Usage
Section titled “Example Usage”Inside Custom_Post_Type class
Section titled “Inside Custom_Post_Type class”<?php
// Instantiate the Manage_Columns class with the required configuration.new Manage_Columns((object) array( // Define the post using Custom_Post_Type variable 'post_type' => $this->post_type,
// Specify columns using Custom_Post_Type argument 'columns' => $this->args['terra_manage_columns']));
?>
Outside Custom_Post_Type class
Section titled “Outside Custom_Post_Type class”<?php
// Instantiate the Manage_Columns class with the required configuration.new Manage_Columns((object) array( // Define the post type (e.g., 'post', 'page', 'custom_post_type') 'post_type' => 'post',
// Specify columns to add and configure 'columns' => array( // Native WordPress column configuration 'featured_image' => array( 'label' => 'Featured Image', // The column header label 'reference' => 'native' // Reference type, can be 'native' or 'acf' ),
// ACF field-based column configuration 'highlighted' => array( 'label' => 'Highlighted', // Custom column label 'reference' => 'acf' // Indicates the data source is ACF ) )));
?>