How to add a URL admin column in WordPress Media Library

Posted on

Wouldn’t it be handy to be able to easily copy a media item URL directly on the main Media Library screen rather than having to open the item?

The following is better formatted version of this code and adds a URL column in the media library. Clicking inside a URL field selects the URL ready for you to copy.

add_filter( 'manage_media_columns', 'sk_media_columns_url' );
/**
 * Filter the Media list table columns to add a URL 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_url( $posts_columns ) {
	$posts_columns['media_url'] = 'URL';
	return $posts_columns;
}

add_action( 'manage_media_custom_column', 'sk_media_custom_column_url' );
/**
 * Display URL custom column in the Media list table.
 *
 * @param string $column_name Name of the custom column.
 */
function sk_media_custom_column_url( $column_name ) {
	if ( 'media_url' !== $column_name ) {
		return;
	}

	echo '<input type="text" width="100%" onclick="jQuery(this).select();" value="' . wp_get_attachment_url() . '" />';
}

add_action( 'admin_print_styles-upload.php', 'sk_url_column_css' );
/**
 * Add custom CSS on Media Library page in WP admin
 */
function sk_url_column_css() {
	echo '<style>
			@media only screen and (min-width: 1400px) {
				.fixed .column-media_url {
					width: 15%;
				}
			}
		</style>';
}

Goes in child theme’s functions.php.