Advertisement
adczk

Custom multi-upload code / Forminator

Oct 4th, 2023
1,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.86 KB | None | 0 0
  1. add_action('forminator_form_after_handle_submit', 'wpmudev_uploaded_filename_fix', 10, 2);
  2. add_action('forminator_form_after_save_entry', 'wpmudev_uploaded_filename_fix', 10, 2);
  3. function wpmudev_uploaded_filename_fix($form_id, $response) {
  4.     if ( $form_id != 761 ) {
  5.         return;
  6.     }
  7.  
  8.     if ( $response && is_array( $response ) ) {
  9.         if ( $response['success'] ) {
  10.             $attachment_ids = array();
  11.             $data = Forminator_CForm_Front_Action::$prepared_data;
  12.             if ( !empty( $data['upload-1'] ) ) {
  13.                 if ( !empty( $data['upload-1']['file']['file_url'] ) ) {
  14.                     foreach( $data['upload-1']['file']['file_url'] as $key => $file_url ) {
  15.                         $attachment_ids[] = attachment_url_to_postid($file_url);
  16.                     }
  17.                 }
  18.             }
  19.  
  20.             // Create a post with a gallery shortcode
  21.             if (!empty($attachment_ids)) {
  22.                 // Create the post title.
  23.                 $post_title = 'Test-Title';
  24.                 // Create Post Content using Block Editor Blocks
  25.                 $post_content = 'test-content';
  26.  
  27.                 // Create a new post
  28.                 $post_data = array(
  29.                     'post_title'   => $post_title,
  30.                     'post_content' => $post_content,
  31.                     'post_status'  => 'publish',
  32.                     'post_type'    => 'post',
  33.                 );
  34.  
  35.                 $post_id = wp_insert_post($post_data);
  36.  
  37.                 // Set the first attached image as the post thumbnail
  38.                 if ($post_id && !is_wp_error($post_id) && !empty($attachment_ids[0])) {
  39.                     set_post_thumbnail($post_id, $attachment_ids[0]);
  40.                 } else {
  41.                     error_log('Error creating post: ' . print_r($post_id, true));
  42.                 }
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement