0

I'm using the function wp_insert_attachment() to run on a form submission from GravityForms, take a file uploaded in the form, and add it to the WordPress Media Library.

Code in functions.php below, also in Github:

function form_to_media_library($entry){
// from https://developer.wordpress.org/reference/functions/wp_insert_attachment/#div-comment-948

// set filename
$upload_path = GFFormsModel::get_upload_path( $entry[ 'form_id' ] );
$upload_url = GFFormsModel::get_upload_url( $entry[ 'form_id' ] );
$filename_verbose = str_replace( $upload_url, $upload_path, $entry[ '1' ] );
$filename = trim($filename_verbose, ' "[]\ ');


// check the type of file. We'll use this as the 'post_mime_type'
$filetype = wp_check_filetype( basename( $filename ), null );

// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();

// Prepare an array of post data for the attachment.
$attachment = array(
    'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ),
    'post_mime_type' => $filetype['type'],
    'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
    'post_content'   => '',
    'post_status'    => 'inherit'
);

// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename );

// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );

// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );

do_action( 'qm/debug', $attach_id );
}

// targets the specific form by form ID of 1
add_action( 'gform_after_submission_1', 'form_to_media_library', 10, 2 );

This is adding the image as expected to the Media Library screen: Main media library screen with uploaded image appearing

However, when I click into that image and view the "Attachment Details" screen for that one item, it shows a default image instead.

Default image shown in attachment details

This doesn't happen on items I manually uploaded to the media library (through WordPress admin). There it shows the actual uploaded image in Attachment Details.

How can I get this function to apply the correct image in the Attachment Details view?

Note that these images uploaded through the GravityForms form should not have parent posts - they should be unattached to posts.

WordPress 5.9.3

Relevant plugins:

  1. Gravity Forms
  2. Media Library Assistant
  3. Advanced Custom Fields

1 Answer 1

2
+50

Hello Mike can you check that folder is the image is there which you have uploaded and if yes then when you click on attachment details then on right there is a key which shows File URL: can you check that.if it is empty or wrong path to show.

If file is not there you can use wp_upload_bits to move the file as wp_insert_attachment not move the files automatically.

If still not works can you check your wp_posts and wp_postmeta table for that particular attachment for correct path.

I have tried this code and it successfully works please check

    // set filename
    $upload_path = GFFormsModel::get_upload_path( $entry[ 'form_id' ] );
    $upload_url = GFFormsModel::get_upload_url( $entry[ 'form_id' ] );
    $filename_verbose = str_replace( $upload_url, $upload_path, $entry[ '1' ] );
    $filename_backslashes = trim( $filename_verbose, ' "[] ');
    $filename = stripslashes( $filename_backslashes );

    // check the type of file. We'll use this as the 'post_mime_type'
    $filetype = wp_check_filetype( basename( $filename ), null );

    // Get the path to the upload directory.
    $wp_upload_dir = wp_upload_dir();

    // Prepare an array of post data for the attachment.
    $attachment = array(
        'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ),
        'post_mime_type' => $filetype['type'],
        'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );

    // create a file in the upload folder
    $upload = wp_upload_bits( basename ( $filename ), null,  file_get_contents( $filename ));

    // Insert the attachment.
    $attach_id = wp_insert_attachment( $attachment, $upload['file'] );

    // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );

    // Generate the metadata for the attachment, and update the database record.
    $attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] );
    wp_update_attachment_metadata( $attach_id, $attach_data );

If still there is issue please post your post and post meta result with Admin file url.

1
  • Looks as though the Media Library was showing a thumbnail image that was actually in the Gravity Forms upload directory as in http://localhost:10003/wp-content/uploads/gravity_forms/1-e4b3aa502d53718b29bfb5e557eb1256/2022/04/Mike-Eng10.png , and it hadn't actually been moved into the WordPress uploads directory. This code basically worked to upload files to the WordPress uploads directory, but I had to modify it to strip backslashes out of $filename first. I suggested an edit to the answer with the code that worked for me.
    – Mike Eng
    Apr 23, 2022 at 2:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.