Home >CMS Tutorial >WordPress >Extending Post Columns in Your Admin Areas
This tutorial demonstrates how to customize WordPress administration screens for your post types by modifying the displayed columns. We'll begin by reviewing the default WordPress post type listings and then explore the necessary hooks for column manipulation. Finally, we'll integrate additional post meta data into these columns.
The goal is to empower you to extend and modify your post type administration screens, providing users with more information and actions.
Key Concepts:
manage_{$post_type}_posts_columns
(defines columns) and manage_{$post_type}_posts_custom_column
(populates column data).manage_{$post_type}_posts_columns
. manage_{$post_type}_posts_custom_column
lets you control the content within each column, utilizing $column
and $post_id
variables.WordPress Post Management Interface:
When creating post types (including default "posts" and "pages"), WordPress generates an administrative interface for management. This is typically accessed through the backend's main administration menu.
The above image shows the "Posts" menu and its "All Posts" submenu. Selecting either opens the post administration screen:
This screen lists all posts of the selected type.
Extending the Post Management Interface:
To enhance this interface and provide additional functionality, especially for custom post types with extra meta data, we'll customize the displayed columns.
Customizing and Populating Post Columns:
WordPress displays default columns (e.g., Title, Author, Date for pages). To modify this, use two filters:
1. Customizing Columns (manage_{$post_type}_posts_columns
):
This filter modifies which columns are displayed. Replace {$post_type}
with your post type's name (e.g., page
, post
, services
). The filter receives an associative array ($columns
) mapping column names to titles. You can add, remove, or reorder items in this array.
Example (modifying page columns):
<code class="language-php">function manage_page_columns($columns) { unset($columns['date']); unset($columns['comments']); unset($columns['author']); $columns['page_featured_image'] = 'Featured Image'; $columns['page_template'] = 'Template'; $columns['page_content'] = 'Content'; return $columns; } add_filter('manage_page_posts_columns', 'manage_page_columns');</code>
2. Populating Columns (manage_{$post_type}_posts_custom_column
):
This filter populates the content of each column. It receives two parameters: $column
(the column ID) and $post_id
.
Example (populating page columns):
<code class="language-php">function populate_page_columns($column, $post_id) { if ($column == 'page_featured_image') { if (has_post_thumbnail($post_id)) { echo get_the_post_thumbnail($post_id, 'thumbnail'); } else { echo 'No featured image'; } } elseif ($column == 'page_template') { $template = get_post_meta($post_id, '_wp_page_template', true); $templates = get_page_templates(); echo isset($templates[$template]) ? 'Using: ' . $templates[$template] : 'Default Template'; } elseif ($column == 'page_content') { $page = get_post($post_id); if ($page) { echo wp_trim_words(apply_filters('the_content', $page->post_content), 20, '...'); // Show a trimmed excerpt } } } add_action('manage_page_posts_custom_column', 'populate_page_columns', 10, 2);</code>
The example shows a trimmed excerpt of the page content for brevity. The complete content could be displayed, but it might be unwieldy.
Conclusion:
Customizing admin columns provides significant control over the user interface, particularly for custom post types. This allows for displaying relevant information and potentially adding dynamic actions directly within the admin list. Remember to replace page
with your specific post type and adjust the code to match your needs. The provided examples offer a solid foundation for building more complex customizations.
The above is the detailed content of Extending Post Columns in Your Admin Areas. For more information, please follow other related articles on the PHP Chinese website!