Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 } ?>
share|improve this question
    
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 '12 at 18:56
    
yes, a new post using the forms field values. –  Anders Kitson Jan 25 '12 at 19:04
add comment

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

share|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 '12 at 19:38
    
See my revised answer. –  Shane Jan 25 '12 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 '12 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 '12 at 21:04
1  
I just tested wp_insert_post and add_post_meta in page.php, works fine for me. Did you look at the codex pages I linked to, they have better examples than I do. –  Shane Jan 26 '12 at 13:58
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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