好的,我们在网站上有一个工作提交表,用户需要在其中填写表格并附上简历和求职信。出于隐私原因,我们不想将简历和求职信保留在服务器上,所以我们一直在做的是发送带有表单提交的电子邮件,附上简历和求职书,然后完全删除提交的内容。
这是我们一直在使用的代码,一切都按照它应该的方式进行。
/**
*
* 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'] );
}
我想知道是否有一种方法可以保留表格条目,但只删除附件?
我刚刚编写了函数,经过测试,删除简历文件效果很好。你们可以用同样的方式为求职信添加更多的代码。
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.
}