Meta Boxes in Front End Post Submission Form

You can make a form that has an action of your form handling php script. In the php script, you can check if the form was submitted, and if there is an action, if so, then create variables for the form fields using the $_POST var. Then create the $post = array() to set up the post meta array, and use the wp_insert_post($post); to insert the post.

Here is an example of a form processing code I use for the frontend post form used on http://WPHonors.com. It works for the custom post type I created for it, which had a basic form set up, with a hidden post_type field with the post type as the value.

if( $_POST['post_type'] == 'site' ) {

    $title = $_POST['title'];
    $desc = $_POST['description'];
    $siteurl = $_POST['siteurl'];
    $cat = array( $_POST['cat'] );
    $tags = trim( $_POST['tags'] );

    if(!isset($title)) { echo '<div class="error">Title required.</div>'; }
    if(!isset($desc)) { echo '<div class="error">Description required.</div>'; }
    if(!isset($siteurl)) { echo '<div class="error">URL required.</div>'; }
    if($cat == -1) { echo '<div class="error">Select a category.</div>'; }
    if(!isset($tags)) { echo '<div class="error">Must use at least one tag.</div>'; }

    if (!current_user_can( 'publish_posts')) { $poststatus="draft"; } else { $poststatus="publish"; } 

    //$insert = new TypeSites();
    $post = array(
        'post_title'    => $title,
        'post_content'  => $desc,
        'siteurl'   => $siteurl,
        'post_category' => $cat,
        'tags_input'    => $tags,
        'post_status'   => $poststatus,
        'post_type' => 'site'
    );
    wp_insert_post( $post );
}

Leave a Comment