eventmedia,
We don't have a hook specific for renaming the uploaded file, but with a little extra work, the post submission hook can do the trick.
Following is the code snippet you will need. Place it in your theme's functions.php file.
Also, you will need to replace the form id (line 6) and the field id's with the appropriate ones for your form (i.e. $entry[2] and $entry[1]). You can find the field id by inspecting your form's html and looking at the input name. It will be in the format name="input_ID" (i.e. name="input_2")
add_action("gform_post_submission", "rename_file", 10, 2);
function rename_file($entry, $form){
//ignore requests from forms other than ID 18
if($form["id"] != 18)
return;
$upload_info = wp_upload_dir();
//original name/path
$original_file_url = $entry[2];
$original_file_path = str_replace($upload_info["baseurl"], $upload_info["basedir"], $original_file_url);
$original_file_info = pathinfo($original_file_url);
//New File Name (without extension).
//NOTE: Change this line to create the filename you would like to have
$new_name = $entry["id"] . "_" . $entry[1];
//adding same extension as original
$new_name .= "." . $original_file_info["extension"];
$new_file_url = str_replace($original_file_info["basename"], $new_name, $original_file_url);
$new_file_path = str_replace($original_file_info["basename"], $new_name, $original_file_path);
//rename file
$is_success = rename($original_file_path, $new_file_path);
//if file was renamed successfully, updating entry so that it points to the new file
if($is_success){
global $wpdb;
$wpdb->update(RGFormsModel::get_lead_details_table_name(), array("value" => $new_file_url), array("lead_id" => $entry["id"], "value" => $original_file_url));
}
}
Posted 14 years ago on Wednesday December 23, 2009 |
Permalink