How to add a Time admin column in WordPress Media Library

Posted on

Looking to display the time at which media has been attached/uploaded to your WordPress library along with the date?

Add the following in your child theme’s functions.php:

add_filter( 'manage_media_columns', 'sk_media_columns_time' );
/**
 * Filter the Media list table columns to add a Time column.
 *
 * @param array $posts_columns Existing array of columns displayed in the Media list table.
 * @return array Amended array of columns to be displayed in the Media list table.
 */
function sk_media_columns_time( $posts_columns ) {
	$posts_columns['time'] = _x( 'Time', 'column name' );

	return $posts_columns;
}

add_action( 'manage_media_custom_column', 'sk_media_custom_column_time' );
/**
 * Display attachment uploaded time under `Time` custom column in the Media list table.
 *
 * @param string $column_name Name of the custom column.
 */
function sk_media_custom_column_time( $column_name ) {
	if ( 'time' !== $column_name ) {
		return;
	}

	the_time( 'g:i:s a' ); // in hh:mm:ss am/pm format.

}

add_action( 'admin_print_styles-upload.php', 'sk_time_column_css' );
/**
 * Add custom CSS on Media Library page in WP admin
 */
function sk_time_column_css() {
	echo '<style>
			.fixed .column-time {
				width: 10%;
			}
		</style>';
}

The time shown will be in the timezone set in WordPress settings. The default is UTC+0.

If you would like to display the time in the format set in WordPress, replace the_time( 'g:i:s a' ) with the_time().

Additionally, to make the Time column sortable add:

add_filter( 'manage_upload_sortable_columns', 'sk_sortable_time_column' );
/**
 * Register Time column as sortable in the Media list table.
 *
 * @param array $columns Existing array of columns.
 */
function sk_sortable_time_column( $columns ) {
	$columns['time'] = 'time';

	return $columns;
}