I'm trying to practice with setting up a front-end post submission form. And I'm getting this error:

Warning: Cannot modify header information - headers already sent by (output started at /home/sadlight/public_html/members/wp-content/themes/default-child/announcement-submit.php:1) in /home/sadlight/public_html/members/wp-includes/pluggable.php on line 876

3 Answers
3

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:

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.

In addition, it would be better to move the entire form processing into your functions file and hook into some action like template_redirect, where you are sure nothing has been sent to the browser yet.
–
Mateusz HajdzionyJan 14 '13 at 22:40

Thanks! This is something that I'd rather do to keep things separate. However, I've been searching for the past hour trying to get a better understanding of how to use template_redirect or what it actually does. I'm dumbfounded.
–
JoshJan 14 '13 at 23:59

Well, I separated it. And it worked! I'll make the $_POST names unique so they won't clash with anything else.
–
JoshJan 15 '13 at 0:10

headers already sent by (output started at
/home/sadlight/public_html/members/wp-content/themes/default-child/announcement-submit.php:1)

The error message tells you exactly what you need to know.

The "announcement-submit.php" is where the problem is.

The "1" is the line number of the problem.

So, you actually have something before your initial <?php line that is causing output. A blank line, perhaps. Or the UTF-8 BOM, perhaps.

Load the file in a plain text editor. Nothing fancy. Notepad, perhaps. Or VIM. Make sure nothing exists before that starting PHP tag. Save the file. Or, in your editor of choice, check the save options and make sure it's not set to save a BOM (Byte-Order-Marker) at the beginning of the file.

it's more than just space before the opening <?php, there's the whole get_header and all the markup output before the wp_redirect line half-way down the template.
–
MiloJan 14 '13 at 21:32

Thanks for your response. I tried what you suggested with just using notepad. I even ensured Notepad++ (my code editor) was saving UTF-8 without BOM. Re-uploaded the file... and now I have this error: Warning: Cannot modify header information - headers already sent by (output started at /home/sadlight/public_html/members/wp-content/plugins/buddypress/bp-themes/bp-de‌​fault/header.php:2) in /home/sadlight/public_html/members/wp-includes/pluggable.php on line 876 .... Line 2 of header.php is <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
–
JoshJan 14 '13 at 21:59

@Milo I removed all white space that I could that made sense... inbetween lines and after lines. Same error.
–
JoshJan 14 '13 at 22:00

There should be absolutely nothing at all before that initial php line. No space, no text, no code.
–
OttoJan 15 '13 at 4:36

This can have multiple causes. The headers can only be send when there is no output before it. And with output I mean any output. So no whitespaces before the PHP opening tags, no echos no BOM characters.

Basically that error message tells you exactly where the output is started so that is where one of above things is happening.

For a complete list of things you should check and also how to prevent this error please see this fine Stack Overflow post.