{"id":64,"date":"2015-09-22T16:13:23","date_gmt":"2015-09-22T16:13:23","guid":{"rendered":"http:\/\/chamankumar.co.uk\/blog\/?p=64"},"modified":"2015-09-22T16:13:47","modified_gmt":"2015-09-22T16:13:47","slug":"contact-form-7-add-uploaded-file-to-media-attach-to-custom-post","status":"publish","type":"post","link":"https:\/\/chamankumar.co.uk\/blog\/uncategorized\/contact-form-7-add-uploaded-file-to-media-attach-to-custom-post","title":{"rendered":"Contact Form 7 add uploaded file to media &#038; attach to custom post"},"content":{"rendered":"<p>Here:<br \/>\n<code><br \/>\nfunction my_wpcf7_save($cfdata) {<\/p>\n<p>    $formtitle = $cfdata->title;<\/p>\n<p>    global $wpdb;<br \/>\n    $form_to_DB = WPCF7_Submission::get_instance();<br \/>\n    if ( $form_to_DB ) {<br \/>\n        $formData = $form_to_DB->get_posted_data();<br \/>\n        $uploaded_files = $form_to_DB->uploaded_files(); \/\/ this allows you access to the upload file in the temp location<br \/>\n    }<\/p>\n<p>\tif ( $formtitle == 'sync_test') {<\/p>\n<p>\t\t\/\/ create a new post<br \/>\n\t\t$newpost = array(<br \/>\n            'post_type' => 'students',<br \/>\n            'post_title' => $formData['your-firstname']. ' ' .$formData['your-lastname']. ' - ' .$formData['your-company'],<br \/>\n            'post_status' => 'pending');<\/p>\n<p>\t\t$newpostid = wp_insert_post($newpost);<\/p>\n<p>\t\t\/\/ add meta data for the new post<br \/>\n        add_post_meta($newpostid, 'rli_meta_students_first_name', $formData['your-firstname']);<br \/>\n        add_post_meta($newpostid, 'rli_meta_students_surname', $formData['your-lastname']);<br \/>\n        add_post_meta($newpostid, 'rli_meta_students_email', $formData['your-email']);<br \/>\n        ...<br \/>\n        add_post_meta($newpostid, 'rli_meta_students_notes', $formData['your-notes']);<br \/>\n        \/\/add_post_meta($newpostid, 'rli_meta_students_photo', $formData['ProfilePhoto']); -- do this after saving image in uploads<\/p>\n<p>        $image_name = $formData['ProfilePhoto'];<br \/>\n        $image_location = $uploaded_files[\"ProfilePhoto\"];<br \/>\n        $image_content = file_get_contents($image_location);<br \/>\n        if ($image_content==false){<br \/>\n            $check = 'content fail';<br \/>\n        } else {<br \/>\n            $check = 'content success';<br \/>\n        }<br \/>\n        $wud = wp_upload_dir();<br \/>\n        $upload = wp_upload_bits( $image_name, null, $image_content);<br \/>\n        $chemin_final = $upload['url']; \/\/ http:\/\/www.xxxx.com\/wp-content\/uploads\/2015\/09\/xxxx.jpg<br \/>\n        $filename= $upload['file']; \/\/ \/home\/sites\/xxxx.com\/public_html\/wp-content\/uploads\/wpcf7_uploads\/1565907367\/xxxx.jpg<br \/>\n        if ($filename>'') {<br \/>\n            require_once(ABSPATH . 'wp-admin\/includes\/admin.php');<br \/>\n            $wp_filetype = wp_check_filetype(basename($filename), null );<br \/>\n            $attachment = array(<br \/>\n                            'post_mime_type' => $wp_filetype['type'],<br \/>\n                            'post_title' => preg_replace('\/\\.[^.]+$\/', '', basename($filename)),<br \/>\n                            'post_content' => '',<br \/>\n                            'post_status' => 'inherit'<br \/>\n            );<br \/>\n            $attach_id = wp_insert_attachment( $attachment, $filename, $newpostid);<br \/>\n            require_once(ABSPATH . 'wp-admin\/includes\/image.php');<br \/>\n            $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );<br \/>\n            wp_update_attachment_metadata( $attach_id, $attach_data );<br \/>\n            update_post_meta($newpostid, \"_thumbnail_id\", $attach_id);<br \/>\n            add_post_meta($newpostid, 'rli_meta_students_photo', $chemin_final); \/\/ $formData['ProfilePhoto']<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\nadd_action('wpcf7_before_send_mail', 'my_wpcf7_save',1);<br \/>\n<\/code><br \/>\nadded to functions.php<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here: function my_wpcf7_save($cfdata) { $formtitle = $cfdata->title; global $wpdb; $form_to_DB = WPCF7_Submission::get_instance(); if ( $form_to_DB ) { $formData = $form_to_DB->get_posted_data(); $uploaded_files = $form_to_DB->uploaded_files(); \/\/ this allows you access to the upload file in the temp location } if ( $formtitle == &#8216;sync_test&#8217;) { \/\/ create a new post $newpost = array( &#8216;post_type&#8217; => &#8216;students&#8217;, &#8216;post_title&#8217; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,18,1,8],"tags":[],"_links":{"self":[{"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/posts\/64"}],"collection":[{"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":1,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"predecessor-version":[{"id":65,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/posts\/64\/revisions\/65"}],"wp:attachment":[{"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}