php - 如何处理包含单个文件的提交的重力表格
问题描述
我创建了一个简单的重力形式(id = 11),其中包含 2 个字段:
文本字段类型(名称 = “my_name”)
文件上传类型(我无法为其设置名称)
并且有我想在提交后用于处理此表单的代码。如何更改此代码段以处理提交的文件? 请假设我必须自己手动处理表格
$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));}
解决方案
为了解决这个问题,你可以按照这个代码
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'];);
}
}
推荐阅读
- python - Scrapy Spider 不会刮任何东西
- c# - 如何知道点击了按钮数组的哪个按钮
- .net-core - Azure Function (v2, .NET Core):反序列化来自流分析作业的事件中心输入
- ubuntu - apt install libleptonica-dev 失败
- jenkins - 如何配置 webhooks.config?
- openlayers - 在 Openlayers 中加载 GPX 文件作为矢量源后如何获取特征集合(几何和元数据)
- postgresql - Spring Boot Hibernate 使用序列或身份生成实体 ID 的问题
- angular - Angular NgRx 的效果创建了一个无限循环
- javascript - tabulator.js 中的复选框列选择问题
- python - Apache Beam pubsub 到 bigquery 模式创建