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've gone through the various issues with headers already sent, checking for white space and making sure that the get_header line appears after the wp_redirect but I'm stuck.

The code is:

<?php if ( !is_user_logged_in()) { 
        include (TEMPLATEPATH . '/member.php'); 
    } else { ?>
<?php
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';

    $tags = $_POST['post_tags'];

    // ADD THE FORM INPUT TO $new_post 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'   =>  'draft',           // Choose: publish, preview, future, draft, etc.
    'post_type' =>  'post',  //'post',page' or use a custom post type if you want to
    );

    //SAVE THE POST
    $pid = wp_insert_post($new_post);

    //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
    wp_set_post_tags($pid, $_POST['post_tags']);

    //REDIRECT TO THE NEW POST ON SAVE
    $link = include (TEMPLATEPATH . '/thankyou.php'); 
    wp_redirect( $link );

    //INSERT OUR MEDIA ATTACHMENTS
    if ($_FILES) {
        foreach ($_FILES as $file => $array) {
        $newupload = insert_attachment($file,$pid);
        }

    } // END THE IF STATEMENT FOR FILES

} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');

?>

<?php get_header(); ?>

    <div id="content" role="main">
        <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
            <div id="post-<?php the_ID(); ?>" class="post">
                <div class="title">
                    <h2>Post</h2>
                </div>
            <div class="form-content">

            <div>
                <form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">

                <fieldset name="name">
                    <label for="title">Title:</label>
                    <input type="text" id="title" value="" tabindex="5" name="title" />
                </fieldset>

                <fieldset class="tax">
                    <label for="cat">Category:</label>
                    <?php wp_dropdown_categories( 'tab_index=10&taxonomy=type&hide_empty=0' ); ?>
                </fieldset>

                <fieldset class="content">
                    <label for="description">Description:</label>
                    <textarea id="description" tabindex="15" name="description" cols="60" rows="10"></textarea>
                </fieldset>

                <fieldset class="images">
                    <label for="company_logo">Company Logo</label>
                    <input type="file" name="company_logo" id="company_logo" tabindex="25" />
                </fieldset>

                <fieldset class="submit">
                    <input type="submit" value="Post It" tabindex="40" id="psubmit" name="submit" />
                </fieldset>

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

                </form>
            </div>

            </div><!-- .entry-content -->
        </div><!-- #post-## -->

    <?php endwhile; // end of the loop. ?>
    </div><!-- #content -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php } ?> <!-- user is logged in -->

If the post status is set to draft and the redirect to thankyou.php, I get the headers already sent message. The post gets saved and the thankyou.php page is displayed but the headers already sent message appears.

If I set the post status to publish and the redirect to something like:

$link = get_permalink( $pid );
    wp_redirect( $link );
    exit;

Then the form works without a hitch and displays the published post.

I want to be able to review the post before it gets published, that's why I want the status sent to draft and a message to appear to the poster.

How do I accomplish this?

Debugging info:

Notice: Undefined index: post_tags in C:\xampp\htdocs\test\wp-content\themes\testtheme\postform.php on line 31
Notice: Undefined variable: description in C:\xampp\htdocs\test\wp-content\themes\testtheme\postform.php on line 48
Notice: Trying to get property of non-object in C:\xampp\htdocs\test\wp-includes\post.php on line 2915
Notice: Undefined index: mytheme_meta_box_nonce in C:\xampp\htdocs\test\wp-content\themes\testtheme\lib\metabox.php on line 177
share|improve this question
    
What should $link = include (TEMPLATEPATH . '/thankyou.php'); do? You're then calling wp_redirect( $link );, while what I guess you want to do is simply wp_redirect(TEMPLATEPATH . '/thankyou.php');, no? In addition, you have to call exit; after the redirect. And the code after the redirect (INSERT OF MEDIA ATTACHMENTS) won't be executed, anyway. –  t f Apr 3 '13 at 12:12
    
Please add debugging information. My guess is that you have a Warning echoing-- possibly the $tags = $_POST['post_tags']; line as you use that key without checking for its existence. Or maybe its the explicit echo just above that line. –  s_ha_dum Apr 3 '13 at 12:51
    
I've added some debugging info. How do I check for the existence of the key? –  annabwashere Apr 4 '13 at 9:08

2 Answers 2

try this.

 echo 'Please enter a title';

Remove this line & check your output.

share|improve this answer
    
Can you explain this better? I don't get it: add-remove-test shouldn't be add-test-remove? –  brasofilo Apr 3 '13 at 12:37
    
Is a common error which means you are basically sending request multiple times.so error occur. –  anjali Apr 3 '13 at 12:49
    
That is a possibility but not the only one. Claiming that "this will work" is a much stronger statement than the available evidence supports. Yes, that is sloppy, but my money is on the line following that one... but that is a guess too. –  s_ha_dum Apr 3 '13 at 12:55
    
I've updated the question with debugging info and it seems that s_ha_dum is on the right track. –  annabwashere Apr 4 '13 at 9:12

Use init hook...

<?php
function custom12345_wp_insert_post()
{
    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';

        $tags = $_POST['post_tags'];

        // ADD THE FORM INPUT TO $new_post 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'   =>  'draft',           // Choose: publish, preview, future, draft, etc.
        'post_type' =>  'post',  //'post',page' or use a custom post type if you want to
        );

        //SAVE THE POST
        $pid = wp_insert_post($new_post);

        //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
        wp_set_post_tags($pid, $_POST['post_tags']);

        //INSERT OUR MEDIA ATTACHMENTS
        if ($_FILES) {
            foreach ($_FILES as $file => $array) {
            $newupload = insert_attachment($file,$pid);
            }

        } // END THE IF STATEMENT FOR FILES

        //REDIRECT TO THE NEW POST ON SAVE
        $link = include (TEMPLATEPATH . '/thankyou.php'); 
        wp_redirect( $link );
        exit;

     } // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
    }
}
add_action('init','custom12345_wp_insert_post');
?>

<?php if ( !is_user_logged_in()) { 
        include (TEMPLATEPATH . '/member.php'); 
    } else { ?>
<?php get_header(); ?>
    <div id="content" role="main">
        <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
            <div id="post-<?php the_ID(); ?>" class="post">
                <div class="title">
                    <h2>Post</h2>
                </div>
            <div class="form-content">

            <div>
                <form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">

                <fieldset name="name">
                    <label for="title">Title:</label>
                    <input type="text" id="title" value="" tabindex="5" name="title" />
                </fieldset>

                <fieldset class="tax">
                    <label for="cat">Category:</label>
                    <?php wp_dropdown_categories( 'tab_index=10&taxonomy=type&hide_empty=0' ); ?>
                </fieldset>

                <fieldset class="content">
                    <label for="description">Description:</label>
                    <textarea id="description" tabindex="15" name="description" cols="60" rows="10"></textarea>
                </fieldset>

                <fieldset class="images">
                    <label for="company_logo">Company Logo</label>
                    <input type="file" name="company_logo" id="company_logo" tabindex="25" />
                </fieldset>

                <fieldset class="submit">
                    <input type="submit" value="Post It" tabindex="40" id="psubmit" name="submit" />
                </fieldset>

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

                </form>
            </div>

            </div><!-- .entry-content -->
        </div><!-- #post-## -->

    <?php endwhile; // end of the loop. ?>
    </div><!-- #content -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php } ?> <!-- user is logged in -->
share|improve this answer
1  
Some more explanatory context around the code would be helpful. Why should the OP hook into 'init'? –  Johannes Pille Apr 3 '13 at 13:41
    
init is called before anything is sent to output, so wraping code into init hook, you will get rid of header allready sent error.. –  Rajeev Vyas Apr 3 '13 at 13:45
    
I tried your code but it doesn't work. The thank you page doesn't load and the post isn't saved. –  annabwashere Apr 4 '13 at 7:41

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.