Add “Image” column to the “Posts” list in WordPress

/**
 * Adds "Image" column (at the second position) to the "Posts" admin page.
 *
 * @see https://wpcodebook.com/add-image-column-posts-list-wordpress/
 * @see https://developer.wordpress.org/reference/hooks/manage_posts_columns/
 * @see https://www.php.net/manual/en/function.array-slice.php
 */
add_filter( 'manage_posts_columns', function ( $post_columns, $post_type ) {
	if ( 'post' !== $post_type ) {
		return $post_columns;
	}
	return array_merge(
		array_slice( $post_columns, 0, 1, true ),
		array( 'image' => esc_html__( 'Image' ) ),
		array_slice( $post_columns, 1, null, true )
	);
}, 10, 2 );

/**
 * Renders the "Image" column.
 *
 * @see https://wpcodebook.com/add-image-column-posts-list-wordpress/
 * @see https://developer.wordpress.org/reference/hooks/manage_posts_custom_column/
 * @see https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/
 */
add_action( 'manage_posts_custom_column', function ( $column_name, $post_id ) {
	if ( 'image' === $column_name ) {
		echo get_the_post_thumbnail( $post_id, array( 50, 50 ) );
	}
}, 10, 2 );

Leave a Comment