2022年03月29日

Filterフック:manage_posts_columns

フィルタフックの manage_posts_columns は、管理画面の「投稿一覧」を表示する時にフックされるフィルターフックです。

このフィルタフックを利用して、投稿一覧に表示するフィールドを追加する事ができます。

 

全体像は下記ドキュメントを参照して下さい。

管理画面のカスタマイズの基本知識

 

1.利用サンプル

下記は、「投稿一覧」に[スラッグ]と[更新日]フィールドを追加した事例です。

function posts_design($columns) {
	$columns = array(
		'cb'					=> '<input type="checkbox" />',
		'title'				=> 'タイトル', 
		'slug'				=> 'スラッグ', //新設
		'categories'	=> 'カテゴリ',  
		'tags'				=> 'タグ',        
		'date'				=> '日時',
		'modified'		=> '更新日', //新設  
		'author'			=> '作成者',
		'comments'		=> '<div class="comment-grey-bubble" title="コメント"></div>',);
	echo
  	'<style>
  		.fixed .column-title {width:30%;}
  		.fixed .column-slug {width:20%;}
  		.fixed .column-categories {width:13%;}
  		.fixed .column-tags {width:13%;}
  		.fixed .column-date {width:10%;}
  		.fixed .column-modified {width:10%;}
  		.fixed .column-comments {width:4%;}
  	</style>'; 	
	return $columns;
}
add_filter('manage_posts_columns', 'posts_design',10,1);

■1行目:$columns

フィルタフック「manage_posts_columns」は関数に表示カラム一覧を引数として渡します。

ユーザはこれを編集して返すことにより、表示するカラムを編集する事ができます。

■24行目

優先順位は10、貰う引数は1つを明示しています。

■5行目、9行目

新規に追加する「スラッグ」と「更新日」フィールドです。

ここにデータを追加するのはActionフック:manage_posts_custom_columnです。

■12~21行目

各表示カラムの幅を%で割り振っています。

 

フィルターフック&関数
  • header_video_settings
  • manage_pages_columns
  • manage_posts_columns
  • mce_buttons
  • mce_external_plugins
  • redirect_canonical
  • the_content
  • tiny_mce_before_init
  • wp_insert_post_data
  • apply_filters()