Add Featured Image column to Posts admin page - Easily see whether a post has a featured image set. https://www.damiencarbery.com/2022/11/add-featured-image-column-to-posts-admin-page/
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 | |
/* | |
Plugin Name: Add Featured Image column to Posts admin page | |
Plugin URI: https://www.damiencarbery.com/2022/11/add-featured-image-column-to-posts-admin-page/ | |
Description: Easily see whether a post has a featured image set. | |
Author: Damien Carbery | |
Version: 0.1 | |
*/ | |
class AddFeaturedImageColumn { | |
// Column key and title. | |
private $column_key; | |
private $column_title; | |
// Name of column to add the new column after. | |
private $insert_after; | |
// Returns an instance of this class. | |
public static function get_instance() { | |
if ( null == self::$instance ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
// Initialize the plugin variables. | |
public function __construct() { | |
$this->column_key = 'featured_image'; | |
$this->column_title = 'Featured <br/>image'; | |
// Comment this line out to add column to the end. | |
$this->insert_after = 'tags'; | |
$this->init(); | |
} | |
// Set up WordPress specfic actions. | |
public function init() { | |
// Add the new column. Could also use 'manage_post_posts_columns' | |
add_filter( 'manage_posts_columns', array( $this, 'set_custom_column' ), 20 ); | |
// Populate the new column with the featured image status. Could also use 'manage_post_posts_custom_column' | |
add_action( 'manage_posts_custom_column' , array( $this, 'populate_custom_column' ), 10, 2 ); | |
// Add CSS to ensure new column width is not distored. | |
add_action( 'admin_head', array( $this, 'add_column_css' ) ); | |
} | |
// Add the new column. | |
public function set_custom_column( $columns ) { | |
// If $this->insert_after is set, and found, add the new column after it. | |
if ( isset( $this->insert_after ) ) { | |
$position = array_search( $this->insert_after, array_keys( $columns ) ); | |
if ( false !== $position ) { | |
$before = $columns; | |
$after = $columns; | |
array_splice( $before, $position + 1 ); | |
array_splice( $after, 0, $position + 1 ); | |
$before[ $this->column_key ] = $this->column_title; // To avoid wrapping. Add space for 'Screen Options' panel. | |
$columns = array_merge( $before, $after ); | |
} | |
} | |
else { | |
// Otherwise add the new column at the end. | |
$columns[ $this->column_key ] = $this->column_title; | |
} | |
return $columns; | |
} | |
// Populate the new column with the featured image status. | |
public function populate_custom_column( $column, $post_id ) { | |
// Use switch() to allow for additional columns in the future. | |
switch ( $column ) { | |
case 'featured_image': | |
if ( has_post_thumbnail( $post_id ) ) { | |
echo '<span class="dashicons dashicons-yes"></span>'; | |
} | |
else { | |
echo '<span aria-hidden="true">—</span><span class="screen-reader-text">No featured image</span>'; | |
} | |
break; | |
} | |
} | |
// Add CSS to ensure new column width is not distored. | |
public function add_column_css() { | |
$currentScreen = get_current_screen(); | |
if ( isset( $currentScreen ) ) { | |
//error_log( 'get_current_screen: ' . var_export( $currentScreen, true ) ); | |
if ( 'edit-post' == $currentScreen->id ) { | |
?> | |
<style>table.wp-list-table .column-featured_image { width: 15%; }</style> | |
<?php | |
} | |
} | |
} | |
} | |
$AddFeaturedImageColumn = new AddFeaturedImageColumn(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment