Advertisement
dimkiriaoks

plugin

Jul 12th, 2023
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name:       drk_delete_attachments
  4.  * Description:       This plugin deletes the related attachments when we are deleting a post
  5.  * Version:           1.0
  6.  * Author:            Dimitrios Kyriakos
  7.  * Author URI:        https://dimkiriakos.com
  8.  * License:           MIT License
  9.  * Text Domain:       drk_delete_attachments
  10.  */
  11.  
  12.  
  13. if (! defined('ABSPATH')){
  14.     die("I'm just a plugin not much I can do when called directly.");
  15.     exit;
  16. }
  17.  
  18. class DRK_Delete_Attachments{
  19.     public function __construct()
  20.     {
  21.         add_action('init', [$this, 'Init']);
  22.     }
  23.  
  24.     public function Init(){
  25.         add_action('before_delete_post', [$this, 'on_delete_post']);
  26.     }
  27.    
  28.     public function on_delete_post($post_id){
  29.         $attachments = get_attached_media( '', $post_id );
  30.         foreach ($attachments as $attachment) {
  31.             wp_delete_attachment( $attachment->ID, 'true' );
  32.         }
  33.     }
  34.  
  35. }
  36.  
  37. new DRK_Delete_Attachments();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement