【发布时间】:2016-10-28 19:04:32
【问题描述】:
我终于设法让 wordpress 前端表单正常工作,创建自定义帖子类型帖子并填写各种自定义字段。但是由于我集成了媒体上传,因此表单提交仅在附加文件时才有效。但是媒体上传不应该是强制性的,它应该是可选的。欢迎提出任何建议!
// Formular Verloren
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post") {
$post_information = array(
'post_type' => 'verloren',
);
$postID = wp_insert_post( $post_information ); //here's the catch
//Image Upload
if (!function_exists('wp_generate_attachment_metadata')){
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
return "upload error : " . $_FILES[$file]['error'];
}
$attach_id = media_handle_upload( $file, $postID );
}
}
if ($attach_id > 0){
//and if you want to set that image as Post then use:
update_post_meta($postID,'_thumbnail_id',$attach_id);
}
//Custom Field Updates
update_field('field_580a640d6e458', $_POST['brand'], $postID);
update_field('field_580a644ddc37f', $_POST['modell'], $postID);
update_field('field_580a60c14f201', $_POST['date'], $postID);
update_field('field_580a6191738cd', $_POST['spot'], $postID);
...
//Redirect to Page
wp_redirect(get_permalink(100)); exit;
}
这是我的提交按钮,如果相关的话:
<button id="submit" class="btn" name="submit" type="submit" value="Publish">Submit</button>
<input name="action" type="hidden" value="new_post" />
【问题讨论】:
标签: php wordpress forms wordpress-theming