The WordPress hook "manage_pages_custom_column" is a powerful tool for customizing the columns displayed on the "Pages" screen in the WordPress admin panel. This hook is triggered when a custom column needs to be added or modified, allowing developers to inject their own content into the columns of the "Pages" screen.
By utilizing this hook, developers can display additional information or perform custom actions in the columns of the "Pages" screen. This can be particularly useful when building custom functionality for managing pages, such as displaying custom fields, performing calculations, or providing interactive elements for users.
When using the "manage_pages_custom_column" hook, the developer needs to specify a callback function that will be executed when the hook is triggered. This callback function should generate the content for the custom column. The function receives two parameters: the column name and the page ID. The column name parameter allows developers to differentiate between different columns, while the page ID parameter provides access to the specific page being displayed.
Here’s an example usage of the "manage_pages_custom_column" hook to display a custom column with the total number of words in each page:
// Add custom column to the "Pages" screen
add_filter('manage_pages_columns', 'custom_page_columns');
function custom_page_columns($columns) {
$columns['word_count'] = 'Word Count';
return $columns;
}
// Populate custom column with word count
add_action('manage_pages_custom_column', 'populate_word_count_column', 10, 2);
function populate_word_count_column($column_name, $page_id) {
if ($column_name === 'word_count') {
$content = get_post_field('post_content', $page_id);
$word_count = str_word_count(strip_tags($content));
echo $word_count;
}
}
In this example, the "custom_page_columns" function adds a new column with the label "Word Count" to the "Pages" screen. Then, the "populate_word_count_column" function calculates the word count for each page and displays it in the custom column.