Forked from wpmudev-sls/forminator-change-upload-dir.php
Created
June 26, 2021 17:38
-
-
Save patrickfreitasdev/68b3a63ec34f85b3e5bfdb5b896b8912 to your computer and use it in GitHub Desktop.
Forminator Pro - Change Upload Path to uploads/form_id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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_form_before_save_entry', array( $this, 'my_form_change_upload_dir' ) ); | |
add_action( 'forminator_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 = '/new-upload-folder/'; | |
$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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment