Skip to content

Instantly share code, notes, and snippets.

@GarySwift
Created June 14, 2017 08:05
Show Gist options
  • Save GarySwift/2c37c0360c820560711fae62b9a67416 to your computer and use it in GitHub Desktop.
Save GarySwift/2c37c0360c820560711fae62b9a67416 to your computer and use it in GitHub Desktop.
WordPress: Page Post Screen Columns
<?php
/*
* WordPress: Page Post Screen Columns
*
* To add a custom column for pages, hook the manage_pages_columns filter.
*/
define("SHOW_ICON", true);
if (SHOW_ICON) {
/*
* image icon
*/
$sizes = get_intermediate_image_sizes();
$icon_size = 'icon';
if (!in_array($icon_size, $sizes)) {
add_image_size( 'icon', 30, 30, true );// 30x30 pixels, hard crop mode
}
}
/*
* Custom Columns for page
*
* Manage the columns of the `page` post type
*/
function manage_columns_for_page($columns){
//remove columns
unset($columns['title']);
unset($columns['date']);
unset($columns['comments']);
unset($columns['author']);
$columns['title'] = _x('Page Title', 'column name');
$columns['page-template'] = 'Page Template';
$columns['author'] = __('Author');
$columns['date'] = _x('Date', 'column name');
if (SHOW_ICON) {
$columns['page-featured-image'] = 'Image';
}
return $columns;
}
add_action('manage_page_posts_columns','manage_columns_for_page');
/*
* Populate custom columns for `page` post type
*/
function populate_page_columns($column,$post_id){
//page template column
if($column == 'page-template'){
//get the current page template being used for the page
$page_template_name = get_post_meta( $post_id, '_wp_page_template', true );
//get a listing of all of the page templates for our site
$page_templates = get_page_templates();
if(in_array($page_template_name,$page_templates)){
//search through each template
foreach($page_templates as $key => $value){
//if the template matches our current template, we found it
if($page_template_name == $value){
echo $key . '';
}
}
}else{
echo 'Default';
}
}
if (SHOW_ICON) {
//featured image column
if($column == 'page-featured-image'){
//if this page has a featured image
if(has_post_thumbnail($post_id)){
$page_featured_image = get_the_post_thumbnail($post_id,'icon');
echo $page_featured_image;
}else{
echo '—';
}
}
}
}
add_action('manage_page_posts_custom_column','populate_page_columns',10,2);
/*
* Quick and dirty way to add style to the head
*/
function page_post_screen_columns_css() {
echo '<style type="text/css">';
echo 'th.column-page-featured-image{width: 3em;}';
echo 'td.page-featured-image {vertical-align: middle;text-align: center;width: 3em;}';
echo 'th.column-page-template, td.page-template {width: 160px}';
echo '</style>';
}
add_action('admin_head', 'page_post_screen_columns_css');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment