Skip to content

Use an alternate upload directory

Use an alternate upload directory for the images.

// Change the upload path and ensure it's used when deleting or editing files.
add_filter( "gform_upload_path", function( $path_info, $form_id ) {
  $path_info["path"] = WP_CONTENT_DIR . "/uploads/Images/" . $form_id . "/";
  $path_info["url"]  = WP_CONTENT_URL . "/uploads/Images/" . $form_id . "/";

  return $path_info;
}, 10, 2 );

/* Ensure the new upload path is used when deleting files */
add_filter( 'gform_file_path_pre_delete_file', function( $file_path, $url ) {
  $wp_upload_dir = wp_upload_dir();
  $file_path     = str_replace( $wp_upload_dir['baseurl'], $wp_upload_dir['basedir'], $file_path );

  return $file_path;
}, 1, 2 );

/* Ensure the new upload path is used when overriding files when using the editor */
add_filter( 'image_hopper_editor_physical_file_path', function( $file_path, $url ) {
  $wp_upload_dir = wp_upload_dir();
  $file_path     = str_replace( $wp_upload_dir['baseurl'], $wp_upload_dir['basedir'], $file_path );

  return $file_path;
}, 10, 2 );