I have been trying this code to submit front-end post: http://pastebin.com/B8LGhsGA

It is not redirecting upon clicking the submit button the form works fine but its not redirecting. When the post is submitted it loads the same page again.

2 Answers 2


That's beacuse you are calling wp_redirect after you have some buffer output from your code. you should change the order of the template functionally, meaning that first check if the form has been submitted and then show the page, so try something like this:

Template Name: Submit Work

// Check if the form was submitted
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {

        // 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 = htmlentities(trim(stripcslashes($_POST['description']))); 
    } else {
        echo 'Please enter the content';

        // Add the content of the form to $post as an array
        $type = trim($_POST['Type']);
        $post = array(
                'post_title'    => $title,
                'post_content'  => $description,

                'post_status'   => 'pending',                     // Choose: publish, preview, future, etc.
                'post_type'     => 'work',  // Use a custom post type if you want to
                'tax_input'    => array( $type)
        $post_id = wp_insert_post($post);
            if ($_FILES) {
                foreach ($_FILES as $file => $array) {
                    $newupload = insert_attachment($file,$post_id);
                    // $newupload returns the attachment id of the file that
                    // was just uploaded. Do whatever you want with that now.
        wp_redirect( 'http://localhost/buddypress/thank-you' );
} // end IF

<?php get_header() ?>

    <div id="content" class="two_column">
        <div class="padder">
  <h1 class="page_title">Works<span class="work_logo"></span></h1>      

        <?php do_action( 'bp_before_blog_page' ) ?>

        <div class="page" id="blog-page" role="main">
        <h2 class="posttitle">Submit Work</h2>

<div id="postBox">

        <form id="new_post" name="new_post" class="post_work" method="post" enctype="multipart/form-data">
                <p><label for="title">Title</label><br />
                <input type="text" id="title" class="required" value="" tabindex="1" size="20" name="title" />
                <p><label for="description">Description</label><br />
                <textarea id="description" type="text" class="required" tabindex="3" name="description" cols="50" rows="6"></textarea>
                <p align="right"><input type="submit" value="Submit" tabindex="6" id="submit" name="submit" /></p>
                <p class="post_category"><label for="category">Category</label>
                <?php wp_dropdown_categories('taxonomy=Type&hide_empty=0&orderby=name&order=asc&name=Type') ?></p>

                <div style="clear:both;"></div>
<p><label for="attachment">Photos: </label><input type="file" id="attachment">
<div id="attachment_list"></div></p>

                <input type="hidden" name="post_type" id="post_type" value="domande" />
                <input type="hidden" name="action" value="post" />

                <?php wp_nonce_field( 'new-post' ); ?>

    var multi_selector = new MultiSelector( document.getElementById( 'attachment_list' ), 8 );
    multi_selector.addElement( document.getElementById( 'attachment' ) );


        </div><!-- .page -->

        <?php do_action( 'bp_after_blog_page' ) ?>

        </div><!-- .padder -->
    </div><!-- #content -->

    <?php locate_template( array( 'sidebar-work.php' ), true ) ?>

<?php get_footer(); ?>
  • If this is done in a plugin, how would I attach to an earlier event after my form is POST'd?
    – clockwiseq
    Commented May 26, 2013 at 22:20

Two things, 1 wp_redirect() will not be called when the page is already started, so you have to call it earlier. 2 wp_redirect() needs exit; after the function call.

wp_redirect( $location, $status );

You can read about it here http://codex.wordpress.org/Function_Reference/wp_redirect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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