MU Plugin that changes upload path has stopped working
-
Hello,
I had a code that you helped me insert using mu-plugins to change the upload path of uploaded attachments that worked fine up until 2022. You can find the topic here:
https://ww.wp.xz.cn/support/topic/change-dir-of-uploaded-files/
We checked yesterday and saw that all attachments are going to the default uploads folder of WordPress now and we would like -if you can ofcourse- to know what has changed during updates and has stopped working and how can we update the code to work once again.
The code we have now is this one:
<?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( "SELECTAUTO_INCREMENTFROM 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 = '/applications/' . $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();Thank you!
The topic ‘MU Plugin that changes upload path has stopped working’ is closed to new replies.