Last active
March 11, 2023 10:06
-
-
Save gmmedia/bd1af134fe5bbb53799d538951f60ca0 to your computer and use it in GitHub Desktop.
Add featured image column to WP admin panel - posts AND pages
This file contains 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 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
Love it! ❤️ Perhaps you can add some syntax highlighting by using a
.php
extension to the filename and/or by adding a<?php
opening tag.