$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - Why after a file is programmatically deleted, is there still a reference in the media library?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin11PV0评论

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> ); ?>
Post a comment

comment list (0)

  1. No comments so far