0

This source works with image upload. Without image upload, it works, but 'redirect' doesn't work. What should I do?? Any suggestions to what the problem might be?

    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['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);
    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,'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,'address',$_POST['address']);
    update_post_meta($pid,'price',$_POST['price']); 


       //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){
    $post = get_post($pid,'ARRAY_A');
    $image = wp_get_attachment_image_src( $attach_id );
    $image_tag = '<img src="'.$image[0].'" width="'.$image[1].'" height="'.$image[2].'" />';

    //add image above the content
    update_post_meta($pid,'_thumbnail_id',$attach_id);

    //add image under the content
    //$post['post_content'] = $post['post_content'] . $image_tag;    
    }


    $url = get_permalink( $pid );
    wp_redirect($url);
    exit();
}
2
  • Do you have debugging information?
    – s_ha_dum
    Jun 19, 2013 at 2:44
  • Jim in your code are you sending an array of suburbs to a custom taxonomy? I'm having trouble with similar code getting a comma separated list from an input to insert correctly. How are you sending / collecting a list of suburbs on your front end? Thanks
    – Anagio
    Aug 15, 2013 at 21:29

1 Answer 1

1

You can not use wp_redirect() if the headers have already been sent so snippet you provided should not be used within your template files, instead you should wrap it within a function and place it into your functions.php file and hook onto template_redirect action.

Example:

add_action('template_redirect', 'front_end_post', 99);

function front_end_post() {

    if( 'POST' == $_SERVER['REQUEST_METHOD'] 
        && !empty( $_POST['action'] ) 
        &&  $_POST['action'] == "new_post") 
    {

    // your logic here

    //your redirect
    $url = get_permalink( $pid );
    wp_redirect($url);
    exit();

    }

}
1
  • alright.. after I used your solution, It seems resolved headers problem but still have a problem... it works right way when added image only..
    – Jin
    Jun 19, 2013 at 3:21

Your Answer

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

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