wp_redirect “headers already sent” after front-end submission form

As Otto pointed out, you’re sending data to the browser before calling wp_redirect().

  • get_header() will output the page’s HTML <head>
  • You’re printing the entire form to the page before processing it.

To fix the “headers already sent” issue, you need to move all of your form processing from the bottom of the page to the top of the page. If you need to call wp_redirect() you must make that call before you print anything – HTML or anything else – to the page.

As for the duplicate post entry, you have wp_insert_post() twice in your code:

if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) {
    if ( trim( $_POST['postTitle'] ) === '' ) {
        $postTitleError="Please enter a title.";
        $hasError = true;
    }
    $post_information = array(
        'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
        'post_content' => wp_kses_post( $_POST['postcontent'] ),
        'post_type' => 'post',
        'post_status' => 'pending'                                      
    );
    wp_insert_post( $post_information );                    
}

$post_id = wp_insert_post( $post_information );

So you’re attempting to insert a post on every page load but if nothing has been submitted the $post_information variable won’t be set and nothing gets inserted. However, when you submit the form you’re creating a new post both inside the bool check and outside the bool check.