Guide on How to Remove Posts with Existing Attachments from the Computer Tab in WordPress

Home » Snippets » Guide on How to Remove Posts with Existing Attachments from the Computer Tab in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Ever found that your WordPress site is cluttered with posts that have unneeded attachments? Over time, these attachments can pile up, leading to a messy website backend and potentially slowing down your site. If you’re looking to have a cleaner user interface and improve your site’s speed, you may want to consider removing the ‘From Computer’ tab for posts with existing attachments. This article will guide you through the steps of how to successfully implement this modification.

					function wpturbo_remove_media_tab($tabs) {
    if ($post_id = (isset($_GET['post_id']) ? $_GET['post_id'] : false)) {
        if(count(get_attached_media('image', $post_id)) > 0) {
            unset($tabs['type']);
        }
    }
    return $tabs;
}
add_filter('media_upload_tabs', 'wpturbo_remove_media_tab');
				

This code snippet begins by defining a function named wpturbo_remove_media_tab(). This function primarily manipulates the media upload tabs displayed in WordPress dashboard while creating or editing posts. Specifically, it aims to remove the tab for uploading media from the computer whenever the current post already has at least one attached media file.

The function takes one parameter, $tabs, a variable that represents the array holding the tab data.

function wpturbo_remove_media_tab($tabs) {

Inside the function body, the code checks whether a post ID can be derived from the browsers’ query string (i.e., the URL parameters). The value is assigned to variable $post_id.

if ($post_id = (isset($_GET['post_id']) ? $_GET['post_id'] : false)) {

The isset($_GET['post_id']) ? $_GET['post_id'] : false block essentially checks if the post id is available in the URL, if it is, it assigns the value to $post_id otherwise it assigns false.

Next, if $post_id is set, the code checks if the current post has any attached media of ‘image’ type. The get_attached_media('image', $post_id) function retrieves all images linked to the current post.

if(count(get_attached_media('image', $post_id)) > 0) {

If the post has any attached media, the unset($tabs['type']); line removes the ‘From Computer’ tab from the media upload screen. This is accomplished by unsetting the ‘type’ key in the $tabs associative array which corresponds to the ‘From Computer’ tab.

After processing and potential adjustments, the manipulated $tabs array is returned.

Finally, the wpturbo_remove_media_tab() function is attached to the media_upload_tabs filter hook. This will call our custom function each time the media upload tabs are generating.

add_filter('media_upload_tabs', 'wpturbo_remove_media_tab');

As a a result, if the post to which new media is being added already has attached media, the ‘From Computer’ tab will not be displayed.

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