1

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

2
  • Why not enable debugging ( DEBUG_LOG ) and use PHPs error_log() function to write it to a log. You could print_r() the whole posted variable and see what it holds.
    – Howdy_McGee
    Commented Jan 19, 2018 at 18:33
  • @Howdy_McGee you are right, thank you. So the error_log show 1 as the value of my variable. So the file information is not stored there. I'll modify my question.
    – Pipo
    Commented Jan 19, 2018 at 19:21

3 Answers 3

4

Thanks @Judd Franklin for the directions. I was also missing $submission->uploaded_files();.

Here is the working code for those who are looking for the same answer:

function image_form_to_featured_image( $contact_form ) {

    $submission = WPCF7_Submission::get_instance();
    $posted_data = $submission->get_posted_data();

    // Creating a new post with contact form values
    $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);

    // Retrieving and inserting uploaded image as featured image
    $uploadedFiles = $submission->uploaded_files();

    if( isset($posted_data['featured']) ){
        $featuredUpload = wp_upload_bits($posted_data['featured'], null, file_get_contents($uploadedFiles['featured']));


        require_once(ABSPATH . 'wp-admin/includes/admin.php');
        $filename = $featuredUpload['file'];
        $attachment = array(
            'post_mime_type' => $featuredUpload['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 );
        }
    }
}
add_action( 'wpcf7_before_send_mail', 'image_form_to_featured_image' );
1

It looks like the filter you are hooking onto doesn't have access to the post data.

If you use the wpcf7_before_send_mail action, and then access the Submission instance data, you get access to the filename of the attachment.

function form_to_post( $posted_data ) {
    $post_id = wp_insert_post($args);
    $submission = WPCF7_Submission::get_instance();
    $postData = $submission->get_posted_data();
    die(var_dump($postData["featured"]));

}
add_action('wpcf7_before_send_mail', 'form_to_post' );

This worked for me locally. Does it work for you?

0

Contact form 7 removed file from temporary directory (wp-content/uploads/wpcf7_uploads) after the mail is sent. That's why we need to make a copy of that file in server.

 function image_form_to_featured_image( $contact_form ) {

$submission = WPCF7_Submission::get_instance();
$posted_data = $submission->get_posted_data();


// Creating a new post with contact form values
$args = array(
    'post_type' => 'post',
    'post_status'=> 'draft',
    'post_title'=> wp_strip_all_tags( $posted_data['title'] ),
    'post_content'=> wp_strip_all_tags( $posted_data['content'] ),
);
$post_id = wp_insert_post($args);

// Retrieving and inserting uploaded image as featured image
$uploadedFiles = $submission->uploaded_files();

if( isset($uploadedFiles['image']) ){
    $featuredUpload = wp_upload_bits($uploadedFiles['image'], null, file_get_contents($uploadedFiles['image']));


    require_once(ABSPATH . 'wp-admin/includes/admin.php');
    
    $dataimg= $uploadedFiles['image'];
    $dataFile = $dataimg['0'];
    $filename = basename($dataFile);
    
    $copy_path = '/home/server_user/public_html/wp-content/uploads/new_upload_folder/'.$filename;
    // Copy image to other folder in server
copy($dataFile,$copy_path);
    
    $feature_img = '/home/server_user/public_html/wp-content/uploads/new_upload_folder/'.$filename;
    

    // Check the type of file. We'll use this as the 'post_mime_type'.
    $filetype = wp_check_filetype( basename( $filename ), null );

    // Get the path to the upload directory.
    $wp_upload_dir = wp_upload_dir();

    // Prepare an array of post data for the attachment.
    $attachment = array(
        'guid'           => $wp_upload_dir['url'] . '/new_upload_folder/' . basename( $feature_img ), 
        'post_mime_type' => $filetype['type'],
        'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $feature_img ) ),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );

    // Insert the attachment.
    $attach_id = wp_insert_attachment( $attachment, $feature_img, $post_id );

    // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );

    // Generate the metadata for the attachment, and update the database record.
    $attach_data = wp_generate_attachment_metadata( $attach_id, $feature_img );
    wp_update_attachment_metadata( $attach_id, $attach_data );

    set_post_thumbnail( $post_id, $attach_id );
    
  }
}
add_action( 'wpcf7_before_send_mail', 'image_form_to_featured_image' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.