PHPwordpressintermediate
Add Custom Admin Column
Add custom columns to the WordPress admin posts list with sortable functionality
Faisal Yaqoob
January 9, 2025
#admin#custom-column#wordpress#ui#management
Code
php
1 // Add the custom column header 2 function add_featured_image_column($columns) { 3 // Insert after title column 4 $new_columns = array(); 5 foreach ($columns as $key => $value) { 6 $new_columns[$key] = $value; 7 if ($key === 'title') { 8 $new_columns['featured_image'] = __('Featured Image', 'textdomain'); 9 } 10 } 11 return $new_columns; 12 } 13 add_filter('manage_posts_columns', 'add_featured_image_column'); 14 add_filter('manage_pages_columns', 'add_featured_image_column'); 15
16 // Populate the custom column 17 function show_featured_image_column($column_name, $post_id) { 18 if ($column_name === 'featured_image') { 19 $thumbnail = get_the_post_thumbnail($post_id, array(80, 80)); 20 echo $thumbnail ? $thumbnail : '—'; 21 } 22 } 23 add_action('manage_posts_custom_column', 'show_featured_image_column', 10, 2); 24 add_action('manage_pages_custom_column', 'show_featured_image_column', 10, 2); 25
26 // Make the column sortable (for post meta fields) 27 function make_column_sortable($columns) { 28 $columns['featured_image'] = 'featured_image'; 29 return $columns; 30 } 31 add_filter('manage_edit-post_sortable_columns', 'make_column_sortable'); 32
33 // Handle the sorting query 34 function sort_by_column($query) { 35 if (!is_admin() || !$query->is_main_query()) { 36 return; 37 } 38
39 if ($query->get('orderby') === 'featured_image') { 40 $query->set('meta_key', '_thumbnail_id'); 41 $query->set('orderby', 'meta_value_num'); 42 } 43 } 44 add_action('pre_get_posts', 'sort_by_column'); 45
46 // Add CSS to set column width 47 function custom_admin_column_css() { 48 echo '<style> 49 .column-featured_image { 50 width: 100px; 51 text-align: center; 52 } 53 .column-featured_image img { 54 max-width: 80px; 55 height: auto; 56 border-radius: 4px; 57 } 58 </style>'; 59 } 60 add_action('admin_head', 'custom_admin_column_css');
Add Custom Admin Column
This snippet adds a custom column to the WordPress admin posts list. The example shows how to display featured image thumbnails, but you can customize it to display any post meta or custom field.
// Add the custom column header
function add_featured_image_column($columns) {
// Insert after title column
$new_columns = array();
foreach ($columns as $key => $value) {
$new_columns[$key] = $value;
if ($key === 'title') {
$new_columns['featured_image'] = __('Featured Image', 'textdomain');
}
}
return $new_columns;
}
add_filter('manage_posts_columns', 'add_featured_image_column');
add_filter('manage_pages_columns', 'add_featured_image_column');
// Populate the custom column
function show_featured_image_column($column_name, $post_id) {
if ($column_name === 'featured_image') {
$thumbnail = get_the_post_thumbnail($post_id, array(80, 80));
echo $thumbnail ? $thumbnail : '—';
}
}
add_action('manage_posts_custom_column', 'show_featured_image_column', 10, 2);
add_action('manage_pages_custom_column', 'show_featured_image_column', 10, 2);
// Make the column sortable (for post meta fields)
function make_column_sortable($columns) {
$columns['featured_image'] = 'featured_image';
return $columns;
}
add_filter('manage_edit-post_sortable_columns', 'make_column_sortable');
// Handle the sorting query
function sort_by_column($query) {
if (!is_admin() || !$query->is_main_query()) {
return;
}
if ($query->get('orderby') === 'featured_image') {
$query->set('meta_key', '_thumbnail_id');
$query->set('orderby', 'meta_value_num');
}
}
add_action('pre_get_posts', 'sort_by_column');
// Add CSS to set column width
function custom_admin_column_css() {
echo '<style>
.column-featured_image {
width: 100px;
text-align: center;
}
.column-featured_image img {
max-width: 80px;
height: auto;
border-radius: 4px;
}
</style>';
}
add_action('admin_head', 'custom_admin_column_css');
Common Use Cases
Display Post Meta:
function show_custom_field_column($column_name, $post_id) {
if ($column_name === 'price') {
echo get_post_meta($post_id, 'product_price', true) ?: '—';
}
}
Show Taxonomy Terms:
function show_taxonomy_column($column_name, $post_id) {
if ($column_name === 'product_category') {
$terms = get_the_term_list($post_id, 'product_cat', '', ', ', '');
echo $terms ?: '—';
}
}
Display Word Count:
function show_word_count_column($column_name, $post_id) {
if ($column_name === 'word_count') {
$content = get_post_field('post_content', $post_id);
$word_count = str_word_count(strip_tags($content));
echo number_format($word_count);
}
}
Related Snippets
Disable Gutenberg Block Editor
Disable the Gutenberg editor and use the Classic Editor instead
PHPwordpressbeginner
phpPreview
// Method 1: Disable Gutenberg completely
add_filter('use_block_editor_for_post', '__return_false', 10);
// Method 2: Disable for specific post types only
...#gutenberg#classic-editor#wordpress+1
1/9/2025
View
Register Custom Post Type
Complete example of registering a custom post type with all common options
PHPwordpressintermediate
phpPreview
function register_portfolio_post_type() {
$labels = array(
'name' => _x('Portfolio Items', 'Post type general name', 'textdomain'),
'singular_name' => _x('Portfolio Item', 'Post type singular name', 'textdomain'),
...#custom-post-type#wordpress#development+1
1/9/2025
View
Remove WordPress Version Number
Remove WordPress version number from header and RSS feeds for better security
PHPwordpressbeginner
phpPreview
// Remove version from head
remove_action('wp_head', 'wp_generator');
// Remove version from RSS feeds
...#security#wordpress#wp-head+1
1/9/2025
View