* @license GPL-2.0+
* @link http://robincornett.com
* @copyright 2015 Robin Cornett Creative, LLC
*/
class AddFeaturedImageColumn {
public function run() {
add_action( 'admin_init', array( $this, 'add_post_type_column' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'featured_image_column_width' ) );
}
public function add_post_type_column() {
if ( class_exists( 'Display_Featured_Image_Genesis' ) ) {
return;
}
$args = array(
'public' => true,
'_builtin' => false,
);
$output = 'names';
$post_types = get_post_types( $args, $output );
$post_types['post'] = 'post';
$post_types['page'] = 'page';
foreach ( $post_types as $post_type ) {
if ( ! post_type_supports( $post_type, 'thumbnail' ) ) {
continue;
}
add_filter( "manage_edit-{$post_type}_columns", array( $this, 'add_featured_image_column' ) );
add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'manage_image_column' ), 10, 2 );
}
}
/**
* add featured image column
* @param column $columns set up new column to show featured image for taxonomies/posts/etc.
*
* @since 0.9.0
*/
public function add_featured_image_column( $columns ) {
$new_columns = $columns;
array_splice( $new_columns, 1 );
$new_columns['featured_image'] = __( 'Featured Image', 'add-featured-image-column' );
return array_merge( $new_columns, $columns );
}
/**
* manage new post_type column
* @param column id $column column id is featured_image
* @param post id $post_id id of each post
* @return featured image display featured image, if it exists, for each post
*
* @since 0.9.0
*/
public function manage_image_column( $column, $post_id ) {
if ( 'featured_image' !== $column ) {
return;
}
$image_id = get_post_thumbnail_id( $post_id );
if ( ! $image_id ) {
return;
}
$args = array(
'image_id' => $image_id,
'context' => 'post',
'alt' => the_title_attribute( 'echo=0' ),
);
echo wp_kses_post( $this->admin_column_image( $args ) );
}
/**
* Generic function to return featured image
* @param array $args array of values to pass to function ( image_id, context, alt_tag )
* @return string image html
*
* @since 0.9.0
*/
protected function admin_column_image( $args ) {
$image_id = $args['image_id'];
$preview = wp_get_attachment_image_src( $image_id, 'thumbnail' );
$preview = apply_filters( 'addfeaturedimagecolumn_thumbnail', $preview, $image_id );
if ( ! $preview ) {
return;
}
return sprintf( '
', $preview[0], $args['alt'] );
}
/**
* sets a width for the featured image column
* @return stylesheet inline stylesheet to set featured image column width
*/
public function featured_image_column_width() {
$screen = get_current_screen();
if ( ! post_type_supports( $screen->post_type, 'thumbnail' ) ) {
return;
}
if ( in_array( $screen->base, array( 'edit' ) ) ) { ?>