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 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.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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:

<?php
/*
Template Name: Submit Work
*/
?>
<?php

// 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);
        wp_set_post_terms($post_id,$type,'Type',true);
            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' );
        exit();
} // 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>

<!--SUBMIT POST-->
<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>
                <p><label for="description">Description</label><br />
                <textarea id="description" type="text" class="required" tabindex="3" name="description" cols="50" rows="6"></textarea>
                </p>
                <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' ); ?>
        </form>

</div>
<script>
    var multi_selector = new MultiSelector( document.getElementById( 'attachment_list' ), 8 );
    multi_selector.addElement( document.getElementById( 'attachment' ) );
</script>

<!--SUBMIT POST END-->

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

        <?php do_action( 'bp_after_blog_page' ) ?>

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

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

<?php get_footer(); ?>
share|improve this answer
    
Excellent. Working Fine now! :) Thanks. –  Towfiq Jan 26 '12 at 16:46
    
If this is done in a plugin, how would I attach to an earlier event after my form is POST'd? –  ClockwiseQ May 26 '13 at 22:20
add comment

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 );
exit;

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

share|improve this answer
add 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.