plugin development - Why after a file is programmatically deleted, is there still a reference in the media library?

admin2025-06-05  3

I work on a plugin that gets a file uploaded to the upload folder through media. The file is processed if it does not meet x condition the process is aborted and the file is deleted.

The deletion is effective when invoking unlink in this way

if ($counter === 0) {
    printMessage('No need to process');

    if (unlink($file)) {
        printMessage('File removed');
    }

    return;
}

Here $file is a reference to wp_handle_upload $upload['file']

My case is that after verifying that the file has indeed been deleted from the file system, there is a reference to it in the media library.

I appreciate your comments

I work on a plugin that gets a file uploaded to the upload folder through media. The file is processed if it does not meet x condition the process is aborted and the file is deleted.

The deletion is effective when invoking unlink in this way

if ($counter === 0) {
    printMessage('No need to process');

    if (unlink($file)) {
        printMessage('File removed');
    }

    return;
}

Here $file is a reference to wp_handle_upload $upload['file']

My case is that after verifying that the file has indeed been deleted from the file system, there is a reference to it in the media library.

I appreciate your comments

Share Improve this question asked Dec 10, 2018 at 23:17 user615274user615274 1237 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

When you upload a file to Media Library, some data (mainly metadata like title, description, and so on) is stored in database.

So if you delete file using unlink, you don't delete any rows from DB - so the attachment will be still visible in Media Library.

If you want to delete attachment from ML, you should use WP functions. wp_delete_attachment might come in handy.

You can use it like so:

<?php wp_delete_attachment( <ID_OF_YOUR_ATTACHMENT> ); ?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749110279a316446.html

最新回复(0)