This Divi code snippet automatically deletes media attachments uploaded through Divi Form Builder forms after a specified delay.
To use this snippet, first assign a unique Form ID to the form where you want this functionality. Then, add that Form ID to the $form_ids_to_delete_attachments array inside both the df_process_uploaded_files and df_after_process hooks. This Divi code snippet is intended for forms of the Contact Form type. It ensures that uploaded files are removed safely after form submission, helping keep your Media Library clean.
Automatically Delete Media Attachments Uploaded via Divi Form Builder
Details
Code
Description:
1) Add your Form IDs to the $form_ids_to_delete_attachments array inside both the df_process_uploaded_files and df_after_process hooks.
2) Adjust the time in seconds in this line (default is 1200 for 20 minutes): wp_schedule_single_event( time() + 1200, 'dfb_delete_uploaded_files', [ $uploaded_files ] );
Language: PHP
Where to add: functions.php
add_action( 'df_process_uploaded_files', function( $form_id, $uploaded_files, $form_type ) {
$form_ids_to_delete_attachments = [
'form_1',
'form_2',
'form_3',
// Add more form IDs as needed
];
if ( ! in_array( $form_id, $form_ids_to_delete_attachments, true ) ) {
return;
}
if ( session_status() === PHP_SESSION_NONE ) {
session_start();
}
if ( ! empty( $uploaded_files ) && is_array( $uploaded_files ) ) {
$_SESSION['dfb_uploaded_files_' . $form_id] = $uploaded_files;
}
}, 10, 3 );
add_action( 'df_after_process', function( $form_id, $post_array, $form_type ) {
$form_ids_to_delete_attachments = [
'form_1',
'form_2',
'form_3',
// Add more form IDs as needed
];
if ( ! in_array( $form_id, $form_ids_to_delete_attachments, true ) ) {
return;
}
if ( session_status() === PHP_SESSION_NONE ) {
session_start();
}
$key = 'dfb_uploaded_files_' . $form_id;
$uploaded_files = isset( $_SESSION[ $key ] ) ? $_SESSION[ $key ] : [];
if ( empty( $uploaded_files ) || ! is_array( $uploaded_files ) ) {
return;
}
// Schedule the deletion 20 minutes later
wp_schedule_single_event( time() + 1200, 'dfb_delete_uploaded_files', [ $uploaded_files ] );
unset( $_SESSION[ $key ] );
}, 10, 3 );
// Actual file deletion handler
add_action( 'dfb_delete_uploaded_files', function( $uploaded_files ) {
if ( empty( $uploaded_files ) || ! is_array( $uploaded_files ) ) {
return;
}
foreach ( $uploaded_files as $attachment_id ) {
if ( get_post( $attachment_id ) ) {
wp_delete_attachment( $attachment_id, true );
}
}
} );
Related Snippets
Explore more from Divi Engine
Divi Form Builder
From simple contact forms to complex frontend post or product creation, Divi Form Builder has you covered.
Divi Machine
Build complex websites that displays dynamic fields you can filter, search and so much more with the Divi Builder.
Divi BodyCommerce
A versatile toolkit for developers using Divi and WooCommerce together, designed to boost your e-commerce site and achieve greater conversion rates.
Divi Handoff
Let clients update content without touching the Divi Builder. Design layouts once, connect them to Flexible Content fields, and give editors simple forms for text, images, and sections.
Divi Loop Extender
Unlock the Full Power of Divi 5 Loop Builder Add advanced sorting, filtering, and relationship logic right inside the Visual Builder.
Divi Membership
Monetize your Divi websites by transforming them into membership sites with seamless subscription management, user-friendly interfaces, and customizable membership tiers.
Divi Machine Accounts
Build an account area for your customers to edit their details, access wishlist, submitted posts and more. *Note: Requires Divi Machine installed and active
Divi Ajax Filter
Filter WooCommerce, Posts & Custom Posts without reloading the page.
Divi Mobile
Divi Mobile helps you create beautiful looking mobile menus without having to code.
Divi Nitro
Give your Divi website that extra boost of speed with our Divi Nitro plugin to enhance your customer's experience.
Divi Protect
Password protect the content of your Divi website with our Divi Protect plugin. Keep unwanted eyes out!
Divi Mega Menu
Create stunning, responsive, and content-rich mega menus using the Divi Builder you already know. No coding required.