ACF和重力表单文件上传前端

时间:2017-10-16 作者:Randomer11

使用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
}

2 个回复
SO网友:Dave from Gravity Wiz

更简单的选择是使用Gravity Forms Media Library 插件。以下是功能的要点:

public function maybe_upload_to_media_library( $entry, $form ) {

    $has_change = false;

    foreach( $form[\'fields\'] as $field ) {

        if( ! $this->is_applicable_field( $field ) ) {
            continue;
        }

        $value = $entry[ $field->id ];

        if( $field->multipleFiles ) {
            $value = json_decode( $value );
        }

        if( empty( $value ) ) {
            continue;
        }

        $has_change = true;
        $ids        = $this->upload_to_media_library( $value, $field, $entry );
        $new_value  = array();

        if( is_wp_error( $ids ) ) {
            continue;
        }

        foreach( $ids as $id ) {
            if( ! is_wp_error( $id ) ) {
                $new_value[] = wp_get_attachment_url( $id );
            }
        }

        if( $field->multipleFiles ) {
            $new_value = json_encode( $new_value );
        } else {
            $new_value = $new_value[0];
            $ids       = $ids[0];
        }

        $entry[ $field->id ] = $new_value;

        $this->update_file_ids( $entry[\'id\'], $field->id, $ids );

    }

    if( $has_change ) {
        GFAPI::update_entry( $entry );
    }

    return $entry;
}
但是。。。它还处理大量您可能没有考虑的其他用例,例如更新条目、多文件上载以及与ACF各种基于图像的自定义字段类型的自动集成。

SO网友:Marcelo Henriques Cortez

我遇到了一个类似的问题,并使用“media\\u handle\\u upload”找到了解决具体问题的方法(https://codex.wordpress.org/Function_Reference/media_handle_upload).

首先,确保表单具有属性“enctype=”multipart/form data“。

第二,确保“input=file”没有value属性。

第三,使用“media\\u handle\\u upload”并传递“input=file”的名称。

Forth-例如,检查“is\\U wp\\U error”是否有错误。

第五,使用要更新的字段名称更新用户元(在我的情况下,与“input=file”的“name”相同)。

我的问题和完整答案如下:ACF Upload Image in front-end with custom form

希望我的代码可以帮助您解决您的问题。。。也许可以,你只需要适应它。

结束

相关推荐

ACF和重力表单文件上传前端 - 小码农CODE - 行之有效找到问题解决它

ACF和重力表单文件上传前端

时间:2017-10-16 作者:Randomer11

使用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
}

2 个回复
SO网友:Dave from Gravity Wiz

更简单的选择是使用Gravity Forms Media Library 插件。以下是功能的要点:

public function maybe_upload_to_media_library( $entry, $form ) {

    $has_change = false;

    foreach( $form[\'fields\'] as $field ) {

        if( ! $this->is_applicable_field( $field ) ) {
            continue;
        }

        $value = $entry[ $field->id ];

        if( $field->multipleFiles ) {
            $value = json_decode( $value );
        }

        if( empty( $value ) ) {
            continue;
        }

        $has_change = true;
        $ids        = $this->upload_to_media_library( $value, $field, $entry );
        $new_value  = array();

        if( is_wp_error( $ids ) ) {
            continue;
        }

        foreach( $ids as $id ) {
            if( ! is_wp_error( $id ) ) {
                $new_value[] = wp_get_attachment_url( $id );
            }
        }

        if( $field->multipleFiles ) {
            $new_value = json_encode( $new_value );
        } else {
            $new_value = $new_value[0];
            $ids       = $ids[0];
        }

        $entry[ $field->id ] = $new_value;

        $this->update_file_ids( $entry[\'id\'], $field->id, $ids );

    }

    if( $has_change ) {
        GFAPI::update_entry( $entry );
    }

    return $entry;
}
但是。。。它还处理大量您可能没有考虑的其他用例,例如更新条目、多文件上载以及与ACF各种基于图像的自定义字段类型的自动集成。

SO网友:Marcelo Henriques Cortez

我遇到了一个类似的问题,并使用“media\\u handle\\u upload”找到了解决具体问题的方法(https://codex.wordpress.org/Function_Reference/media_handle_upload).

首先,确保表单具有属性“enctype=”multipart/form data“。

第二,确保“input=file”没有value属性。

第三,使用“media\\u handle\\u upload”并传递“input=file”的名称。

Forth-例如,检查“is\\U wp\\U error”是否有错误。

第五,使用要更新的字段名称更新用户元(在我的情况下,与“input=file”的“name”相同)。

我的问题和完整答案如下:ACF Upload Image in front-end with custom form

希望我的代码可以帮助您解决您的问题。。。也许可以,你只需要适应它。

相关推荐