admin管理员组

文章数量:1201974

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 

// 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:

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

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

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:

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

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
Share Improve this question edited Apr 22, 2022 at 2:07 Mike Eng asked Apr 19, 2022 at 18:56 Mike EngMike Eng 2783 silver badges22 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: