0

I have created some custom post forms that I'm now using in place of a plugin called WP User Frontend which allows you to post and edit from front end. These post forms we're working great but when I deactivated the plugin I now I get a warning when the post completes its' upload saying..

Warning: Cannot modify header information - headers already sent by (output started at /home/####/public_html/#####/wp-content/themes/####/header-default.php:2) in /home/#####/public_html/#####/wp-includes/pluggable.php on line 881.

Now I checked for white spaces and I honestly don't see any in the header, page template, or footer and I don't believe it has anything to with that because the post forms work fine when the plugin is activated. I'm also using buddypress which has a form submit for avatars and that works fine without plugin activated so I'm thinking my form has something missing that the plugin was providing. Instead of pasting the entire code including the html form I'm going to show the php part of the template that does the work.

<?php// Template Name: Album Post Form ?>
<?php get_header(); ?>

<?php if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {
if ( ! function_exists( 'wp_handle_upload' )) {
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
$file=$_FILES;
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
    $title =  $_POST['title'];
} else {
    echo 'Please enter a game  title';
}
if (isset ($_POST['description'])) {
    $description = $_POST['description'];
} else {
    echo 'Please enter the content';
}
$tags = $_POST['post_tags'];

// Add the content of the form to $post as an array
$new_post = array(
    'post_title'    => $title,
    'post_content'  => $description,
    'tags_input'    => array($tags),
    'post_status'   => 'publish',           // Choose: publish, preview, future, draft, etc.
    'post_type' => fod_albums  // Use a custom post type if you want to
);
//save the new post and return its ID
$pid = wp_insert_post($new_post);
if (!($file['album_image']['name'] == "")) {
  $cover_art_id = media_handle_sideload( $file['album_image'], $pid );
  if ( is_wp_error($cover_art_id) ) {
    @unlink($file_array['tmp_name']);
    return $cover_art_id;
  }
  if(!is_wp_error($cover_art_id)){
   wp_set_object_terms( $cover_art_id, 'cover_art', 'category');
  }
  update_post_meta($pid,'album_cover',$cover_art_id);
} elseif ($file['album_image']['name'] == "" && !($_POST['cover_radio'] == '')) {
  update_post_meta($pid,'music_art',$_POST['cover_radio']);
}
wp_redirect( get_permalink($pid)); 
exit();
} 
do_action('wp_insert_post', 'wp_insert_post');
?>

Any ideas?

1 Answer 1

2

You can't do this:

wp_redirect( get_permalink($pid));

after you've already sent content to the browser. Headers were already sent, and wp_redirect is attempting to send a header again. The first three lines of the template you pasted above are all sending content to the browser, when you close and open php. evidently your header.php file is doing the same.

You should hook an action to process your form before the template is loaded.

function wpa63889_process_form( $query ) {
    if ( $query->is_page( 'my-form-page' ) && isset( $_POST['title'] ) ) {
        // process form data
    }
}
add_action( 'pre_get_posts', 'wpa63889_process_form' );
6
  • Out of curiosity why did this work when the plugin was activated? Sep 2, 2012 at 17:11
  • And sorry but could you elaborate on how to hook an action to process the form? Sep 2, 2012 at 17:16
  • Were you using this same template as-is when the plugin was activated? I'd guess that the wp_redirect line was never reached. see edit above.
    – Milo
    Sep 2, 2012 at 17:25
  • No I created a completely custom post form that has no ties to the plugin. It had something in it though that was making this process work. Bizarre to me Sep 2, 2012 at 17:34
  • the only way it would work is if that offending line was not processed, the first if condition was not met and your code was skipped.
    – Milo
    Sep 2, 2012 at 17:42

Your Answer

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

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