Skip to content

Instantly share code, notes, and snippets.

@mlbd
Created July 7, 2018 04:34
Show Gist options
  • Save mlbd/25b9c0b41618522b7b95a23b17f739d1 to your computer and use it in GitHub Desktop.
Save mlbd/25b9c0b41618522b7b95a23b17f739d1 to your computer and use it in GitHub Desktop.
Create a post automatically with contact form 7 submission
add_action( 'wpcf7_before_send_mail', 'fordwich_create_post_after_cf7' );
/**
* Do stuff for my contact form form. This function shouldn't return aything
*
* @param WPCF7_ContactForm $contact_form wpcf7 object, passed by refference
*/
function fordwich_create_post_after_cf7( $WPCF7_ContactForm ) {
// Not my desired form? bail
if ( 5 == $WPCF7_ContactForm->id() ) {
//Get current form
$wpcf7 = WPCF7_ContactForm::get_current();
// get current SUBMISSION instance
$submission = WPCF7_Submission::get_instance();
// Ok go forward
if ($submission) {
// get submission data
$data = $submission->get_posted_data();
$uploaded_files = $submission->uploaded_files(); // this allows you access to the upload file in the temp location
// nothing's here... do nothing...
if (empty($data))
return;
$title = isset($data['title']) ? $data['title'] : "";
$content = isset($data['your-message']) ? $data['your-message'] : "";
$reason = isset($data['your-reason']) ? $data['your-reason'] : "";
if ( $reason == 'You want to publish' ) :
$admin_email = get_option( 'admin_email' );
$user = get_user_by( 'email', $admin_email );
$user_id = $user->ID;
$my_post = array(
'post_type' => 'post',
'post_status' => 'pending',
);
if ( !empty($title) ) {
$my_post['post_title'] = wp_strip_all_tags( $title );
}
if ( !empty($content) ) {
$my_post['post_content'] = $content;
}
if ( !empty($user_id) ) {
$my_post['post_author'] = $user_id;
}
// Insert the post into the database
$the_post_id = wp_insert_post( $my_post );
if ( $the_post_id ) {
// We need to get the CF7 field name from FILE
$cf7_file_field_name = 'your-files'; // [file uploadyourfile]
//Do the magic the same as the refer link above
$image_name = $data['your-files'];
$image_location = $uploaded_files['your-files'];
$image_content = file_get_contents($image_location);
$wud = wp_upload_dir();
$upload = wp_upload_bits($image_name, null, $image_content);
$chemin_final = $upload['url'];
$filename = $upload['file'];
if ($filename > '') {
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, $the_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);
//Define the new Thumbnail can be also a ACF field
update_post_meta($the_post_id, "_thumbnail_id", $attach_id);
}
}
endif; // if reason
// return current cf7 instance
return $wpcf7;
}
}
// Do stuff for my contact form
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment