• Resolved CRE8

    (@cre8gr)


    Hello,

    I had a code that you helped me insert using mu-plugins to change the upload path of uploaded attachments that worked fine up until 2022. You can find the topic here:

    https://ww.wp.xz.cn/support/topic/change-dir-of-uploaded-files/

    We checked yesterday and saw that all attachments are going to the default uploads folder of WordPress now and we would like -if you can ofcourse- to know what has changed during updates and has stopped working and how can we update the code to work once again.

    The code we have now is this one:

    <?php
    /**
    * Plugin Name: Forminator - Change Upload Path
    * Plugin URI: https://premium.wpmudev.org/
    * Description: mu-plugin for changing the Forminator upload dir to /uploads/forminator/submission_id.
    * We also had an another git to custom the upload path to wp-content/forminator/form_id/YYYY/MM/DD/submission_id
    * which you can check here: https://gist.github.com/wpmudev-sls/639f2b6d711016e74ffa86d3a8e3c0d3
    * Version: 1.0.0
    * Author: Konstantinos Xenos & Thobui @ WPMUDEV
    * Author URI: https://premium.wpmudev.org/
    * License: GPLv2 or later
    */
    
    class Change_Forminator_Upload_Dir {
    
    	/**
    	* New dir var.
    	*
    	* @var $form_id
    	*/
    	private $form_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' ) );
    		add_filter( 'forminator_custom_form_pseudo_submitted_data', array( $this, 'my_form_change_upload_dir' ), 999, 2 );
    		add_filter( 'forminator_custom_form_submit_before_set_fields', 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( $pseudo_submitted_data, $custom_form ) {
    		$this->form_id = $custom_form->id;
    		add_filter( 'upload_dir', array( $this, 'my_form_ad_formid_to_upload_dir' ) );
    		return $pseudo_submitted_data;
    	}
    
    	/**
    	* 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 ) {
    		global $wpdb;
    		// $entry_id = $wpdb->get_var( "SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{$wpdb->dbname}' AND TABLE_NAME = '{$wpdb->prefix}frmt_form_entry';" );
    		$entry_id = 0;
    		$result = $wpdb->get_row("SHOW CREATE TABLE {$wpdb->prefix}frmt_form_entry");
    
    		if( preg_match('/AUTO_INCREMENT=([\d]+)/', $result->{"Create Table"}, $match ) ){
    			$entry_id = $match[1];
    		}
    
    		if( $entry_id ){
    			$submitted_data['hidden-1'] = ( $entry_id );
    		}elseif( ! isset( $submitted_data['hidden-1'] ) ){
    			$submitted_data['hidden-1'] = 0;
    		}
    
    		$new_path = '/applications/' . $submitted_data['hidden-1'];
    		$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( $submit_errors ) {
    		$this->form_id = 0;
    		remove_filter( 'upload_dir', array( $this, 'my_form_ad_formid_to_upload_dir' ) );
    		return $submit_errors;
    	}
    }
    
    new Change_Forminator_Upload_Dir();

    Thank you!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hi @cre8gr

    Hope you are doing fine!

    Thanks for sharing the code. I have performed a few tests on my lab site and was able to reproduce the same issue when uploading the files. We will need to perform some additional tests with the help of our Second Level Support team and will provide an update based on the results.

    Please take into account our Second Level Support team handle more complex issues and their response may take some considerable time.

    Thanks for your patience and understanding on this matter.

    Kind regards

    Luis

    Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @cre8gr

    Here is an updated code that should help for now (you’ll need to configure it the same way as the one you have used so far):

    <?php
    /**
    * Plugin Name: Forminator - Change Upload Path
    * Plugin URI: https://premium.wpmudev.org/
    * Description: mu-plugin for changing the Forminator upload dir to /uploads/forminator/submission_id.
    * We also had an another git to custom the upload path to wp-content/forminator/form_id/YYYY/MM/DD/submission_id
    * which you can check here: https://gist.github.com/wpmudev-sls/639f2b6d711016e74ffa86d3a8e3c0d3
    * Version: 1.0.1
    * Author: Konstantinos Xenos & Thobui & Prashant @ WPMUDEV
    * Author URI: https://premium.wpmudev.org/
    * License: GPLv2 or later
    */
    
    class Change_Forminator_Upload_Dir {
    
    	/**
    	* New dir var.
    	*
    	* @var $form_id
    	*/
    	private $form_id = 0;
    
    	/**
    	* Constructor.
    	*/
    	public function __construct() {
    
    		add_action( 'forminator_form_before_save_entry', array( $this, 'my_form_change_upload_dir' ), 999, 1 );
    		add_action( 'forminator_form_after_save_entry', array( $this, 'my_form_restore_upload_dir' ), 999, 2 );
    	}
    
    	/**
    	* Hook into Forminator and apply the upload_dir filter.
    	*
    	* @param int $form_id The form ID.
    	*/
    	public function my_form_change_upload_dir( $module_id ) {
    		$this->form_id = $module_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 ) {
    		global $wpdb;
    		// $entry_id = $wpdb->get_var( "SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{$wpdb->dbname}' AND TABLE_NAME = '{$wpdb->prefix}frmt_form_entry';" );
    		$entry_id = 0;
    		$result = $wpdb->get_row("SHOW CREATE TABLE {$wpdb->prefix}frmt_form_entry");
    
    		if( preg_match('/AUTO_INCREMENT=([\d]+)/', $result->{"Create Table"}, $match ) ){
    			$entry_id = $match[1];
    		}
    
    		if( $entry_id ){
    			$submitted_data['hidden-1'] = ( $entry_id );
    		}elseif( ! isset( $submitted_data['hidden-1'] ) ){
    			$submitted_data['hidden-1'] = 0;
    		}
    
    		$new_path = '/applications/' . $submitted_data['hidden-1'];
    		$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( $module_id, $response ) {
    		$this->form_id = 0;
    		remove_filter( 'upload_dir', array( $this, 'my_form_ad_formid_to_upload_dir' ) );
    	}
    }
    
    new Change_Forminator_Upload_Dir();

    Please note though:

    As of version 1.23.1 of Formiantor this is expected to stop working as well due to security enhancements implemented in the plugin. There’ll be no “code solution” for this anymore and paths will be different.

    You will, however, be able to change default file storage path directly from the plugin settings on “Formiantor -> Settings -> Data” page. It will not change the names of files and how they are created but the “main path” will be possible to change (though not to dynamic values – only to “fixed” path).

    Kind regards,
    Adam

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @cre8gr

    I hope you are doing well and safe!

    We haven’t heard from you in a while, I’ll mark this thread as resolved.

    Feel free to let us know if you have any additional questions or problems.

    Best Regards
    Patrick Freitas

    Thread Starter CRE8

    (@cre8gr)

    So as of version 1.23.1 onwards we cannot create say a new folder using the id of each submission and save in there all pdf uploaded from each user?

    Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @cre8gr,

    I hope you are doing well today!

    Yes, it is now possible to change file uploads folder from within the plugin (“Forminator -> Settings -> Data” and change “File Upload Storage” option from “Default” to “Custom”) but not possible to alter it via code after 1.23.1 as the paths are now encrypted due to the security.

    https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#file-uploads-storage

    Kind regards,
    Zafer

    Thread Starter CRE8

    (@cre8gr)

    Thank you for the info. One last question. Is it possible in the code you submitted to have another field included in the custom path as-well?

    What I mean is right now it’s /applications/$submitted_data[‘hidden-1’]/, could we add say the value of a radio button named radio-2 for instance and the new path would be /applications/$submitted_data[‘radio-2’]/$submitted_data[‘hidden-1’]/ ?

    Thanks!

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @cre8gr

    It would be possible yes, you can append any data there just make sure to use Value on Form > Settings, but default it uses Label which can cause issues https://monosnap.com/file/ewbvBJlwNeWPDJOwzcJnn8GleHTPKa

    So on value don’t use special characters or spaces.

    Best Regards
    Patrick Freitas

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘MU Plugin that changes upload path has stopped working’ is closed to new replies.