Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @paplo,

    The fields are only saved to the DB after the form is submitted, so I’m not sure how well it would be possible to rename the directory during the submission due to that.

    I’m checking with our developer to see if this is possible and if there is any workaround that could be looked into regarding this.

    Will keep you updated once I get further feedback asap.

    Kind Regards,
    Nithin

    Plugin Support Dimitris – WPMU DEV Support

    (@wpmudev-support6)

    Hello there @paplo

    Please use the following snippet in a new MU plugin file (https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins):

    <?php
    function wpmudev_modify_uploaded_file_names( $filename, $filename_raw ) {
        $info = pathinfo($filename);
        $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
        $name = basename($filename, $ext);
    	if ( !empty( $_POST['name-1'] ) ) {
    		$user_name = sanitize_text_field( $_POST['name-1'] );
    		$filename = $user_name . $ext;
    		$filename = str_replace( ' ', '_',  $filename );
    	}
        return $filename;
    }
    
    add_action( 'forminator_form_before_save_entry', 'wpmudev_uploaded_filename_check', 10, 1 );
    function wpmudev_uploaded_filename_check( $form_id ) {
    	if( $form_id == 2910 ) {
    		add_filter('sanitize_file_name', 'wpmudev_modify_uploaded_file_names', 10, 2);
    	}
    }

    Keep in mind that you will have to change the form ID in the code from 2910 to your form’s ID (the same integer number found in the form’s shortcode).

    Let us know if further assistance is needed.

    Thank you,
    Dimitris

    Thread Starter paplo

    (@paplo)

    Hi Dimitris

    Amazing, it works! Thanks! However I have altered my name field to consist of both fistname and lastname, like: name-1-first-name, name-1-last-name.

    If i rename “name-1” in your code to name-1-first-name, the picture gets renamed correctly. However if you can add the name-1-last-name for me as well, would be great!

    Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @paplo,

    I hope you are doing well today!

    You can try the following code for that;

    <?php
    function wpmudev_modify_uploaded_file_names( $filename, $filename_raw ) {
        $info = pathinfo($filename);
        $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
        $name = basename($filename, $ext);
    	if ( !empty( $_POST['name-1-first-name'] ) && !empty( $_POST['name-1-last-name'] ) ) {
    		$user_first_name = sanitize_text_field( $_POST['name-1-first-name'] );
    		$user_last_name = sanitize_text_field( $_POST['name-1-last-name'] );
    		$filename = $user_first_name . '_' . $user_last_name . $ext;
    		$filename = str_replace( ' ', '_',  $filename );
    	}
        return $filename;
    }
    
    add_action( 'forminator_form_before_save_entry', 'wpmudev_uploaded_filename_check', 10, 1 );
    function wpmudev_uploaded_filename_check( $form_id ) {
    	if( $form_id == 2910 ) {
    		add_filter('sanitize_file_name', 'wpmudev_modify_uploaded_file_names', 10, 2);
    	}
    }

    Please do not forget to change $form_id variable with your form’s ID and test this code on your dev/staging site first.

    Kind regards,
    Zafer

    Thread Starter paplo

    (@paplo)

    Hi Zafer

    Works flawlessly! Thanks so much!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Rename uploaded files after form field’ is closed to new replies.