Buil in functions not deleting file of attachments
-
I am trying to regenerate a PDF file whenever a post is being saved/updated. The PDF file name changes according to post_meta data, therefore I want to delete the existing PDF attachment AND the file from the server when a post is saved/updated, before the pdf gets regenerated and attached.
wp_delete_attachment() deletes the attachment alright, but the file stays on the server even when forced to delete.
I also tried wp_delete_file_from_directory( $file, $path); It returns true for having deleted the file, but the file stays on the server. Same for wp_delete_file();
The only thing that seemed to be working was unlink(), but that creates another problem, because in case the file name doesn’t change, unlink() seems to put a stop to creating the file with the same name.
wp_update_post( $my_post ); if(get_post_status( $post_id ) == "publish"): $existing_PDFs = get_attached_media('application/pdf', $post_id); foreach($existing_PDFs as $pdf): $file = get_attached_file($pdf->ID, true); $path = pathinfo($file, PATHINFO_DIRNAME); wp_delete_file_from_directory( $file, $path); wp_delete_file( $file ); wp_delete_attachment($pdf->ID, true); endforeach; include('generate-single-machine-pdf.php'); endif;
What’s the secret to get WordPress to delete a file from the server along with the attachment?
- The topic ‘Buil in functions not deleting file of attachments’ is closed to new replies.