You can automatically delete media when deleting a post by adding a simple code to your website. This is especially useful for member websites where users can upload media. Why it matters: Automatically deleting media when deleting a post can help keep your website organized and prevent unnecessary clutter. This feature is particularly useful for member websites where users can upload media, as it can help prevent the accumulation of unused media files. It can also save time and effort by eliminating the need to manually delete media file The big picture: To enable this feature, you need to add a simple code to your website. However, if you are using Jet Engine or Form Builder for Jet Engine, you will need to edit a plugin first. You can find the code in the video description. If you are using Jet Engine or Form Builder for Jet Engine, you will need to edit a plugin to ensure that media files are attached to posts. How to do it: To enable automatic media deletion, follow these steps: Add the code to your website or edit it in your theme file. 💻 If you are using Jet Engine or Form Builder for Jet Engine, edit the plugin to ensure that media files are attached to posts. 🛠️ When you delete a post, the media files attached to it will also be deleted automatically. 📁🗑️ Remember to test the feature after adding the code to ensure that it works properly. 🧪
THE CODE :
add_action( ‘before_delete_post’, function( $id ) { $attachments = get_attached_media( ”, $id ); foreach ($attachments as $attachment) { wp_delete_attachment( $attachment->ID, ‘true’ ); } } );