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.

Im trying to make front end posting page but, sometime 'wp_redirect' is not working with 'cannot modify header information' error. adding data is working well but I don't know why is working well without error sometime, showing error sometime..

Sorry for my bad english, please help me out... and I'm not a programmer just understand some codes anyway...

Many thanks!!

    if (isset ($_POST['post_title'])) {
        $title =  $_POST['post_title'];
    } else {
        echo 'Please enter a game  title';
    }
    if (isset ($_POST['description'])) {
        $description = $_POST['description'];
    } else {
        echo 'Please enter the content';
    }

    $new_post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'post_status'   => 'publish',
        'post_type' => 'property',  
        'tax_input' => array( 'property_type' => array($property_type), 'suburbs' => array($suburbs) ) 
    );


    //save the new post and return its ID
    $pid = wp_insert_post($new_post); 

       //add thumbnail
        if (!function_exists('wp_generate_attachment_metadata')){
                require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                require_once(ABSPATH . "wp-admin" . '/includes/file.php');
                require_once(ABSPATH . "wp-admin" . '/includes/media.php');
            }
             if ($_FILES) {
                foreach ($_FILES as $file => $array) {
                    if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                        return "upload error : " . $_FILES[$file]['error'];
                    }
                    $attach_id = media_handle_upload( $file, $pid );
                }   
            }
            if ($attach_id > 0){
                //and if you want to set that image as Post  then use:
                update_post_meta($pid,'_thumbnail_id',$attach_id);
            }  //add thumbnail end

    update_post_meta($new_post_id, 'tax_input', $property_type);
    update_post_meta($new_post_id, 'tax_input', $suburbs);

    //insert custom fields
    update_post_meta($pid,'bathrooms',$_POST['bathrooms']);
    update_post_meta($pid,'property_lease',$_POST['property_lease']);
    update_post_meta($pid,'lost_found_date',$_POST['lost_found_date']);
    update_post_meta($pid,'dog_sex',$_POST['dog_sex']);
    update_post_meta($pid,'latitude',$_POST['latitude']);
    update_post_meta($pid,'longitude',$_POST['longitude']);


    $url = get_permalink( $pid );
    wp_redirect($url);
    exit();
}

 get_header(); 
share|improve this question
1  
Is anything being output to the screen prior to the wp_redirect() call? Anything echoed will cause you problems, as will any PHP files with any content -- including whitespace -- after the closing ?> tag. –  Pat J Jun 16 '13 at 21:01
    
after submit, sometime redirect to the permalink, sometime nothing comes but added contents... –  Jin Jun 16 '13 at 21:28
    
even I removed "$url = get_permalink( $pid ); wp_redirect($url); exit();" it's not working as well.... –  Jin Jun 16 '13 at 21:31

1 Answer 1

Lines like this one can cause trouble: if ($_FILES) {. If $_FILES is not set and you try to use it in a conditional like that, you will get a Notice and if debugging is on that notice will print to the screen. I am guessing that is what is happening.

You should check variables with isset or empty, the way you do in a number of other places, and never use a bare variable as if it were a boolean.

Also, you are also returning from what appears to be global scope. When I try that, I get bool(false) printed to the screen. That could cause this issue as well.

share|improve this answer
    
I don't understand php at all... please let me know the correct code.. many thanks... –  Jin Jun 16 '13 at 21:57
    
Sorry, but I can't let you "know the correct code" without duplicating your setup and you haven't posted enough code for me to do that even if I had the time to do it. –  s_ha_dum Jun 17 '13 at 4:08

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.