Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active November 3, 2022 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpmudev-sls/07079f66859c4625f00ef8a1707f068c to your computer and use it in GitHub Desktop.
Save wpmudev-sls/07079f66859c4625f00ef8a1707f068c to your computer and use it in GitHub Desktop.
Forminator Pro - Change Upload Path to uploads/form_id
<?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();
@solventweb
Copy link

Great script. However, it stopped working for me sometime after Forminator version 1.15.12. I'd love to see an update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment