Last active
November 2, 2016 18:53
-
-
Save finallyRaunak/b17f5e45829e3c2dab49cd08e483c253 to your computer and use it in GitHub Desktop.
WordPress delete media on post/page/product deletion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function delete_associated_media($id) { | |
$media = get_children(array( | |
'post_type' => 'attachment', | |
'post_parent' => $id, | |
'post_status' => 'inherit', | |
//'post_mime_type' => 'image/jpeg', | |
'numberposts' => -1 | |
)); | |
if (empty($media)) return; | |
foreach ($media as $file) { | |
//deletes an attachment and all of its derivatives. | |
if ( false === wp_delete_attachment($file->ID, TRUE) ) { //force deletion (so no trash) | |
// Log failure to delete attachment. | |
} | |
} | |
} | |
add_action('before_delete_post', 'delete_associated_media'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment