Skip to content

Instantly share code, notes, and snippets.

@Qubadi
Created July 8, 2025 23:48
Show Gist options
  • Save Qubadi/bc59de1f52ab96f8f4aad2e59b42d647 to your computer and use it in GitHub Desktop.
Save Qubadi/bc59de1f52ab96f8f4aad2e59b42d647 to your computer and use it in GitHub Desktop.
JetFormBuilder custom upload directory
Don’t forget to add your folder name here:
define( 'JFB_CUSTOM_UPLOAD_DIR_NAME', 'ADD_YOUR_FOLDER_NAME_HERE' );
Find this line of code and add your folder name there.
Copy the following PHP and create a PHP snippet using your snippet plugins.
Paste the code into the plugin and save it.
_______________________________________________________________
/**
* JetFormBuilder custom upload directory
* Securely redirects JetFormBuilder file uploads to a custom folder
*/
// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Change this to rename your upload folder.
* Folder will be created (if needed) inside wp-content/uploads/
*/
if ( ! defined( 'JFB_CUSTOM_UPLOAD_DIR_NAME' ) ) {
define( 'JFB_CUSTOM_UPLOAD_DIR_NAME', 'ADD_YOUR_FOLDER_NAME_HERE' );
}
// Main class to handle JetFormBuilder uploads
if ( ! class_exists( 'JFB_Custom_Upload_Handler' ) ) {
class JFB_Custom_Upload_Handler {
private $custom_folder = JFB_CUSTOM_UPLOAD_DIR_NAME;
private $is_jfb_upload = false;
public function __construct() {
$this->init_hooks();
}
private function init_hooks() {
// Hook into JetFormBuilder request processing
add_action( 'wp_loaded', array( $this, 'setup_upload_filter' ), 1 );
add_action( 'init', array( $this, 'maybe_setup_late_hooks' ), 999 );
}
public function setup_upload_filter() {
if ( $this->is_jetformbuilder_request() ) {
add_filter( 'upload_dir', array( $this, 'custom_upload_directory' ), 999 );
}
}
public function maybe_setup_late_hooks() {
if ( class_exists( 'Jet_Form_Builder\Plugin' ) ) {
add_action( 'jet-form-builder/request-handler/before-send', array( $this, 'enable_custom_upload' ) );
add_action( 'jet-form-builder/request-handler/after-send', array( $this, 'disable_custom_upload' ) );
add_action( 'jet-form-builder/file-upload/before-upload', array( $this, 'enable_custom_upload' ) );
add_action( 'jet-form-builder/file-upload/after-upload', array( $this, 'disable_custom_upload' ) );
}
}
public function enable_custom_upload() {
$this->is_jfb_upload = true;
add_filter( 'upload_dir', array( $this, 'custom_upload_directory' ), 999 );
}
public function disable_custom_upload() {
$this->is_jfb_upload = false;
remove_filter( 'upload_dir', array( $this, 'custom_upload_directory' ), 999 );
}
public function custom_upload_directory( $upload_data ) {
if ( ! $this->is_jfb_upload && ! $this->is_jetformbuilder_request() ) {
return $upload_data;
}
$custom_subdir = '/' . sanitize_file_name( $this->custom_folder );
$custom_path = $upload_data['basedir'] . $custom_subdir;
if ( ! file_exists( $custom_path ) ) {
if ( ! wp_mkdir_p( $custom_path ) ) {
error_log( 'JFB Custom Upload: Failed to create directory ' . $custom_path );
return $upload_data;
}
}
if ( ! is_writable( $custom_path ) ) {
error_log( 'JFB Custom Upload: Directory not writable ' . $custom_path );
return $upload_data;
}
$upload_data['path'] = $custom_path;
$upload_data['url'] = $upload_data['baseurl'] . $custom_subdir;
$upload_data['subdir'] = $custom_subdir;
return $upload_data;
}
private function is_jetformbuilder_request() {
$indicators = array(
( isset( $_POST['action'] ) && (
strpos( $_POST['action'], 'jet_form_builder' ) !== false ||
strpos( $_POST['action'], 'jetformbuilder' ) !== false
) ),
isset( $_POST['_jet_form_builder'] ),
isset( $_POST['jet_form_builder'] ),
( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'jet-form-builder' ) ),
did_action( 'jet-form-builder/request-handler/request' ),
doing_action( 'jet-form-builder/request-handler/request' ),
( isset( $_FILES ) && ! empty( $_FILES ) && $this->has_jfb_context() ),
( defined( 'REST_REQUEST' ) && REST_REQUEST &&
isset( $_SERVER['REQUEST_URI'] ) &&
strpos( $_SERVER['REQUEST_URI'], 'jet-form-builder' ) !== false
)
);
return in_array( true, $indicators, true );
}
private function has_jfb_context() {
return (
( isset( $_SERVER['HTTP_REFERER'] ) &&
strpos( $_SERVER['HTTP_REFERER'], 'jet-form-builder' ) !== false
) ||
( isset( $_POST['form_id'] ) && is_numeric( $_POST['form_id'] ) ) ||
( function_exists( 'jet_form_builder' ) &&
class_exists( 'Jet_Form_Builder\Classes\Tools' )
)
);
}
}
// Initialize the handler
new JFB_Custom_Upload_Handler();
}
// .htaccess protection for the custom folder
add_action( 'wp_loaded', function() {
$upload_dir = wp_upload_dir();
$custom_path = $upload_dir['basedir'] . '/' . JFB_CUSTOM_UPLOAD_DIR_NAME;
$htaccess_file = $custom_path . '/.htaccess';
if ( file_exists( $custom_path ) && ! file_exists( $htaccess_file ) ) {
$htaccess_content = "# Protect JetFormBuilder uploads\n";
$htaccess_content .= "Options -Indexes\n";
$htaccess_content .= "<Files ~ \"\\.(php|php3|php4|php5|phtml|pl|py|jsp|asp|sh|cgi)$\">\n";
$htaccess_content .= " deny from all\n";
$htaccess_content .= "</Files>\n";
file_put_contents( $htaccess_file, $htaccess_content );
}
}, 999 );
// Email attachment compatibility
add_filter( 'jet-form-builder/mail/attachment-path', function( $attachment_path, $file_data ) {
if ( strpos( $attachment_path, JFB_CUSTOM_UPLOAD_DIR_NAME ) === false ) {
return $attachment_path;
}
if ( ! file_exists( $attachment_path ) ) {
$upload_dir = wp_upload_dir();
$filename = basename( $attachment_path );
$custom_path = $upload_dir['basedir'] . '/' . JFB_CUSTOM_UPLOAD_DIR_NAME . '/' . $filename;
if ( file_exists( $custom_path ) ) {
return $custom_path;
}
}
return $attachment_path;
}, 10, 2 );
// Cleanup function (optional - for uninstall)
function jfb_custom_upload_cleanup() {
$upload_dir = wp_upload_dir();
$custom_path = $upload_dir['basedir'] . '/' . JFB_CUSTOM_UPLOAD_DIR_NAME;
if ( file_exists( $custom_path ) ) {
// Manual cleanup logic here if desired
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment