How to Add a URL Column in the WordPress Media Library for Easy Copy and Paste

Home » Snippets » How to Add a URL Column in the WordPress Media Library for Easy Copy and Paste
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Managing a lot of media files on WordPress can sometimes be a chore, especially if you need to refer to the URL of specific files regularly. Whether you’re linking to these files on your website, sharing them externally, or simply organizing your content, having quick and easy access to your media URLs can save you a significant amount of time. Wouldn’t it be more efficient if there was a way to simply copy and paste these URLs from your media library? If you’re thinking, "Yes, definitely!" then you’re in the right place. In this article, we will show you how to add a URL column to your WordPress media library for easy copy and paste convenience.

					function wpturbo_media_library_add_url_column( $columns ) {
    $columns['media_url'] = 'URL';
    return $columns;
}

add_filter( 'manage_media_columns', 'wpturbo_media_library_add_url_column' );

function wpturbo_media_library_display_url_column( $column_name, $post_id ) {
    if ( 'media_url' === $column_name ) {
        echo esc_url( wp_get_attachment_url( $post_id ) );
    }
}

add_action( 'manage_media_custom_column', 'wpturbo_media_library_display_url_column', 10, 2 );
				

The provided code snippet’s main purpose is to add an extra column with the media URL to the WordPress media library. Users can quickly copy and paste the URLs from this column, making media management more efficient.

At the beginning of the code, a function, wpturbo_media_library_add_url_column( $columns ), is declared. This function takes the $columns array as an argument, which contains all the column names of the media library.

function wpturbo_media_library_add_url_column( $columns ) {
    $columns['media_url'] = 'URL';
    return $columns;
}

In the function body, we add a new key-value pair to the $columns array. The key is ‘media_url‘, which will be used as an identifier for the new column. The value is ‘URL’, which will serve as the column’s name that appears in the media library. The function then returns the modified $columns array.

The next line of the code adds this function to the manage_media_columns filter using the add_filter function. This filter allows for modification of the columns array. By hooking our wpturbo_media_library_add_url_column function to this filter, we modify the $columns array, thereby adding our new column.

add_filter( 'manage_media_columns', 'wpturbo_media_library_add_url_column' );

Following this, another function, wpturbo_media_library_display_url_column( $column_name, $post_id ), is defined. This function is responsible for filling our newly created column with appropriate URLs.

function wpturbo_media_library_display_url_column( $column_name, $post_id ) {
    if ( 'media_url' === $column_name ) {
        echo esc_url( wp_get_attachment_url( $post_id ) );
    }
}

The function receives two arguments: $column_name, which refers to the current column name, and $post_id, specifying the current post. Inside the function, an if statement checks if the $column_name is equal to ‘media_url’, our new column. If this condition is met, WordPress prints the media file URL using the wp_get_attachment_url function, which extracts the URL of the attached media file.

To ensure this function fills the correct column with URLs, it is hooked into the manage_media_custom_column action. This action is triggered when custom columns (like ours) are being rendered. The 10 specifies the priority of our function, and the 2 defines the number of arguments our function accepts.

add_action( 'manage_media_custom_column', 'wpturbo_media_library_display_url_column', 10, 2 );

Our function wpturbo_media_library_display_url_column then adds the URL to the correct post in the ‘URL’ column of the media library.

With these code snippet, the WordPress media library now includes a URL column, making it easy for users to copy and paste their media URLs.

Register an account to save your snippets or go Pro to get more features.