title: wp_redirect not working
body: <p>I am trying to post to a page and do some wordpress insertion in post but somehow the redirect is not working and after that. I tried following three options:</p>
  
  <ol>
  <li>wp_redirect($location);</li>
  <li>wp_safe_redirect($location);</li>
  <li>header('Location:http://example.com');</li>
  </ol>
  
  <p>here is what I want to do:</p>
  
  <ol>
  <li>posting to another page.</li>
  <li>look for file to be uploaded and upload directory</li>
  <li>Upload the file.</li>
  <li>Set post thumbnail using set_post_thumbnail</li>
  <li>redirect to desired page.</li>
  </ol>
  
  <p>the script work till setting thumbnail and then dies.
  If anyone want to use it to upload thumbnail from template front end, they can use it.</p>
  
  <hr>
  
  <pre><code>// if you have this in a file you will need to load "wp-load.php" to get access to WP functions. If you post to "self" with this code then WordPress is by default loaded
      require $_SERVER['DOCUMENT_ROOT'] . "/wp-load.php";
      // require two files that are included in the wp-admin but not on the front end. These give you access to some special functions below.
      require $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/file.php";
      require $_SERVER['DOCUMENT_ROOT'] . "/wp-admin/includes/image.php";
  
      // required for wp_handle_upload() to upload the file
      $upload_overrides = array( 'test_form' =&gt; FALSE );
  
      global $current_user;
      get_currentuserinfo();
      $logged_in_user = $current_user-&gt;ID;
  
      //get user POST information
      global $post;
      $loop = new WP_Query( array( 
                              'posts_per_page' =&gt; 1, 
                              'post_type' =&gt; "vet-profile", 
                              'order' =&gt; "ASC", 
                              'orderby' =&gt; "menu_order", 
                              'author'=&gt;"$logged_in_user"
                          )
      );
      while ( $loop-&gt;have_posts() ): $loop-&gt;the_post();
          $current_postID = $post-&gt;ID;
          $current_posttitle = get_the_title(); 
      endwhile;   
  
      // count how many files were uploaded
      $upload_files = $_FILES[ 'upload_files' ];
  
      // load up a variable with the upload direcotry
      $uploads = wp_upload_dir();
  
      // foreach file uploaded do the upload
      //foreach ( range( 0, $count_files ) as $i ) 
      //{
          // create an array of the $_FILES for each file
          $file_array = array(
              'name' =&gt; $_FILES['upload_files']['name'],
              'type'  =&gt; $_FILES['upload_files']['type'],
              'tmp_name'  =&gt; $_FILES['upload_files']['tmp_name'],
              'error' =&gt; $_FILES['upload_files']['error'],
              'size'  =&gt; $_FILES['upload_files']['size'],
          );
  
          // check to see if the file name is not empty
          if ( !empty( $file_array['name'] ) ) 
          { ?&gt;
       &lt;?php
              // upload the file to the server
              $uploaded_file = wp_handle_upload( $upload_files, $upload_overrides );
  
              if ( $uploaded_file ) 
              {
                  echo '&lt;script&gt;alert("Image successfully uploaded.\n");&lt;/script&gt;';
              } 
              else 
              {
                  echo '&lt;script&gt;alert("Fish! some error occured. Please try again.");&lt;/script&gt;';
              } 
  
              // checks the file type and stores in in a variable
              $wp_filetype = wp_check_filetype( basename( $uploaded_file['file'] ), null );   
  
              // set up the array of arguments for "wp_insert_post();"
              $attachment = array(
                  'post_mime_type' =&gt; $wp_filetype['type'],
                  'post_title' =&gt; preg_replace('/\.[^.]+$/', '', basename( $uploaded_file['file'] ) ),
                  'post_content' =&gt; '',    
                  'post_author' =&gt; $logged_in_user,
                  'post_status' =&gt; 'inherit',
                  'post_type' =&gt; 'attachment',
                  'post_parent' =&gt; $current_postID,
                  'guid' =&gt; $uploads['url'] . '/' . $file_array['name']
              );
  
  
              // insert the attachment post type and get the ID
              $attachment_id = wp_insert_attachment( $attachment, $uploaded_file['file'], $current_postID );
  
              // generate the attachment metadata
              $attach_data = wp_generate_attachment_metadata( $attachment_id, $uploaded_file['file'] );
  
              // update the attachment metadata
              wp_update_attachment_metadata( $attachment_id, $attach_data);
  
              // set thumbnail to the current post
              set_post_thumbnail( $current_postID, $attachment_id );
  
              echo '&lt;script&gt;alert("set thumbnail")';
              $location = empty($_POST['redirect_to']) ? get_redirect_link() : $_POST['redirect_to'];
              wp_safe_redirect( $location ); 
              echo '&lt;script&gt;alert("End Loop");&lt;/script&gt;';
          }
  </code></pre>
  
  <hr>
  
  <p>Thanks in Advance</p>