I’m saving the contact form submission with flamingo plugin. And I also save the form uploaded files as WordPress attachments using something this code:
//Save CF7 data
function cf7_create_post($WPCF7_ContactForm) {
//In case you wanna check for a especific form
/* $form_id = $data->id;
if (224 == $WPCF7_ContactForm->id()) { */
//Get current form
$wpcf7 = WPCF7_ContactForm::get_current();
// get current SUBMISSION instance
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$formData = $submission->get_posted_data(); // Get all data from the posted form
$uploaded_files = $submission->uploaded_files(); // this allows you access to the upload file in the temp location
}
// We need to get the CF7 field name from FILE
$cf7_file_field_name = 'file-cv'; // [file uploadyourfile]
//Do the magic the same as the refer link above
$image_name = $formData[$cf7_file_field_name];
$image_location = $uploaded_files[$cf7_file_field_name];
$image_content = file_get_contents($image_location);
$wud = wp_upload_dir();
$upload = wp_upload_bits($image_name, null, $image_content);
$chemin_final = $upload['url'];
$filename = $upload['file'];
if ($filename > '') {
require_once(ABSPATH . 'wp-admin/includes/admin.php');
$wp_filetype = wp_check_filetype(basename($filename), null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $filename); // $newpostid optional
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
$attach_url = get_attached_file( $attach_id );
}
}
add_action('wpcf7_before_send_mail', 'cf7_create_post', 10);
I want to save the URL of the attachment in flamingo (add it to the posted data), when I use ‘wpcf7_before_send_mail’ filter, the ‘wpcf7_posted_data’ filter is already fired and can’t modify the posted data anymore.
I tried to save the file as attachment using ‘wpcf7_posted_data’ filter itself (where I can add a field with the URL and it will show on flamingo post), but when ‘wpcf7_posted_data’ filter is fired, the uploaded files are not available yet in the posted data or uploaded_files() method.
I guess I can achieve that buy modifying the flamingo post later, but sure how to get its info and if it will be a good solution. Thanks
The only way i found to do the task was to add the filepath/filename to a global variable and use one of the actionhooks of the flamingo plugin to insert the filepath.
in contact form 7 under modules/flamingo.php there is a hook "wpcf7_after_flamingo":
we can use it to update the fields.
'wpcf7_before_send_mail' is triggered before the flamingo-hook, save the filepath in a global variable.