Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/2aec86a7546fbd5b4606e8a6fff66569 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/2aec86a7546fbd5b4606e8a6fff66569 to your computer and use it in GitHub Desktop.
Forminator - Change Upload Path (uploads/forminator/submission_id)
<?php
/**
* Plugin Name: Forminator - Change Upload Path
* Plugin URI: https://premium.wpmudev.org/
* Description: mu-plugin for changing the Forminator upload dir to /uploads/forminator/submission_id.
* We also had an another git to custom the upload path to wp-content/forminator/form_id/YYYY/MM/DD/submission_id
* which you can check here: https://gist.github.com/wpmudev-sls/639f2b6d711016e74ffa86d3a8e3c0d3
* Version: 1.0.0
* Author: Konstantinos Xenos & Thobui @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
class Change_Forminator_Upload_Dir {
/**
* New dir var.
*
* @var $form_id
*/
private $form_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' ) );
add_filter( 'forminator_custom_form_pseudo_submitted_data', array( $this, 'my_form_change_upload_dir' ), 999, 2 );
add_filter( 'forminator_custom_form_submit_before_set_fields', 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( $pseudo_submitted_data, $custom_form ) {
$this->form_id = $custom_form->id;
add_filter( 'upload_dir', array( $this, 'my_form_ad_formid_to_upload_dir' ) );
return $pseudo_submitted_data;
}
/**
* 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 ) {
global $wpdb;
// $entry_id = $wpdb->get_var( "SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{$wpdb->dbname}' AND TABLE_NAME = '{$wpdb->prefix}frmt_form_entry';" );
$entry_id = 0;
$result = $wpdb->get_row("SHOW CREATE TABLE {$wpdb->prefix}frmt_form_entry");
if( preg_match('/AUTO_INCREMENT=([\d]+)/', $result->{"Create Table"}, $match ) ){
$entry_id = $match[1];
}
if( $entry_id ){
$submitted_data['hidden-1'] = ( $entry_id );
}elseif( ! isset( $submitted_data['hidden-1'] ) ){
$submitted_data['hidden-1'] = 0;
}
$new_path = '/forminator/' . $submitted_data['hidden-1'];
$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( $submit_errors ) {
$this->form_id = 0;
remove_filter( 'upload_dir', array( $this, 'my_form_ad_formid_to_upload_dir' ) );
return $submit_errors;
}
}
new Change_Forminator_Upload_Dir();
@ericlma2
Copy link

ericlma2 commented Nov 3, 2022

This code no longer works after WordPress 6.0 upgrade. Hopefully someone can fix the bug.

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