1
更新:好吧,现在自定义发布数据帖子正确..添加post_type =>'vacation'使这项工作...但现在我得到一个404错误提交。有任何想法吗?Wordpress在前端显示自定义帖子类型表格
我想添加一个前端表单到Wordpress中的任何页面/文章,但是这个表单并没有触发或保存任何数据。提交后不会显示感谢信息(但使用[休假]简码可以很好地显示)。该帖子没有被添加到我的自定义帖子类型的“休假”区域的仪表板(这是工作正常)。有没有一种方法,我需要告诉它,我插入自定义帖子类型“休假”莫名其妙?我想念什么人?
// Add shortcode to front end for vacation input
add_shortcode('vacation', 'vacation_shortcode');
function vacation_shortcode() {
if($_POST['vacation']=="submit" && !empty($_POST['action'])) {
echo "Thanks for submitting your vacation request!";
}
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please add a description of your request!';
}
?><form method="post" name="vacation_form" action="" id="vacation_form" >
<input type="text" name="title" value="Title of vacation request" />
<input type="text" name="_simple_vacation_type" value="Reason for absence" />
<input type="hidden" name="vacation" value="submit" />
<input type="hidden" name="action" value="new_vacation" />
<input type="submit" value="Submit">
<?php wp_nonce_field('new_vacation'); ?>
</form>
<?php
}
function simple_vacation_add_post(){
if($_POST['vacation']=="submit" && !empty($_POST['action'])) {
$title = $_POST['title'];
$vacation_type = $_POST['_simple_vacation_type'];
//the array of arguments to be inserted with wp_insert_post
$new_post = array(
'post_title' => $title,
'post_type' =>'vacation',
'post_status' => 'publish'
);
//insert the the post into database by passing $new_post to wp_insert_post
$pid = wp_insert_post($new_post);
//we now use $pid (post id) to help add our post meta data
add_post_meta($pid, '_simple_vacation_type', $vacation_type, true);
}
}
add_action('init','simple_vacation_add_post');