Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.77 KB | None | 0 0
  1. function wpcf7_to_post($cfdata) {
  2.    // Daten aus der Anfrage auslesen
  3.     $submission = WPCF7_Submission::get_instance();
  4.     if ( $submission ) {
  5.         $formdata = $submission->get_posted_data();
  6.     }
  7.    
  8.     // Post ID des Formulares angeben, welches "abgefangen" werden soll
  9.     if ( $cfdata->id() == '126') {
  10.         $newdraft = array(
  11.             'post_title'=> $formdata['your-name'],
  12.             'post_content'=> $formdata['your-message'],
  13.             'post_status' => 'draft', // Status
  14.             'post_type' => 'testimonials', // Post Type
  15.         );
  16.  
  17.         // Eintrag als Entwurf anlegen
  18.         $newpostid = wp_insert_post($newdraft);
  19.  
  20.         // Bild in die Mediathek kopieren/hochladen
  21.         $random = date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000)).rand(0,10000);
  22.         $uploaded_files = $submission->uploaded_files();
  23.         $cf7_image = $uploaded_files['file-772'];
  24.         $cf7_image_clean = "/".$random.str_replace("/html/wp-content/uploads/cf7_images/","",$cf7_image);
  25.         $wp_upload_dir = wp_upload_dir();
  26.         copy($cf7_image, $wp_upload_dir['path'].'/'.$cf7_image_clean);
  27.  
  28.         $wp_filetype = wp_check_filetype($wp_upload_dir['path'].'/'.$cf7_image_clean, null);
  29.         $attachment = array(
  30.             'post_mime_type' => $wp_filetype['type'],
  31.             'post_title' => sanitize_file_name($cf7_image_clean),
  32.             'post_content' => '',
  33.             'post_status' => 'inherit'
  34.         );
  35.  
  36.         $image_id = wp_insert_attachment($attachment, $wp_upload_dir['path'].'/'.$cf7_image_clean, $newpostid);
  37.         require_once(ABSPATH . 'wp-admin/includes/image.php');
  38.         $image_data = wp_generate_attachment_metadata($image_id, $wp_upload_dir['path'].'/'.$cf7_image_clean);
  39.         wp_update_attachment_metadata($image_id, $image_data);
  40.  
  41.         // Bild als Beitragsbild setzen
  42.         set_post_thumbnail($newpostid, $image_id);
  43.     }
  44. }
  45. add_action('wpcf7_before_send_mail', 'wpcf7_to_post',1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement