使用gravity表单作为前端文件加载程序,用户可以通过表单上载库图像,然后将其保存到ACF(高级自定义表单)库字段。
它在一定程度上起作用,表单上载图像并将其存储在重力表单条目中(您可以很好地查看图像)。
下面代码的第二部分尝试将gravity表单附件中的图像作为后期附件移动到媒体库中
这就是它失败的地方,留下的是图像文件名,但图像已损坏(大小为0kb)。
可以在这里找到原始代码,但我不知道问题出在哪里:https://joshuadnelson.com/connect-gravity-forms-file-upload-to-acf-gallery-field/
/**
* Attach images uploaded through Gravity Form to ACF Gallery Field
*
* @return void
*/
$gravity_form_id = 10; // gravity form id, or replace {$gravity_form_id} below with this number
add_filter( "gform_after_submission_{$gravity_form_id}", \'jdn_set_post_acf_gallery_field\', 10, 2 );
function jdn_set_post_acf_gallery_field( $entry, $form ) {
$gf_images_field_id = 69; // the upload field id
$acf_field_id = \'field_59e260b6e28fd\'; // the acf gallery field id
// get post, if there isn\'t one, bail
if( isset( $entry[\'post_id\'] ) ) {
$post = get_post( $entry[\'post_id\'] );
if( is_null( $post ) )
return;
} else {
return;
}
// Clean up images upload and create array for gallery field
if( isset( $entry[ $gf_images_field_id ] ) ) {
$images = stripslashes( $entry[ $gf_images_field_id ] );
$images = json_decode( $images, true );
if( !empty( $images ) && is_array( $images ) ) {
$gallery = array();
foreach( $images as $key => $value ) {
// NOTE: this is the other function you need: https://gist.github.com/joshuadavidnelson/164a0a0744f0693d5746
if( ! class_exists( \'JDN_Create_Media_File\' ) )
break;
// Create the media library attachment and store the attachment id in the gallery array
$create_image = new JDN_Create_Media_File( $value, $post->ID );
$image_id = $create_image->attachment_id;
if( absint( $image_id ) ) {
$gallery[] = $image_id;
}
}
}
}
// Update gallery field with array
if( ! empty( $gallery ) ) {
update_field( $acf_field_id, $gallery, $post->ID );
}
}
__
<?php
class JDN_Create_Media_File {
var $post_id;
var $image_url;
var $wp_upload_url;
var $attachment_id;
/**
* Setup the class variables
*/
public function __construct( $image_url, $post_id = 0 ) {
// Setup class variables
$this->image_url = esc_url( $image_url );
$this->post_id = absint( $post_id );
$this->wp_upload_url = $this->get_wp_upload_url();
$this->attachment_id = $this->attachment_id ?: false;
return $this->create_image_id();
}
/**
* Set the upload directory
*/
private function get_wp_upload_url() {
$wp_upload_dir = wp_upload_dir();
return isset( $wp_upload_dir[\'url\'] ) ? $wp_upload_dir[\'url\'] : false;
}
/**
* Create the image and return the new media upload id.
*
* @see https://gist.github.com/hissy/7352933
*
* @see http://codex.wordpress.org/Function_Reference/wp_insert_attachment#Example
*/
public function create_image_id() {
if( $this->attachment_id )
return $this->attachment_id;
if( empty( $this->image_url ) || empty( $this->wp_upload_url ) )
return false;
$filename = basename( $this->image_url );
$upload_file = wp_upload_bits( $filename, null, file_get_contents( $this->image_url ) );
if ( ! $upload_file[\'error\'] ) {
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_parent\' => $this->post_id,
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', $filename),
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
);
$attachment_id = wp_insert_attachment( $attachment, $upload_file[\'file\'], $this->post_id );
if( ! is_wp_error( $attachment_id ) ) {
require_once( ABSPATH . "wp-admin" . \'/includes/image.php\' );
require_once( ABSPATH . \'wp-admin/includes/media.php\' );
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file[\'file\'] );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
$this->attachment_id = $attachment_id;
return $attachment_id;
}
}
return false;
} // end function get_image_id
}