Deleting the entries do not delete the files

I get entries with files in Forminator form. The issue is when I try to delete the entries the files do not get deleted.

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hi F404 ,

    I’ve created another mu-plugin for you to achieve this.

    To install follow the same simple process as with the other one that we’ve recently tackled for changing the upload folder.

    1] Create a backup ( especially now that we’re dealing with file deletion ).

    2] Extract the attached .zip to find the .php file.

    3] Upload the .php file into the wp-content/mu-plugins directory.

    Regards,

    Konstantinos

    Code preview:

    <?php

    /**
    * Plugin Name: Forminator - Delete uploaded files after submission removal.
    * Plugin URI: https://premium.wpmudev.org/
    * Description: mu-plugin for deleting form attached files after a submission is deleted.
    * Version: 1.0.0
    * Author: Konstantinos Xenos @ WPMUDEV
    * Author URI: https://premium.wpmudev.org/
    * License: GPLv2 or later
    */

    if ( ! defined( 'ABSPATH' ) ) {
    exit;
    }

    if ( ! class_exists( 'WPMUDEV_Forminator_Delete_Uploaded_Files' ) ) {

    /**
    * WPMUDEV_Forminator_Delete_Uploaded_Files Class
    */
    class WPMUDEV_Forminator_Delete_Uploaded_Files {

    /**
    * Constructor.
    */
    public function __construct() {
    add_action( 'forminator_before_delete_entries', array( $this, 'remove_files_entries' ), 15, 2 );
    }

    /**
    * Removes the uploaded files from given entries.
    *
    * @param int $form_id The Form ID.
    * @param string $entries Comma separated list of entries.
    *
    * @return void
    */
    public function remove_files_entries( $form_id, $entries ) {
    $entries = explode( ',', $entries );

    foreach ( $entries as $entry_id ) {
    $entry = Forminator_API::get_entry( $form_id, $entry_id );

    foreach ( $entry->meta_data as $meta ) {
    if ( isset( $meta['value']['file']['file_path'] ) ) {
    wp_delete_file( $meta['value']['file']['file_path'] );
    }
    }
    }
    }
    }

    new WPMUDEV_Forminator_Delete_Uploaded_Files();
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.