Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement