I am trying to retrieve a file uploaded in the front-end with contact form 7 and assign it to a featured image custom post type. Heres is my code so far :
function form_to_post( $posted_data ) {
$args = array(
'post_type' => 'projects',
'post_status'=> 'draft',
'post_title'=> wp_strip_all_tags( $posted_data['title'] ),
'post_content'=> wp_strip_all_tags( $posted_data['pitch'] ),
);
$post_id = wp_insert_post($args);
if( ! is_wp_error( $post_id ) ) {
if( isset($posted_data['featured']) ){
$featuredUpload = wp_upload_bits($posted_data['featured']['name'], null, file_get_contents($posted_data['featured']['tmp_name']));
$filename = $featuredUpload['file'];
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_parent' => $post_id,
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
if (!is_wp_error($attachment_id)) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
set_post_thumbnail( $post_id, $attachment_id );
}
}
}
return $posted_data;
}
add_filter( 'wpcf7_posted_data', 'form_to_post' );
The error_log
shows 1 as the value of $posted_data['featured']
so that means the file datas are not stored in this variable. I've looked at the Contact Form 7 doc and they say that the file is moved to a temporary directory (wp-content/uploads/wpcf7_uploads) before the mail is sent. So does anyone know how to get the file datas?
Thanks
error_log()
function to write it to a log. You couldprint_r()
the whole posted variable and see what it holds.