milenbogoev

Auto Delete Images on Post Delete

May 3rd, 2023
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.48 KB | Source Code | 0 0
  1. function auto_delete_images_on_post_delete( $post_id ) {
  2.     // Get the post object
  3.     $post = get_post( $post_id );
  4.  
  5.     // Get any attached images
  6.     $images = get_attached_media( 'image', $post_id );
  7.  
  8.     // Loop through the images and delete them
  9.     foreach ( $images as $image ) {
  10.         wp_delete_attachment( $image->ID, true );
  11.     }
  12. }
  13.  
  14. // Hook the function to the 'before_delete_post' action
  15. add_action( 'before_delete_post', 'auto_delete_images_on_post_delete' );
Advertisement
Add Comment
Please, Sign In to add comment