Support » Fixing WordPress » Using add_post_meta in form not creating custom meta after form submits

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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']);
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Using add_post_meta in form not creating custom meta after form submits’ is closed to new replies.