Change Upload Directory

Customize the directory to which GP Media Library will import files for a specific form or field.

Instructions

Code

Filename: gpml-change-upload-directory.php

<?php
/**
 * Gravity Perks // Media Library // Change Upload Directory
 * https://gravitywiz.com/documentation/gravity-forms-media-library/
 *
 * Instruction Video: https://www.loom.com/share/f4da8e923c72492ca97eb6135cd1057e
 *
 * Customize the directory to which GP Media Library will import files for a specific form or field.
 */
// Using "gpml_media_data" filter to ensure we only change upload directory for GPML-enabled uploads.
// Update "123" to your form ID.
add_filter( 'gpml_media_data_123', function( $return, $field, $entry ) {
	$modify_upload_dir = function( $upload_dir ) use ( $entry ) {
		// Update the path and URL as desired.
		// You can use values from the entry to create a custom folder.
		$upload_dir['path'] = WP_CONTENT_DIR . "/uploads/{$entry[4]}/my-custom-folder";
		$upload_dir['url']  = WP_CONTENT_URL . "/uploads/{$entry[4]}/my-custom-folder";
		return $upload_dir;
	};
	add_filter( 'upload_dir', $modify_upload_dir );
	// The "wp_handle_upload" filter is triggered after the file has been imported into the Media Library. Remove our
	// upload directory modifier function.
	add_filter( 'wp_handle_upload', function( $return ) use ( $modify_upload_dir ) {
		remove_filter( 'upload_dir', $modify_upload_dir );
		return $return;
	} );
	return $return;
}, 10, 3 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.