php 连接重力表单多文件上载和高级自定义字段重复文件字段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 连接重力表单多文件上载和高级自定义字段重复文件字段相关的知识,希望对你有一定的参考价值。

<?php
	 /**
	 * Connect Gravity Forms Multi File Upload and Advanced Custom Fields Repeating File-Field and Gallery Field
	 * REMEMBER: Set ID correctly for forms and fields AND set acf field names
	 */
	 add_action( 'gform_after_submission_4', 'add_form_after_submission', 10, 2 );
	 function add_form_after_submission( $entry, $form ) {

    	//getting post
    	$post = get_post( $entry['post_id'] );

	    if( $post != null ) {

			// Fotos, Gallery Field
			if( rgar( $entry, '30' ) != null && !empty(rgar( $entry, '30' ) ) ) {
                gravity_forms_acf_add_files_to_media_library( $post, $entry, '30', 'acf_production_gallery' ); // @TODO
            }

			// Dokumenter, Repeating File-Field
			if( rgar( $entry, '51' ) != null && !empty(rgar( $entry, '51' ) ) ) {
                gravity_forms_acf_add_files_to_media_library( $post, $entry, '51', 'acf_production_documents', 'acf_production_documents_file' ); // @TODO
            }
		}

		return;
	}

	function gravity_forms_acf_add_files_to_media_library( $post, $entry, $gravity_form_field_id, $acf_field_name, $acf_sub_field_name = null ) {
		$array_file_urls = stripslashes( rgar( $entry, $gravity_form_field_id ) );
	    $array_file_urls = json_decode( $array_file_urls, true );
	    add_files_to_media_library( $post, $array_file_urls, $acf_field_name, $acf_sub_field_name );
	}

	function add_files_to_media_library( $post, $array_file_urls, $acf_field_name, $acf_sub_field_name = null ) {

	    $acf_file_id_array = array();
		$wp_upload_dir = wp_upload_dir();

	    foreach( $array_file_urls as $key => $file_url ) {

	        // $file = rtrim( ABSPATH, '/' ) . parse_url( $file_url , PHP_URL_PATH );
	        $filename = basename( $file_url );
	        $upload_file = wp_upload_bits( $filename, null, file_get_contents( $file_url ) );

	        if ( !$upload_file['error'] ) {

	        	$wp_filetype = wp_check_filetype( $filename, null );
	        	$attachment = array(
					'guid' => $wp_upload_dir['url'] . '/' . $filename,
	        		'post_mime_type' => $wp_filetype['type'],
	        		'post_parent' => $post->ID,
	        		'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
	        		'post_content' => '',
	        		'post_status' => 'inherit'
	        	);

	        	$attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $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 );

					if( $acf_sub_field_name != null ) {
						array_push( $acf_file_id_array, array( $acf_sub_field_name => $attachment_id ) );
					} else {
						array_push( $acf_file_id_array, $attachment_id );
					}
	        	}


	        }
	    }

	    if( !empty( $acf_file_id_array ) ) {
	        update_field( $acf_field_name, $acf_file_id_array, $post->ID );
	    }
	}

以上是关于php 连接重力表单多文件上载和高级自定义字段重复文件字段的主要内容,如果未能解决你的问题,请参考以下文章

php 连接重力表单多文件上载和高级自定义字段重复文件字段

php 连接重力表单多文件上载和高级自定义字段重复文件字段

php 连接重力表单多文件上载和高级自定义字段重复文件字段

php 连接重力表单多文件上载和高级自定义字段重复文件字段

php 连接重力表单多文件上载和高级自定义字段重复文件字段

php 将重力表单文件上载到高级自定义字段WordPress中的图像/文件字段