Attached Uploaded File to Newly Created Post using Gravity Forms Advanced Post Creation Add-On

, ,

Using the plugin Gravity Forms Advanced Post Creation Add-On allows you to save form fields to ACF data points for any post type.  Using this plugin doesn't handle uploading files/images easily. The below function looks at the form with ID number 3 and file upload filed that is limited to images with ID number 20.  First this filter saves the logo to the media library and then it attaches the media library image as the featured image to the newly created post that was created using this post creation plugin.

// set the _3 to the form ID that contains the file upload and post creation
add_action( 'gform_advancedpostcreation_post_after_creation_3', 'save_logo_to_featured', 10, 4 );
function save_logo_to_featured( $post_id, $feed, $entry, $form ) {

    $field_id = 20; //set this to the file upload field ID
    $field = GFAPI::get_field( $form, $field_id );

    foreach ($form['fields'] as $field) {
        if ($field->type == 'fileupload') {
        //See if an image was submitted with this entry
        if (isset($entry[$field->id])) {
            $fileurl = $entry[$field->id];
             
            // The ID of the post this attachment is for. Use 0 for unattached.
            $parent_post_id = $post_id; //the newly created post ID is passed through the filter
             
            // Check the type of file. We'll use this as the 'post_mime_type'.
            $filetype = wp_check_filetype( basename( $fileurl ), null );
             
            // Get the path to the upload directory.
            $wp_upload_dir = wp_upload_dir();
             
            //Gravity forms often uses its own upload folder, so we're going to grab whatever location that is
            $parts = explode('uploads/', $entry[$field->id]);
            $filepath = $wp_upload_dir['basedir'].'/'.$parts[1];
            $fileurl = $wp_upload_dir['baseurl']. '/'.$parts[1];
             
            // Prepare an array of post data for the attachment.
            $attachment = array(
            'guid' => $fileurl,
            'post_mime_type' => $filetype['type'],
            'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $fileurl) ),
            'post_content' => '',
            'post_status' => 'inherit'
            );
             
            // Insert the attachment.
            $attach_id = wp_insert_attachment( $attachment, $filepath, $parent_post_id );
             
            //Image manipulations are usually an admin side function. Since Gravity Forms is a front of house solution, we need to include the image manipulations here.
            require_once( ABSPATH . 'wp-admin/includes/image.php' );
             
            // Generate the metadata for the attachment, and update the database record.
            if ($attach_data = wp_generate_attachment_metadata($attach_id, $filepath)) {
                wp_update_attachment_metadata($attach_id, $attach_data);
            } else {
                echo '
                <div id="message" class="error">
                <h1>Failed to create Meta-Data</h1>
                </div>
                ';
            }
             
            wp_update_attachment_metadata( $attach_id, $attach_data );
            //$attach_id
            set_post_thumbnail($post_id, $attach_id); //set the uploaded image as the featured image
             
            }
        }
    }


}

 

Skills

Posted on

July 8, 2022

Submit a Comment

Your email address will not be published. Required fields are marked *