Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

contact form 7 - add featured image to post WordPress

  • SOLVED

I'm using the code below to add a post using contact form 7.

I need to add the first image from the form as a featured image on the post.

The image file field is [artistimage1]


// CONTACT FORM 7

function my_wpcf7_save($cfdata) {

$formtitle = $cfdata->title;
$formdata = $cfdata->posted_data;

if ( $formtitle == 'Artists Application') {

// access data from the submitted form
$formfield = $formdata['fieldname'];

// create a new post
$newpost = array( 'post_title' => $formdata['artist-artist1'],
'post_type' => 'artistsposts',
'post_status' => 'draft');

$newpostid = wp_insert_post($newpost);

// add meta data for the new post
add_post_meta($newpostid, 'studio', $formdata['artist-studio']);
add_post_meta($newpostid, 'website', $formdata['artist-website']);

}

}
add_action('wpcf7_before_send_mail', 'my_wpcf7_save',1);

Answers (1)

2013-03-28

Arnav Joy answers:

which type of field you have taken for "artistimage1" ? if it file type then try following code:-


<?php

function my_wpcf7_save($cfdata) {

require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');


$formtitle = $cfdata->title;

$formdata = $cfdata->posted_data;



if ( $formtitle == 'Artists Application') {



// access data from the submitted form

$formfield = $formdata['fieldname'];



// create a new post

$newpost = array( 'post_title' => $formdata['artist-artist1'],

'post_type' => 'artistsposts',

'post_status' => 'draft');



$newpostid = wp_insert_post($newpost);

$attach_id = media_handle_upload( $formdata['artistimage1'], $newpostid );

// add meta data for the new post

add_post_meta($newpostid, 'studio', $formdata['artist-studio']);

add_post_meta($newpostid, 'website', $formdata['artist-website']);



}



}

add_action('wpcf7_before_send_mail', 'my_wpcf7_save',1);


?>


Austin comments:

The field for the image is a file attachment - [artistimage1]

Tried the code - the form went through and created a post but did not create a featured image,

The image did not go in the media library either.


Austin comments:

Arnav did a great job - here is the finished code:


// CONTACT FORM 7

remove_all_filters ('wpcf7_before_send_mail');

add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );

function contactform7_before_send_mail( $cf7 ) {


$formtitle = $cf7->title;
$formdata = $cf7->posted_data;

if ( $formtitle == 'FORM NAME') {

$article_title = $formdata['title-field'];


$my_post = array();
$my_post['post_title'] = $article_title;
$my_post['post_status'] = 'draft';


$new_post_id = wp_insert_post( $my_post );


$image_name = $cf7->posted_data["file-field"];
$image_location = $cf7->uploaded_files["file-field"];

$content = file_get_contents($image_location);
$wud = wp_upload_dir();

$upload = wp_upload_bits( $image_name, '', $content);
$chemin_final = $upload['url'];
$filename= $upload['file'];

require_once(ABSPATH . 'wp-admin/includes/admin.php');
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);

$attach_id = wp_insert_attachment( $attachment, $filename, $new_post_id);

require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_post_meta($new_post_id, "_thumbnail_id", $attach_id);

add_post_meta($new_post_id, 'customfield1', $formdata['customfield1']);
add_post_meta($new_post_id, 'customfield2', $formdata['customfield2']);

}
}