Remove an image that was not put in uploads folder via wp_handle_upload

前端 未结 3 1153
长情又很酷
长情又很酷 2021-02-20 05:48

I\'m saving an image to uploads folder, but I\'m using file_put_contents instead of wp_handle_upload - because I get an image in base64 and not as a file in $_FILES.

Im

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-20 06:01

    The simple solution is probably to just manually add _wp_attached_file meta value to your attachment.

    Set it to the file you want deleted when the attachment is deleted.

    The logic behind this is according to https://core.trac.wordpress.org/browser/tags/4.4/src/wp-includes/post.php#L0:

    get_attached_file returns that value to wp_delete_attachment which sets it as $file

    And at the end of wp_delete_attachment it calls wp_delete_file($file)

    So I'm guessing the underlying issue is that, you are never setting a value for the meta key _wp_attached_file.

    The only downside is that I'm not sure what other processes in wp might be using that value. But you might get lucky, perhaps none, and/or it doesn't mess anything up by setting it manually.

提交回复
热议问题