钩子文档

manage_{$screen->id}_columns

💡 云策文档标注

概述

manage_{$screen->id}_columns 是一个动态过滤器钩子,用于修改特定屏幕(如文章列表、媒体库等)的列表表格列标题。钩子名称中的 $screen->id 动态部分对应屏幕ID,例如文章列表的屏幕ID为 edit-post,因此钩子为 manage_edit-post_columns。

关键要点

  • 钩子用于过滤列表表格的列标题,参数为 $columns(以列ID为键的标题标签数组)。
  • 动态钩子名称基于屏幕ID,如 manage_edit-post_columns 用于文章列表,manage_users_columns 用于用户列表。
  • 使用 manage_media_columns 钩子时需注意,它在 wp-admin/includes/class-wp-media-list-table.php 中传递两个参数,可能导致“参数过少”致命错误。
  • 可通过添加、替换或隐藏列来自定义列表视图,适用于文章、页面、自定义文章类型和用户等屏幕。

代码示例

// 为自定义文章类型“books”添加列
add_filter('manage_books_posts_columns', 'book_cpt_columns');
function book_cpt_columns($columns) {
    $new_columns = array(
        'publisher' => __('Publisher', 'ThemeName'),
        'book_author' => __('Book Author', 'ThemeName'),
    );
    return array_merge($columns, $new_columns);
}

// 隐藏文章和页面的列
add_filter('manage_edit-post_columns', 'my_columns_filter', 10, 1);
add_filter('manage_edit-page_columns', 'my_columns_filter', 10, 1);
function my_columns_filter($columns) {
    unset($columns['author'], $columns['categories'], $columns['tags'], $columns['comments']);
    return $columns;
}

// 为用户列表添加列
add_filter('manage_users_columns', 'add_extra_user_column');
function add_extra_user_column($columns) {
    return array_merge($columns, array('foo' => __('Bar')));
}

注意事项

  • 使用 manage_media_columns 钩子时,需确保处理两个参数以避免致命错误,具体参考官方文档。
  • 在自定义列时,注意钩子名称的正确拼写和屏幕ID的匹配,如 manage_edit-CUSTOMPOSTTYPE_columns 用于自定义文章类型。
  • 使用 unset 函数移除列时,参数列表末尾不应有逗号,以避免语法错误。

📄 原文内容

Filters the column headers for a list table on a specific screen.

Description

The dynamic portion of the hook name, $screen->id, refers to the ID of a specific screen. For example, the screen ID for the Posts list table is edit-post, so the filter for that screen would be manage_edit-post_columns.

Parameters

$columnsstring[]
The column header labels keyed by column ID.

More Information

We need to care while using manage_{$screen->id}_columns hook for media screen because when we used manage_media_columns hook we will receive Too few arguments fatal error in Add New Media File page error.

</pre>



<p class="wp-block-paragraph">This error is coming because in the ‘manage_media_columns’ filter hook in wp-admin/includes/class-wp-media-list-table.php passes two arguments.</p>



<p class="wp-block-paragraph">Hook Details : <a href="https://developer.wordpress.org/reference/hooks/manage_media_columns/">https://developer.wordpress.org/reference/hooks/manage_media_columns/</a></p>
</section>
		
		<section class="wp-block-wporg-code-reference-source"><h2 id="source" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#source">Source</a></h2> <pre class="wp-block-code" data-start="37" aria-label="Function source code"><code id="wporg-source-code" lang="php" class="language-php line-numbers">$column_headers[ $screen->id ] = apply_filters( "manage_{$screen->id}_columns", array() );

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Example migrated from Codex:

    Add Columns

    Suppose you have a ‘books’ custom post type and you want to add the publisher and book author to the list of columns.

    add_filter('manage_books_posts_columns' , 'book_cpt_columns');
    
    function book_cpt_columns($columns) {
    	$new_columns = array(
    		'publisher' => __('Publisher', 'ThemeName'),
    		'book_author' => __('Book Author', 'ThemeName'),
    	);
    
        return array_merge($columns, $new_columns);
    }

    Replace Columns

    Here’s another example that removes and replaces some of the columns.

    add_filter('manage_books_posts_columns' , 'book_cpt_columns');
    
    function book_cpt_columns($columns) {
    	unset(
    		$columns['author'],
    		$columns['comments']
    	);
    
    	$new_columns = array(
    		'publisher' => __('Publisher', 'ThemeName'),
    		'book_author' => __('Book Author', 'ThemeName'),
    	);
    
        return array_merge($columns, $new_columns);
    }

    Note that unlike an array data type, the unset PHP function accepts arguments in which the last argument should NOT end with a comma.

    Hide Columns

    Here is an example of how to remove or (hide) columns from Pages / Posts and Custom Post Types without adding any.

    You can also add the name of the custom-post type and filter them out in there too… But if you have 5 custom post types you will need five different filters.

    Note: Replace the text CUSTOMPOSTTYPE below with the name of your post type.

    // Filter pages
    add_filter( 'manage_edit-page_columns', 'my_columns_filter',10, 1 );	
    
    // Filter Posts
    add_filter( 'manage_edit-post_columns', 'my_columns_filter',10, 1 );
    
    // Custom Post Type
    add_filter( 'manage_edit-CUSTOMPOSTTYPE_columns', 'my_columns_filter',10, 1 );
    
    function my_columns_filter( $columns ) {
       unset($columns['author']);
       unset($columns['categories']);
       unset($columns['tags']);
       unset($columns['comments']);
       
       return $columns;
    }

  2. Skip to note 6 content

    We need to care while using manage_{$screen->id}_columns hook for media screen because when we used manage_media_columns hook we will receive Too few arguments fatal error in Add New Media File page error.

    This error is coming because in the 'manage_media_columns' filter hook in wp-admin/includes/class-wp-media-list-table.php passes two arguments.

    Hook Details : https://developer.wordpress.org/reference/hooks/manage_media_columns/