Skip to content

Forminator: Custom multiple upload folder

add_action('forminator_form_after_handle_submit', 'wpmudev_uploaded_filename_fix', 10, 2);
add_action('forminator_form_after_save_entry', 'wpmudev_uploaded_filename_fix', 10, 2);
function wpmudev_uploaded_filename_fix($form_id, $response) {
    if ( $form_id != 761 ) {
        return;
    }
 
    if ( $response && is_array( $response ) ) {
        if ( $response['success'] ) {
            $attachment_ids = array();
            $data = Forminator_CForm_Front_Action::$prepared_data;
            if ( !empty( $data['upload-1'] ) ) {
                if ( !empty( $data['upload-1']['file']['file_url'] ) ) {
                    foreach( $data['upload-1']['file']['file_url'] as $key => $file_url ) {
                        $attachment_ids[] = attachment_url_to_postid($file_url);
                    }
                }
            }
 
            // Create a post with a gallery shortcode
            if (!empty($attachment_ids)) {
                // Create the post title.
                $post_title = 'Test-Title';
                // Create Post Content using Block Editor Blocks
                $post_content = 'test-content';
 
                // Create a new post
                $post_data = array(
                    'post_title'   => $post_title,
                    'post_content' => $post_content,
                    'post_status'  => 'publish',
                    'post_type'    => 'post',
                );
 
                $post_id = wp_insert_post($post_data);
 
                // Set the first attached image as the post thumbnail
                if ($post_id && !is_wp_error($post_id) && !empty($attachment_ids[0])) {
                    set_post_thumbnail($post_id, $attachment_ids[0]);
                } else {
                    error_log('Error creating post: ' . print_r($post_id, true));
                }
            }
        }
    }
}

https://pastebin.com/AdJuBdmi

Dieser Beitrag hat 0 Kommentare

Schreibe einen Kommentar

Deine E-Mail wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

An den Anfang scrollen