I am working on a blog where I am using contact form 7. But I need these info to save into db and want to show in user profile with attachment also. As cf7 upload images or attachment temporary in wpcf_upload folders. I can save all data in custom table by this:
add_action( 'wpcf7_before_send_mail', 'save_application_form');
So first i have to keep images or attachment of cf7 permanently on server.
So please tell me how to do it?
Thanks
I would suggest you to create your own custom post type to handle user profile, and then create a side bar widget or something to submit the data,
But if you want to save wpcf7 data, insert the below code inside your function.php file,
Make sure you create your custom table,
add_action('wpcf7_before_send_mail', 'save_application_form' );
function save_application_form($wpcf7)
{
global $wpdb;
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$submited = array();
$submited['title'] = $wpcf7->title();
$submited['posted_data'] = $submission->get_posted_data();
}
$data = array(
'name' => $submited['posted_data']['name'],
'email' => $submited['posted_data']['email']
);
$wpdb->insert($wpdb->prefix . 'your_table_name', array(
'form' => $submited['title'],
'data' => serialize($data) ,
'date' => date('Y-m-d H:i:s')
));
}
Here's a link.
I simplified the code from the link above. Maybe it helps:
function cf7_create_post($data) {
//In case you wanna check for a especific form
/* $form_id = $data->id;
if ($form_id != '1234')
return;/* */
//Get the current posted form instance
$form_to_DB = WPCF7_Submission::get_instance();
if ($form_to_DB) {
$formData = $form_to_DB->get_posted_data(); // Get all data from the posted form
$uploaded_files = $form_to_DB->uploaded_files(); // this allows you access to the upload file in the temp location
}
// Let's insert the new post first to get the ID.
$newpostid = wp_insert_post(array(
'post_status' => 'draft',
'post_title' => "Your new Post Title", // Can be $formData['YOUR-CF7-FIELD-NAME']
'post_type' => "post", //Post Type
'post_content' => "Your new Post Content", // Can be $formData['YOUR-CF7-FIELD-NAME']
));
// We need to get the CF7 field name from FILE
$cf7_file_field_name = 'uploadyourfile'; // [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);
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);
//Define the new Thumbnail can be also a ACF field
update_post_meta($newpostid, "_thumbnail_id", $attach_id);
}
}
add_action('wpcf7_before_send_mail', 'cf7_create_post',1);//added to functions.php
All credits go to the link above, I just changed a bit. ;)
You can keep the uploaded files in server by installing "Contact Form Submissions" plugin. The uploaded files will be saved to "wp-content/uploads/wpcf7-submissions/".