提交后删除 Gravity Forms 文件附件但保留所有其他字段?

Delete Gravity Forms file attachments after submission but keep all other fields?

好的,我们在网站上有一个工作提交表,用户需要填写该表并附上简历和求职信。出于隐私原因,我们不想在服务器上保留简历和求职信,所以我们一直在发送带有表单提交的电子邮件,附上简历和求职信,然后完全删除提交。

这是我们一直在使用的代码,一切正常。

/**
 *
 * Target submissions from form ID  2.
 *
 * Change gform_after_submission_2 to reflect your target form ID, 
 * or use gform_after_submission to target all forms.
 * 
 */

add_action( 'gform_after_submission_2', 'remove_form_entry' );
function remove_form_entry( $entry ) {
    GFAPI::delete_entry( $entry['id'] );
}

我想知道是否有办法保留表单条目而只删除附件?

我刚写了这个函数,测试了一下,删除resume文件没问题。 您可以为求职信添加更多代码。

add_filter( 'gform_after_submission_2', 'remove_form_entry', 10, 2 );
function remove_form_entry( $entry, $form  ) {
     global $wpdb;
 
    $lead_id = $entry['id'];
    
    $meta_key = 1; //it is the uploads field id.
    
    $entry_table = $wpdb->prefix . 'gf_entry_meta'; //meta table of gravity forms
    
    $get_resume_file_statement = $wpdb->prepare( "SELECT `meta_value` FROM `$entry_table` WHERE  `meta_key` = %d AND `entry_id` = %d", $meta_key, $lead_id); 
    $meta_values_of_resume_file = $wpdb->get_col( $get_resume_file_statement ); // get uploaded csv file url
     
    $resume_url = explode('uploads', $meta_values_of_resume_file [0]); //get file url after uploads folder. It returns file url like http://localhost/g2a/wp-content/uploads/gravity_forms/3-dbbe121585c30ed9e49ec2a6803270b0/2021/07/89636498_188586552577282_8867025936109797376_n.jpg
    
    $resume_file_name = end($resume_url ); //get file url after uploads folder. value like /gravity_forms/3-dbbe121585c30ed9e49ec2a6803270b0/2021/07/89636498_188586552577282_8867025936109797376_n.jpg

    $upload_dir = wp_upload_dir(); //uploads dir
    
    $full_path = $upload_dir['basedir'] . $resume_file_name; //get full path of file
    wp_delete_file( $full_path ); //delete the file
    //update entry meta field value here or delete the entry meta.
}