Advertisement
wpmupredrag

Forminator - Change Upload Path

Dec 19th, 2018
1,095
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.71 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: Forminator Pro - Change Upload Path
  4.  * Plugin URI: https://premium.wpmudev.org/
  5.  * Description: mu-plugin for changing the Forminator upload dir to /uploads/ID.
  6.  * Version: 1.0.0
  7.  * Author: Konstantinos Xenos @ WPMUDEV
  8.  * Author URI: https://premium.wpmudev.org/
  9.  * License: GPLv2 or later
  10.  */
  11. class Change_Forminator_Upload_Dir {
  12.     /**
  13.      * New dir var.
  14.      *
  15.      * @var $new_dir_id
  16.      */
  17.     private $new_dir_id = 0;
  18.     /**
  19.      * Constructor.
  20.      */
  21.     public function __construct() {
  22.         add_action( 'forminator_custom_form_before_save_entry', array( $this, 'my_form_change_upload_dir' ) );
  23.         add_action( 'forminator_custom_form_after_save_entry', array( $this, 'my_form_restore_upload_dir' ) );
  24.     }
  25.     /**
  26.      * Hook into Forminator and apply the upload_dir filter.
  27.      *
  28.      * @param int $form_id The form ID.
  29.      */
  30.     public function my_form_change_upload_dir( $form_id ) {
  31.         $this->new_dir_id = $form_id;
  32.         add_filter( 'upload_dir', array( $this, 'my_form_ad_formid_to_upload_dir' ) );
  33.     }
  34.     /**
  35.      * Hook into the upload_dir filter and change the path.
  36.      *
  37.      * @param array $param The upload dir parameters array.
  38.      */
  39.     public function my_form_ad_formid_to_upload_dir( $param ) {
  40.         $new_path = '/' . $this->new_dir_id . '/' . date( 'Y' ) . '/' . date( 'm' );
  41.         $param['path'] = $param['basedir'] . $new_path;
  42.         $param['url']  = $param['baseurl'] . $new_path;
  43.         return $param;
  44.     }
  45.     /**
  46.      * Hook into Forminator and remove the upload_dir filter.
  47.      *
  48.      * @param int $form_id The form ID.
  49.      */
  50.     public function my_form_restore_upload_dir( $form_id ) {
  51.         $this->new_dir_id = 0;
  52.         remove_filter( 'upload_dir', array( $this, 'my_form_ad_formid_to_upload_dir' ) );
  53.     }
  54. }
  55. new Change_Forminator_Upload_Dir();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement