Created
February 20, 2015 14:44
-
-
Save raphaelchaib/a685b81addc107d40b5b to your computer and use it in GitHub Desktop.
WordPress: Add new columns to wp-admin posts list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| Add filter and action: Choose one | |
| **/ | |
| // Add to all post types | |
| // add_filter('manage_post_posts_columns', 'cg_new_columns_headers'); | |
| // add_action('manage_post_posts_custom_column', 'cg_new_columns_content', 10, 2); | |
| // Add to specific post type (replace $posttype with the desired post type slug. eg: post, page, movie, etc) | |
| // add_filter('manage_$posttype_posts_columns', 'cg_new_columns_headers'); | |
| // add_action('manage_$posttype_posts_custom_column', 'cg_new_columns_content', 10, 2); | |
| // Add new column header | |
| function cg_new_columns_headers($defaults) { | |
| $count = 0; | |
| // Reorder header | |
| foreach($defaults as $k => $default) { | |
| if($count == 2) { // Order | |
| $new_defaults['the_field'] = 'Header'; | |
| } | |
| $new_defaults[$k] = $default; | |
| $count++; | |
| } | |
| return $new_defaults; | |
| } | |
| // The column content | |
| function cg_new_columns_content($column_name, $post_ID) { | |
| switch ($column_name) : | |
| case 'the_field': | |
| // example of code | |
| $show = get_post_meta($post_ID, 'show_in_slideshow', true); | |
| if($show) echo 'Yes'; | |
| break; | |
| default: | |
| echo ''; | |
| break; | |
| endswitch; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment