I am wanting to take the following form, and use $wpdb to insert it into a post. I have tried reading the class reference page INSERT rows, but I don't really know what should be referenced. I am not sure how to tell the form to insert the data into a post using $wpdb?

<?php
if (isset($_POST['pickup-form']))
{
    // Clean POST data
    $demo_input = (isset($_POST['demo-input'])) ? trim((string) $_POST['demo-input']) : '';

    // Validate POST data
    if ('' === $demo_input)
    {
        $errors['demo_input'] = __('Demo field is required');
    }

    // No errors
    if (empty($errors))
    {
        // Save POST data to database, send some e-mails, etc.
        $success = TRUE;
    }
}
?>

<?php if ( ! empty($success)) { ?>

    <p>Thank you!</p>

<?php } else { ?>

    <?php if ( ! empty($errors)) { ?>
        <?php print_r($errors) ?>
    <?php } ?>

    <form method="post" action="">
        <input type="hidden" name="pickup-form">
        <input type="text" name="demo-input" value="<?php if (isset($demo_input)) { echo esc_attr($demo_input); } ?>">
        <input type="submit">
    </form>

<?php } ?>
link|improve this question

65% accept rate
What do you mean by "insert the form into a post", you mean inserting a NEW post using the form's field values? – Shane Jan 25 at 18:56
yes, a new post using the forms field values. – Anders Kitson Jan 25 at 19:04
feedback

1 Answer

In the success part of your code, you can build an array representing a post, and use wp_insert_post as such :

Example

 $mypost = array(
      'post_title' => 'My Title',
      'post_type' => 'page'
      //... add other fields according to your form
 );

 $mypost_id = wp_insert_post( $mypost ); //Returns new post id on success

Any field you don't specify will be filled by WordPress automatically.

EDIT

For custom fields, see add_post_meta :

 $mypost_id = wp_insert_post( $mypost ); //SEE ABOVE
 $meta_key = 'your-new-field-name';
 $meta_value = 'your-form-value';
 $unique = true; // or false     

 add_post_meta( $mypost_id, $meta_key, $value, $unique );

Source : wp_insert_post

link|improve this answer
what would I put if I wanted to insert the form field values into a custom fields. I guess I would also have to create those custom fields not sure how to do that either. – Anders Kitson Jan 25 at 19:38
See my revised answer. – Shane Jan 25 at 19:44
sorry if I am a little bit slow, I am not sure what to place in the $meta_key and $meta_value – Anders Kitson Jan 25 at 20:58
@AndersKitson $meta_key is the name of your custom field, $meta_value is it's value. Example, if you wanted to add a custom field 'subtitle' for a post : add_post_meta( $post_id, 'subtitle', 'This is my subtitle text', true ); – Shane Jan 25 at 21:04
so this is how I create a custom field in the admin page. when I place this code in my custom-page.php file it doesn't show. – Anders Kitson Jan 25 at 21:43
show 1 more comment
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.