Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Store uploaded files in separated folders [uploads/forminator/member_name]
<?php
/**
* Plugin Name: [Forminator] - Store uploaded files in separated folders
* Description: [Forminator] - Store uploaded files in separated folders [uploads/forminator/member_name] - 1132168970814141
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org/profile/tho2757
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'plugins_loaded', 'wpmudev_forminator_custom_upload_files_path_func', 100 );
function wpmudev_forminator_custom_upload_files_path_func() {
if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) {
class WPMUDEV_Forminator_Custom_Upload_Path{
private static $_instance;
private $folder_field_key;
private $hash_folder;
private $sub_folder = 'forminator'; //it's sub folder
public function __construct(){
$this->folder_field_key = 'name-1'; //enter field id here. The name field maybe not unique, so if you want to use unique folder for each member, we suggest use email field instead of.
$this->hash_folder = true; // if enabled it, the folder will be hash with method crc32 (the result like this: 3d653119 ), source: https://www.php.net/manual/en/function.hash.php
add_action( 'forminator_custom_form_pseudo_submitted_data', array( $this, 'add_anchor_change_upload_dir_byname' ) );
add_filter( 'upload_dir', array( $this, 'upload_dir' ) );
add_action( 'forminator_custom_form_submit_errors', array( $this, 'remove_anchor_change_upload_dir_byname' ) );
}
public static function get_instance(){
if( is_null( self::$_instance ) ){
self::$_instance = new self();
}
return self::$_instance;
}
public function get_folder_path( $user_name ){
$uploads = wp_get_upload_dir();
$user_name = sanitize_key( $user_name );
if( $this->hash_folder ){
$user_name = hash('crc32', $user_name);
}
$folder_path = trailingslashit( $uploads['basedir'] );
if( $this->sub_folder ){
$folder_path .= $this->sub_folder .'/';
}
return $folder_path;
}
/**
* get real file path by name or email
* @param string $file_name
* @param string $user_name User name or Email
* @return string file path
*/
public function get_file_path( $file_name, $user_name ){
$folder_path = $this->get_folder_path( $user_name );
// if( @file_exists( $file_path ) )
return $folder_path .'/'. $file_name;
}
function add_anchor_change_upload_dir_byname(){
global $wpmudev_forminator_upload_dir_byname;
$wpmudev_forminator_upload_dir_byname = 1;
}
function upload_dir( $uploads ){
global $wpmudev_forminator_upload_dir_byname;
if( $wpmudev_forminator_upload_dir_byname && ! empty( $uploads['path'] ) && ! empty( $_POST[ $this->folder_field_key ] ) ){
$folder = sanitize_key( $_POST[ $this->folder_field_key ] );
if( $this->hash_folder ){
$folder = hash('crc32', $folder);
}
$uploads['path'] = trailingslashit( $uploads['basedir'] ) ;
if( $this->sub_folder ){
$uploads['path'] .= $this->sub_folder .'/';
}
$uploads['path'] .= $folder;
}
return $uploads;
}
function remove_anchor_change_upload_dir_byname(){
global $wpmudev_forminator_upload_dir_byname;
if( $wpmudev_forminator_upload_dir_byname ){
unset( $GLOBALS['wpmudev_forminator_upload_dir_byname'] );
}
}
}
$run = WPMUDEV_Forminator_Custom_Upload_Path::get_instance();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.