I have added a featured image column to my posts in the WP admin. This all works fine except when using the Quick Edit feature. Upon update, the column disappears, or rather isn’t returned by the ajax save function. The column header remains, but the row being updated no longer has a featured image cell, resulting in a “blank” cell at the end of the row (all the default cells shifts left).
I’m unsure where to look to address this issue and haven’t had any luck finding an answer.
Thanks for any help.
EDIT:
Here is the code to add the custom column. The filter and action are called in the plugin’s __construct
// Set featured image columns
add_filter('manage_edit-post_columns', array($this, 'set_custom_columns'));
add_action( 'manage_post_posts_custom_column', array( $this, 'set_custom_column_data' ), 10, 2 );
/**
* Function to create featured image column
* @param $columns
* @return array
*/
public function set_custom_columns($columns)
{
if ( !is_array( $columns ) ) {
$columns = array();
}
$new_columns = array();
foreach( $columns as $key => $label ) {
if ( $key == 'title' ) { // Put the Thumbnail column before the Title column
$new_columns['featured-image'] = __( 'Image', $this->plugin_slug );
}
$new_columns[$key] = $label;
}
return $new_columns;
}
/**
* Display custom column data
*/
public function set_custom_column_data( $column_name, $post_id ) {
// If featured image column and a featured image exists, display it
if ( 'featured-image' == $column_name ) {
if ( has_post_thumbnail( $post_id ) ) {
echo get_the_post_thumbnail($post_id, 'thumbnail');
}
}
}
1 Answer
I’m using Tom McFarlin’s Plugin Boilerplate for the plugins I write and in the main plugin file, there is a conditional statement to keep things as light as possible in the admin:
if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
...
}
Since the Quick Edit feature uses AJAX, the column wasn’t being recreated on a Quick Edit save. Changing the above line to:
if ( is_admin() ) {
...
}
solved the problem and the custom column appears when saving via Quick Edit.
Hopefully this helps someone who might run into the same type of issue.
Thanks.