Last updated on September 14, 2023

Modify the WordPress Media Library Tab

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Customize media tab for efficient use.

The WordPress Media Library is a powerful tool for managing and organizing your media files. By default, it has four tabs: “Library”, “Upload Files”, “Media”, and “Create Gallery”. However, you can modify the Media Library tab and customize it to fit your specific needs.

To modify the WordPress Media Library tab, you can use the media_upload_tabs filter hook. This hook allows you to add, remove, or modify the existing tabs in the Media Library.

Here’s an example code snippet that demonstrates how to modify the Media Library tab by adding a new tab called “My Tab”:

function wpsnippets_modify_media_library_tab($tabs) {
    // Add a new tab
    $tabs['my_tab'] = __('My Tab', 'text-domain');

    return $tabs;
}
add_filter('media_upload_tabs', 'wpsnippets_modify_media_library_tab');

In the code above, we define a custom function wpsnippets_modify_media_library_tab that takes the $tabs array as a parameter. We then add a new tab called “My Tab” to the array using the key 'my_tab' and the translated label __('My Tab', 'text-domain'). Finally, we return the modified $tabs array.

You can customize the label and key of the new tab according to your requirements. Remember to replace 'text-domain' with your actual theme or plugin’s text domain.

This code snippet can be useful when you want to add a custom tab to the WordPress Media Library, allowing you to extend its functionality and provide additional features for managing your media files.

Examples

Example 1: Add a Custom Column to the Media Library Tab

This example demonstrates how to add a custom column to the Media Library tab in WordPress. The code snippet below adds a column called “Downloads” to display the number of times a media file has been downloaded.

function wpsnippets_add_custom_column( $columns ) {
    $columns['downloads'] = 'Downloads';
    return $columns;
}
add_filter( 'manage_media_columns', 'wpsnippets_add_custom_column' );

function wpsnippets_display_custom_column( $column_name, $attachment_id ) {
    if ( 'downloads' === $column_name ) {
        $downloads = get_post_meta( $attachment_id, 'downloads', true );
        echo $downloads ? $downloads : '0';
    }
}
add_action( 'manage_media_custom_column', 'wpsnippets_display_custom_column', 10, 2 );

The wpsnippets_add_custom_column function adds a new column called “Downloads” to the Media Library tab by modifying the $columns array. The wpsnippets_display_custom_column function displays the value of the “Downloads” column for each media file by retrieving the value from the downloads custom field using get_post_meta.

Example 2: Remove an Existing Column from the Media Library Tab

This example demonstrates how to remove an existing column from the Media Library tab in WordPress. The code snippet below removes the “Author” column from the Media Library.

function wpsnippets_remove_column( $columns ) {
    unset( $columns['author'] );
    return $columns;
}
add_filter( 'manage_media_columns', 'wpsnippets_remove_column' );

The wpsnippets_remove_column function removes the “Author” column from the Media Library tab by unsetting the corresponding key in the $columns array.

Example 3: Change the Order of Columns in the Media Library Tab

This example demonstrates how to change the order of columns in the Media Library tab in WordPress. The code snippet below moves the “Date” column to the first position.

function wpsnippets_change_column_order( $columns ) {
    $date_column = $columns['date'];
    unset( $columns['date'] );
    $columns = array_merge( array( 'date' => $date_column ), $columns );
    return $columns;
}
add_filter( 'manage_media_columns', 'wpsnippets_change_column_order' );

The wpsnippets_change_column_order function changes the order of columns in the Media Library tab by moving the “Date” column to the first position. It does this by storing the value of the “Date” column, unsetting it from the $columns array, and then merging it back into the array at the beginning.

Last updated on September 14, 2023. Originally posted on September 18, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *