Skip to content

Instantly share code, notes, and snippets.

@drikusroor
Forked from gmmedia/functions.php
Last active March 10, 2023 22:13
Show Gist options
  • Save drikusroor/728c625436fb02fe630d9383560293fe to your computer and use it in GitHub Desktop.
Save drikusroor/728c625436fb02fe630d9383560293fe to your computer and use it in GitHub Desktop.
Add featured image column to WP admin panel - posts AND pages
<?php
/**
* Add featured image column to WP admin panel - posts AND pages
* See: https://bloggerpilot.com/featured-image-admin/
*/
// Set thumbnail size
add_image_size( 'j0e_admin-featured-image', 60, 60, false );
// Add the posts and pages columns filter. Same function for both.
add_filter('manage_posts_columns', 'j0e_add_thumbnail_column', 2);
add_filter('manage_pages_columns', 'j0e_add_thumbnail_column', 2);
function j0e_add_thumbnail_column($j0e_columns){
$j0e_columns['j0e_thumb'] = __('Image');
return $j0e_columns;
}
// Add featured image thumbnail to the WP Admin table.
add_action('manage_posts_custom_column', 'j0e_show_thumbnail_column', 5, 2);
add_action('manage_pages_custom_column', 'j0e_show_thumbnail_column', 5, 2);
function j0e_show_thumbnail_column($j0e_columns, $j0e_id){
switch($j0e_columns){
case 'j0e_thumb':
if( function_exists('the_post_thumbnail') )
echo the_post_thumbnail( 'j0e_admin-featured-image' );
break;
}
}
// Move the new column at the first place.
add_filter('manage_posts_columns', 'j0e_column_order');
function j0e_column_order($columns) {
$n_columns = array();
$move = 'j0e_thumb'; // which column to move
$before = 'title'; // move before this column
foreach($columns as $key => $value) {
if ($key==$before){
$n_columns[$move] = $move;
}
$n_columns[$key] = $value;
}
return $n_columns;
}
// Format the column width with CSS
add_action('admin_head', 'j0e_add_admin_styles');
function j0e_add_admin_styles() {
echo '<style>.column-j0e_thumb {width: 60px;}</style>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment