12
Sep
How to add your custom column on post or page list page in wordpress
- Category:
- Wordpress
Posted On : September 12, 2013
| No Comment
Want to add your plugin custom column to post or page list page ?
here is the code for add column to post listing table
View Code PHP
1 2 3 4 5 6 7 | <?php function custom_post_table($column) { $column['my_column'] = 'My Custom Column'; return $column; } add_filter('manage_posts_columns', 'custom_post_table'); ?> |
we are using a filter “manage_posts_columns” with having one argument “$column”. “$column” passes the all column name of post table as an array. just we add our custom column in that array and return it.
Now we need to add value to custom column that we created above
here is code that add value to custom column
View Code PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php function custom_post_table_row($column_name, $post_id) { $mycustom_field_value = get_post_meta($post_id, "my_custom_field_meta", true); switch ($column_name) { case 'my_column' : echo $mycustom_field_value; break; } } add_filter('manage_posts_custom_column', 'custom_post_table_row', 10, 2); ?> |
we are using ‘manage_posts_custom_column’ with having two arguments. One is the column name and another is the post ID.
View Code PHP
1 | <?php $mycustom_field_value = get_post_meta($post_id, "my_custom_field_meta", true); ?> |
Above code is fetching custom field value.
For pages, those filter will be “manage_pages_columns” and “manage_pages_custom_column”.
- Tags: