首页 > 解决方案 > 如何处理包含单个文件的提交的重力表格

问题描述

我创建了一个简单的重力形式(id = 11),其中包含 2 个字段:

  1. 文本字段类型(名称 = “my_name”)

  2. 文件上传类型(我无法为其设置名称)

并且有我想在提交后用于处理此表单的代码。如何更改此代码段以处理提交的文件? 请假设我必须自己手动处理表格

$input_1 = $_POST['myname'];


//how to get sbmitted file?
//$file = ?


$array_of_inputs = array("input_1" => $input_1 , "file_name" => $file);


$gravity_submit_result = submit_in_gravity_form(11 , $array_of_inputs , $api_key , $private_key ,$web_url); 
if ($gravity_submit_result){
    $response['message'] = $body['response'];
    $response['status'] = 'ok';
}
else{
    $response['status'] = false;
}
exit(json_encode($response));}

此页面包含我的重力形式

标签: phpwordpressgravityforms

解决方案


为了解决这个问题,你可以按照这个代码

  add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
        function set_post_content( $entry, $form ) {

            //if the Advanced Post Creation add-on is used, more than one post may be created for a form submission
            //the post ids are stored as an array in the entry meta
            $created_posts = gform_get_meta( $entry['id'], 'gravityformsadvancedpostcreation_post_id' );
            foreach ( $created_posts as $post )
            {
                $post_id = $post['post_id'];
                $post = get_post( $post_id );
              require_once( ABSPATH . 'wp-admin/includes/image.php' );
              require_once( ABSPATH . 'wp-admin/includes/file.php' );
              require_once( ABSPATH . 'wp-admin/includes/media.php' );
              $attach_id = media_handle_upload('file', $post_id);
              if (is_numeric($attach_id)) {
                 update_option('option_image', $attach_id);
                 update_post_meta($post_id, '_my_file_upload', $attach_id);
}
                update_post_meta($post_id, 'myname', $_POST['myname'];);
            }
        }

推荐阅读