【发布时间】:2018-08-30 22:32:06
【问题描述】:
我正在使用 flamingo 插件保存联系表单提交。我还使用以下代码将表单上传的文件保存为 WordPress 附件:
//Save CF7 data
function cf7_create_post($WPCF7_ContactForm) {
//In case you wanna check for a especific form
/* $form_id = $data->id;
if (224 == $WPCF7_ContactForm->id()) { */
//Get current form
$wpcf7 = WPCF7_ContactForm::get_current();
// get current SUBMISSION instance
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$formData = $submission->get_posted_data(); // Get all data from the posted form
$uploaded_files = $submission->uploaded_files(); // this allows you access to the upload file in the temp location
}
// We need to get the CF7 field name from FILE
$cf7_file_field_name = 'file-cv'; // [file uploadyourfile]
//Do the magic the same as the refer link above
$image_name = $formData[$cf7_file_field_name];
$image_location = $uploaded_files[$cf7_file_field_name];
$image_content = file_get_contents($image_location);
$wud = wp_upload_dir();
$upload = wp_upload_bits($image_name, null, $image_content);
$chemin_final = $upload['url'];
$filename = $upload['file'];
if ($filename > '') {
require_once(ABSPATH . 'wp-admin/includes/admin.php');
$wp_filetype = wp_check_filetype(basename($filename), null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $filename); // $newpostid optional
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
$attach_url = get_attached_file( $attach_id );
}
}
add_action('wpcf7_before_send_mail', 'cf7_create_post', 10);
我想将附件的 URL 保存在 flamingo 中(将其添加到已发布的数据中),当我使用 ‘wpcf7_before_send_mail’ 过滤器时,‘wpcf7_posted_data’ 过滤器已被触发,无法再修改已发布的数据。
我尝试使用‘wpcf7_posted_data’ 过滤器本身将文件保存为附件(我可以在其中添加带有 URL 的字段,它将显示在 flamingo 帖子上),但是当‘wpcf7_posted_data’ 过滤器被触发时,上传的文件不是在发布的数据或uploaded_files() 方法中可用。
我想我以后可以通过修改 flamingo 帖子来实现购买,但要确定如何获取它的信息以及它是否是一个好的解决方案。 谢谢
【问题讨论】:
-
你好拉米,你找到解决办法了吗?
-
嗨@LucasBarros,很遗憾没有。我最终使用了我自己的自定义帖子类型。
标签: wordpress contact-form-7 wordpress-flamingo-plugin