6

Basically I want to display a form on my blog (on a certain page) that will allow anyone to fill it out and it will create a post in a custom post type.

I saw the answer once before but I can't find it now.

  • 1
    I know you probably want to build this yourself, but I recently started working with a plugin called Formidable that does this really well. There's a free version, you may want to check it out. wordpress.org/extend/plugins/formidable – Dalton Jan 24 '12 at 20:13
  • I tried formidable. But the free version have limitations. Date, time, hidden fields etc are locked. – Eugine Joseph Jan 29 '17 at 11:27
6

posting from the front-end is a matter of displaying a form and processing it:

form:

<!-- New Post Form -->
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="">

<!-- post name -->
<p><label for="title">Title</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="title" />
</p>

<!-- post Category -->
<p><label for="Category">Category:</label><br />
<p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?></p>


<!-- post Content -->
<p><label for="description">Content</label><br />
<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
</p>

<!-- post tags -->
<p><label for="post_tags">Tags:</label>
<input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>
<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>

<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
</div>

the processing:

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';
    }
    $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' => 'post_type_name'  //'post',page' or use a custom post type if you want to
    );
    //save the new post
    $pid = wp_insert_post($new_post); 
    //insert taxonomies
}
  • 1
    @Bainternet - Thanks, now is there a way to have the form redirect to the new post after they submit it? – Jared Mar 6 '11 at 10:45
  • yah wp_redirect(get_permalink($pid)); exit; – Bainternet Mar 6 '11 at 10:55
  • @Bainternet - Thanks again, but when i place that after the wp_insert_post function, I get an error that says cannot modify headers. Where do I put that? – Jared Mar 6 '11 at 20:37
  • 2
    This is how you should laid out the code: pastebin.com/SRi2ZxWa – ariefbayu Mar 8 '11 at 22:56
  • 1
    @Jared silent is right the processing part needs to come before the form – Bainternet Mar 8 '11 at 23:07
0

It might be simpler to use a plugin to do this.

You could also use the excellent Contact Form 7 plugin along with the Post My CF7 Form extension plugin which will allow you to save any custom forms to a custom post, including images as featured attachments, custom meta fields and select/checkbox/radio inputs as taxonomies.

The Post My CF7 Form plugin has a rich functionality that can be leveraged to further customise and tweak the way your forms should be saved. There is a detailed documentation section too.

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