php 将Gravity表单文件上传字段映射到ACF用户头像图像字段(Wordpress)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 将Gravity表单文件上传字段映射到ACF用户头像图像字段(Wordpress)相关的知识,希望对你有一定的参考价值。

function themeprefix_save_gf_upload_to_acf_image($entry, $form) {
  // If the ID of the form being saved isn't the one we're looking for, bail.
  $forms_with_class = themeprefix_get_form_ids_by_form_class('edit-profile');

  if ( $forms_with_class[0] != $entry['form_id']) {
    return;
  }

  // If we don't have the ID of the user, bail.
  if (!isset($entry['created_by'])) {
    return;
  }
  
  $gf_field_id = null;

  $gf_form_fields_classes = wp_list_pluck($form['fields'], 'id', 'cssClass');
  $gf_field_id = (int) $gf_form_fields_classes['admin-user-avatar'];
  // If there are no fields with this class, bail.
  if (!$gf_field_id) {
    return;
  }

  $acf_field_id       = 'field_9rhzquouy1ayl';
  $file_url           = $entry[$gf_field_id];
  $relative_file_path = parse_url( $file_url, PHP_URL_PATH );
  $absolute_file_path = untrailingslashit( ABSPATH ) . $relative_file_path;
  $file_type          = wp_check_filetype( $absolute_file_path )['type'];
  $timeout_seconds    = 5;

  // Sideload media file.
  $sideload_result = themeprefix_sideload_media_file( $file_url, $file_type, $timeout_seconds );

  // If an error occurred while trying to sideload the media file; continue to next blog.
  if ( ! $sideload_result || ! empty( $sideload_result['error'] ) ) {
    return;
  }

  $new_file_path = $sideload_result['file'];
  $new_file_type = $sideload_result['type'];

  // Insert file into the media library.
  $attachment_id = themeprefix_insert_media_file( $new_file_path, $new_file_type );

  if ( $attachment_id ) {
    // Update the ACF field to reference the newly uploaded file.
    update_field( $acf_field_id, $attachment_id, 'user_'.$entry['created_by'] );
  }

  // Delete the original file from the Gravity Forms upload directory.
  unlink( $absolute_file_path );
}

add_action( 'gform_after_submission', 'themeprefix_save_gf_upload_to_acf_image', 10, 2 );



function themeprefix_sideload_media_file( $file_url, $file_type, $timeout_seconds ) {
  // Gives us access to the download_url() and wp_handle_sideload() functions.
  require_once( ABSPATH . 'wp-admin/includes/file.php' );
  // Download file to temp dir.
  $temp_file = download_url( $file_url, $timeout_seconds );
  if ( is_wp_error( $temp_file ) ) {
    return false;
  }
  // Array based on $_FILE as seen in PHP file uploads.
  $file = array(
    'name'     => basename( $file_url ),
    'type'     => $file_type,
    'tmp_name' => $temp_file,
    'error'    => 0,
    'size'     => filesize( $temp_file ),
  );
  $overrides = array(
    // Tells WordPress to not look for the POST form
    // fields that would normally be present, default is true,
    // we downloaded the file from a remote server, so there
    // will be no form fields.
    'test_form'   => false,
    // Setting this to false lets WordPress allow empty files – not recommended.
    'test_size'   => true,
    // A properly uploaded file will pass this test.
    // There should be no reason to override this one.
    'test_upload' => true,
  );
  // Move the temporary file into the uploads directory.
  return wp_handle_sideload( $file, $overrides );
}



function themeprefix_insert_media_file( $file_path = '', $file_type = '' ) {

  if ( ! $file_path || ! $file_type ) {
    return false;
  }

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

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

  // Insert the attachment.
  $inserted_attachment_id   = wp_insert_attachment( $attachment_data, $file_path );
  $inserted_attachment_path = get_attached_file( $inserted_attachment_id );

  // Update the attachment metadata.
  themeprefix_update_inserted_attachment_metadata( $inserted_attachment_id, $inserted_attachment_path );

  return $inserted_attachment_id;
}



function themeprefix_update_inserted_attachment_metadata( $inserted_attachment_id, $file_path ) {

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

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

以上是关于php 将Gravity表单文件上传字段映射到ACF用户头像图像字段(Wordpress)的主要内容,如果未能解决你的问题,请参考以下文章

php Gravity Perks // GP嵌套表单//通过用户注册附加组件映射完整的子项数据

Gravity Forms 文件上传导致 WordPress Admin 出现 404

php Gravity Wiz // Gravity Forms //将GF数据填充到WP Job Manager自定义字段中

php 将Gravity Forms添加到单个AgentPress列表帖子中,并使用帖子标题动态填充表单。将其添加到functions.php中。

如何在节点js中使用单个api保存文本字段值以及文件上传?

MVC:如何将文件上传和其他表单字段发布到一个操作