Using add_post_meta in form not creating custom meta after form submits
-
I have a form which i’ve modified that includes two fields that should use add_post_meta but are not working. I’ve used the code from this answer on stack exchange http://wordpress.stackexchange.com/a/11289/10413 that works. It create a post with tags and all the fields you see.
I then found this tutorial which had some sample code for adding post meta http://www.odharma.com/2011/01/how-to-create-data-entry-forms-for-wordpress-with-contactform7/
My code is below which is not working, the post is created but not the meta data from the two form fields I added
$new_post = array( 'post_title' => $title, 'post_content' => $description, 'post_category' => array($_POST['cat']), // Usable for custom taxonomies too 'tags_input' => array($tags), 'post_status' => 'publish', // Choose: publish, preview, future, draft, etc. 'post_type' => 'website' //'post',page' or use a custom post type if you want to ); add_post_meta($new_post, 'custom1', $new_post['field-custom1']); add_post_meta($new_post, 'custom2', $new_post['field-custom2']);
The two custom fields are called custom1 and custom2. How can I get these to post with the form?
Thanks
-
the add_post_meta requires an ID, not the whole $post object, so basically you need to save that post THEN get the returned ID it gives you and add it to the add_post_meta
This is my updated code, it is now adding the custom keys but not inserting the values from the form fields, you’ll see near the bottom i’ve added. domain/keywords are the custom field names and form field names
update_post_meta($pid,'domain',$domain); update_post_meta($pid,'keywords',$keywords);
<?php /* Template Name: Test Form */ get_header(); if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post") { // Do some minor form validation to make sure there is content if (isset ($_POST['title'])) { $title = $_POST['title']; } else { echo 'Please enter a title'; } if (isset ($_POST['description'])) { $description = $_POST['description']; } else { echo 'Please enter the content'; } if (isset ($_POST['domain'])) { $description = $_POST['domain']; } else { echo 'Please enter the domain'; } if (isset ($_POST['keywords'])) { $description = $_POST['keywords']; } else { echo 'Please enter the keywords'; } $tags = $_POST['post_tags']; // Add the content of the form to $post as an array $new_post = array( 'post_title' => $title, 'post_content' => $description, 'post_category' => array($_POST['cat']), // Usable for custom taxonomies too 'tags_input' => array($tags), 'post_status' => 'publish', // Choose: publish, preview, future, draft, etc. 'post_type' => 'website' //'post',page' or use a custom post type if you want to ); //save the new post $pid = wp_insert_post($new_post); update_post_meta($pid,'domain',$domain); update_post_meta($pid,'keywords',$keywords); wp_redirect(get_permalink($pid)); exit; //insert taxonomies } ?>
I got it I didn’t update the variables
$description = $_POST['domain'];
and$description = $_POST['keywords'];
when I copied those functions it’s working now. Thanks again.. $description = $_post[‘keywords’]; .. should probably be $keywords = $_POST[‘keywords’]; .. same with domain? .. you’re not setting the $domain var or the $keywords var
Yes just saw that, thanks for the help
You really REALLY need to validate data on those inputs from $_POST as well
http://codex.wordpress.org/Data_Validation
like
$domain = esc_attr($_POST['domain']);
- The topic ‘Using add_post_meta in form not creating custom meta after form submits’ is closed to new replies.