UPDATE: OK, now custom post data posts properly.. added post_type =>’vacation’ to make this work… but now I get a 404 error upon submit. Any ideas?
I’m trying to add a frontend form to any page/post in WordPress, but this form is not firing or saving any data. It won’t show the thank you message after submit (but the form does show fine using the [vacation] shortcode). The post doesn’t get added into my custom post type “vacation” area in the Dashboard (which is working fine). Is there a way I need to tell it that I’m inserting a custom post type “vacation” somehow? What am I missing anyone please?
// 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');
1 comment