Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active September 18, 2024 05:48
Show Gist options
  • Save wpmudev-sls/b24ed7f4f24c8b63ae1348f2ad5effe0 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/b24ed7f4f24c8b63ae1348f2ad5effe0 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Rename the uploaded file name based on a text field
<?php
/**
* Plugin Name: [Forminator Pro] Rename the uploaded file name.
* Description: Rename the file uploaded based on the text field in a multiupload field.
* Author: Prashant @ WPMUDEV
* Task: SLS-6496
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
function wpmudev_modify_uploaded_file_name( $filename ) {
$prefix = Forminator_CForm_Front_Action::$prepared_data['text-1'];
$filename = ! empty( $prefix ) ? $prefix . '-' . $filename : $filename;
return $filename;
}
add_action( 'forminator_form_before_handle_submit', 'wpmudev_uploaded_filename_fix', 10, 1 );
add_action( 'forminator_form_before_save_entry', 'wpmudev_uploaded_filename_fix', 10, 1 );
function wpmudev_uploaded_filename_fix( $form_id ) {
if ( 2960 !== intval( $form_id ) ) { // Please change the form ID.
return;
}
add_filter( 'wp_unique_filename', 'wpmudev_modify_uploaded_file_name', 10, 1 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment