• Resolved acamilicko

    (@acamilicko)


    Hello, thank you for great plugin, saved my life. But now problem is persisting on particular issue.

    The form consists of may inputs, and all work except file/image/thumbnail.

    Code in CF7 is:
    Your Image: [file image limit:1048576 filetypes:jpg|jpeg|png|gif]

    And code in functions.php is (I got rid of unnecessary lines of code for showing here):

    
    add_action( 'cf7_2_post_save_form', 'a_new_post',10,3);
    function a_new_post($cf7_key, $submitted_data, $submitted_files){
    
        $thumbnail =  $submitted_data['image'];
        //first insert/update your new post.
        //for the current user if someone is logged in.
        
      $post_args = array(
            'the_post_thumbnail' => $thumbnail
      );
      //check if this is an existing post (draft) being saved/submitted
      if(isset($submitted_data['map_post_id'])){
        $post_id = $submitted_data['map_post_id'];
        $post_args['ID']=$post_id;
        wp_udpate_post($post_args);
      }else{
        $post_id = wp_insert_post($post_args);
      }
      //update the meta-field
      update_post_meta($post_id, 'the_post_thumbnail', $thumbnail);
    }

    Do you have any idea what am I doing wrong?

    Thanks,
    Alex

    • This topic was modified 7 years, 9 months ago by acamilicko.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Aurovrata Venet

    (@aurovrata)

    Hello Alex

    why not simply map your image field directly to the feature image of the post when you are mapping your form? (see screenshot 6).

    Thread Starter acamilicko

    (@acamilicko)

    Hi Aurovrata, thanks for your answer. This was mapping to existing CPT, so I couldn’t use that kind of mapping…

    Plugin Author Aurovrata Venet

    (@aurovrata)

    ok, got it.

    then you need to make sure you upload your file to the upload folder, insert it into the media library (create an attachment post) and finally attach it to the post as a thumbnail. Here is the code for this,

    //$file is the path to your uploaded file (for example as set in the $_FILE posted file array)
    //$filename is the name of the file
    //first we need to upload the file into the wp upload folder. 
    $upload_file = wp_upload_bits($filename, null, @file_get_contents($file));
    if (!$upload_file['error']) {
      //if succesfull insert the new file into the media library (create a new attachment post type)
      $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'
      );
      //wp_insert_attachment( $attachment, $filename, $parent_post_id );
      $attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $post_id );
      if (!is_wp_error($attachment_id)) {
         //if attachment post was successfully created, insert it as a thumbnail to the post $post_id
         require_once(ABSPATH . "wp-admin" . '/includes/image.php');
         //wp_generate_attachment_metadata( $attachment_id, $file ); for images
         $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
         wp_update_attachment_metadata( $attachment_id,  $attachment_data );
         set_post_thumbnail( $post_id, $attachment_id );
       }
    }
    Thread Starter acamilicko

    (@acamilicko)

    Hi Aurovrata,

    the above code didn’t work at all… After several days of trying a number of combinations, here is a code that works:

    require_once(ABSPATH . 'wp-admin/includes/image.php');
    require_once(ABSPATH . 'wp-admin/includes/media.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    	
    	
    $file_path = ABSPATH . "wp-content/uploads/attorney_images/" . $filename;
    	
    if (move_uploaded_file($file, $file_path)) {
    		
    	chmod($file_path, 0755);
    		
    	$filetype = wp_check_filetype( basename( $file_path ), 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'] . '/' . basename( $file_path ), 
    	'post_mime_type' => $filetype['type'],
    	'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $file_path ) ),
    	'post_content'   => '',
    	'post_status'    => 'inherit'
    		);
    
    // Insert the attachment.
    	$attach_id = wp_insert_attachment( $attachment, $file_path, $post_id );
    
    	// Generate the metadata for the attachment, and update the database record.
    	$attach_data = wp_generate_attachment_metadata( $attach_id, $file_path );
    	wp_update_attachment_metadata( $attach_id, $attach_data );
    		
    	exec ("find " . ABSPATH . "wp-content/uploads/attorney_images/ -type f -exec chmod 0755 {} +");
    	set_post_thumbnail( $post_id, $attach_id );
    		
    	}
    • This reply was modified 7 years, 8 months ago by acamilicko.
    Plugin Author Aurovrata Venet

    (@aurovrata)

    oh good. Glad you made it work. v2.0 is out which allows you to map forms to existing posts usign the GUI. Hopefully this kind of exercise should become simpler moving forward.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Thumbnail not showing up’ is closed to new replies.