Last active
March 29, 2021 02:00
-
-
Save smartdeal/9e34dd38a2edc2429331b7502383952e to your computer and use it in GitHub Desktop.
add data from Contact form 7 to post with attachments (ACF) #Wordpress #WP_CF7 #WP_ACF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('wpcf7_before_send_mail', 'send_mail'); | |
function send_mail($form){ | |
global $wpdb; | |
$submission = WPCF7_Submission::get_instance(); | |
if ( $submission && 138 == $form->id() ) { | |
$form->skip_mail = true; | |
$submited = array(); | |
$submited['title'] = $form->title(); | |
$submited['posted_data'] = $submission->get_posted_data(); | |
$uploadedFiles = $submission->uploaded_files(); | |
$data_name = $submited['posted_data']['nm']; | |
$data_tel = $submited['posted_data']['tel']; | |
$data_mail = $submited['posted_data']['mail']; | |
$data_msg = $submited['posted_data']['msg']; | |
file_put_contents('reviews.txt', print_r($uploadedFiles, true)); | |
} | |
$new_post = array( | |
'post_title' => 'Отзыв о леcтнице', | |
'post_type'=>'review', | |
'post_status'=>'draft', | |
'post_content'=>$data_msg | |
); | |
$post_id = wp_insert_post($new_post); | |
if ($post_id) { | |
if ($data_name) update_field('reviews_name',$data_name,$post_id); | |
if ($data_tel) update_field('reviews_tel',$data_tel,$post_id); | |
if ($data_mail) update_field('reviews_email',$data_mail,$post_id); | |
$arrImg = array(); | |
foreach ($uploadedFiles as $value) { | |
$file = $value; | |
$filename = basename($file); | |
$upload_file = wp_upload_bits($filename, null, file_get_contents($file)); | |
if (!$upload_file['error']) { | |
$wp_filetype = wp_check_filetype($filename, null ); | |
$attachment = array( | |
'post_mime_type' => $wp_filetype['type'], | |
'post_parent' => $post_id, | |
'post_title' => preg_replace('/\.[^.]+$/', '', $filename), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
$attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $post_id ); | |
if (!is_wp_error($attachment_id)) { | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
$attach_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] ); | |
wp_update_attachment_metadata( $attachment_id, $attach_data ); | |
$arrImg[] = $attachment_id; | |
} | |
} | |
} | |
if (count($arrImg) > 0) { | |
update_field('reviews_img', $arrImg, $post_id ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment