The gform_media_upload_path filter allows you to change the location where files uploaded using the Post Image field are copied to when the post is created.
On this pageJump to a section
Usage
To apply the filter to all forms:
add_filter( 'gform_media_upload_path', 'your_function_name', 10, 3 );
To limit the scope of your function to a specific form, append the form id to the end of the hook name (format: gform_media_upload_path_FORMID):
add_filter( 'gform_media_upload_path_5', 'your_function_name', 10, 3 );
Parameters
- $upload_dir (array)
$upload_dir['path'](string): Full physical path to the folder.$upload_dir['url'](string): Full URL to the folder.
- $form_id (integer): The ID of the form currently being processed.
- $post_id (integer): The ID of the post created from the entry currently being processed.
More information
See Gravity Forms Docs: gform_media_upload_path
Examples
Log the current path
Find out the current path with logging enabled:
add_filter( 'gform_media_upload_path', function( $upload_dir, $form_id ) {
GFCommon::log_debug( "gform_media_upload_path(): upload_dir for form #{$form_id} => " . print_r( $upload_dir, true ) );
return $path_info;
}, 1, 2 );
Change the path and URL
Change the upload path and URL for the File Upload field:
add_filter( 'gform_media_upload_path', 'change_media_upload_path', 10, 3 );
function change_media_upload_path( $upload_dir, $form_id, $post_id ) {
$upload_dir['path'] = '/home/public_html/yourdomainfolder/new/path/';
$upload_dir['url'] = 'http://yourdomainhere.com/new/path/';
return $upload_dir;
}
Change the path and URL for a specific form
Change the upload path and URL for the File Upload field in form ID 5:
add_filter( 'gform_media_upload_path_5', 'change_media_upload_path_for_form_5', 10, 3 );
function change_media_upload_path_for_form_5( $upload_dir, $form_id, $post_id ) {
$upload_dir['path'] = '/home/public_html/yourdomainfolder/form_5_new/path/';
$upload_dir['url'] = 'http://yourdomainhere.com/form_5_new/path/';
return $upload_dir;
}
Add date-based subfolders to the path
Organize uploaded files into date-based subfolders:
add_filter( 'gform_media_upload_path', 'add_date_subfolders_to_path', 10, 3 );
function add_date_subfolders_to_path( $upload_dir, $form_id, $post_id ) {
$subfolder = date('Y/m');
$upload_dir['path'] = trailingslashit( $upload_dir['path'] ) . $subfolder;
$upload_dir['url'] = trailingslashit( $upload_dir['url'] ) . $subfolder;
return $upload_dir;
}