Forminator create separate folder for uploads

Can I generate a separate folder for the Forminator upload field inside the uploads folder? Especially I am trying to generate a unique folder for every upload.
Or only 1 separate folder for upload files for all forminator uploads. For example:
/wp-content/uploads/sites/4/**Forminator form folder***/2018/11/IMG_20180721_153052.jpg

  • viobru

    Hi F404!

    Hope you are doing great :slight_smile:

    I've pinged our SLS team to check if they can provide their valuable feedback on this and hopefully suggest a fix to achieve this. Please note that if this is too complex to achieve they might move this to our Feature & Feedback forum or even suggest to hire a developer to assist you achieving this.

    Let me also inform that they work round the clock and they have to deal with lots of critical issues on a daily basis, so it may take a little while for them to check this and give feedback. We highly appreciate your patience on this.

    Kind regards,
    Violeta

  • Konstantinos Xenos

    Hi F404 !

    I've create a mu-plugin for you to achieve the new folder system that you want.

    How to install:
    Always make sure to keep a backup of your site before changing/adding custom code.

    1] Navigate to your /wp-content/ directory and create a new one named mu-plugins if it doesn't exist.
    2] Download the attached .zip and extract it. You will find a file named forminator-change-upload-dir.php
    3] Upload the .php file into the mu-plugins directory.
    4] The final path should look like /wp-content/mu-plugins/forminator-change-upload-dir.php
    5] Everything should work after that automatically.

    The new path should follow this order {form_id}/{year}/{month}/{file}. So for example if I upload a test.jpg from form with ID 10 it would be at wp-content/uploads/10/2018/11/test.jpg.

    Tell me if you need further help customizing this!

    Regards,
    Konstantinos

    Preview of the code in the .zip:

    <?php
    
    /**
     * Plugin Name: Forminator Pro - Change Upload Path
     * Plugin URI: https://premium.wpmudev.org/
     * Description: mu-plugin for changing the Forminator upload dir to /uploads/ID.
     * Version: 1.0.0
     * Author: Konstantinos Xenos @ WPMUDEV
     * Author URI: https://premium.wpmudev.org/
     * License: GPLv2 or later
     */
    
    class Change_Forminator_Upload_Dir {
    
    	/**
    	 * New dir var.
    	 *
    	 * @var $new_dir_id
    	 */
    	private $new_dir_id = 0;
    
    	/**
    	 * Constructor.
    	 */
    	public function __construct() {
    		add_action( 'forminator_custom_form_before_save_entry', array( $this, 'my_form_change_upload_dir' ) );
    		add_action( 'forminator_custom_form_after_save_entry', array( $this, 'my_form_restore_upload_dir' ) );
    	}
    
    	/**
    	 * Hook into Forminator and apply the upload_dir filter.
    	 *
    	 * @param int $form_id The form ID.
    	 */
    	public function my_form_change_upload_dir( $form_id ) {
    		$this->new_dir_id = $form_id;
    		add_filter( 'upload_dir', array( $this, 'my_form_ad_formid_to_upload_dir' ) );
    	}
    
    	/**
    	 * Hook into the upload_dir filter and change the path.
    	 *
    	 * @param array $param The upload dir parameters array.
    	 */
    	public function my_form_ad_formid_to_upload_dir( $param ) {
    		$new_path = '/' . $this->new_dir_id . '/' . date( 'Y' ) . '/' . date( 'm' );
    
    		$param['path'] = $param['basedir'] . $new_path;
    		$param['url']  = $param['baseurl'] . $new_path;
    
    		return $param;
    	}
    
    	/**
    	 * Hook into Forminator and remove the upload_dir filter.
    	 *
    	 * @param int $form_id The form ID.
    	 */
    	public function my_form_restore_upload_dir( $form_id ) {
    		$this->new_dir_id = 0;
    		remove_filter( 'upload_dir', array( $this, 'my_form_ad_formid_to_upload_dir' ) );
    	}
    }
    
    new Change_Forminator_Upload_Dir();
  • F404

    Hello
    Thank you, it works.
    But I have question
    -For with ID 10 it would be at wp-content/uploads/10/2018/11/test.jpg
    -As you now wp creates creates folder named by year 2017, 2018 etc. in wp-content/uploads/
    -If my form id will be Equal to Folder name. For ex. form id 2018 What will happen then ? :slight_smile:

    I think it will better have forminator root folder forminator/{form_id}/{year}/{month}/{file}

  • Konstantinos Xenos

    Hi F404 ,

    You can change the code on line:

    $new_path = '/' . $this->new_dir_id . '/' . date( 'Y' ) . '/' . date( 'm' );

    into:

    $new_path = '/forminator/' . $this->new_dir_id . '/' . date( 'Y' ) . '/' . date( 'm' );

    to have a result like /uploads/forminator/10/2018/11/test.jpg

    Personally I would also name the folders as form_10 instead of just 10, or even better with the actual form name to find it easier as it's hard to remember 'which form is number 10', but I simply made the code with your original suggestion.

    Tell me if you need any further adjustments and I'll be happy to help!

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.