title: Saving Info on web form error on submit
body: <p>I have a php script that I am working on and have yet to be able to get the form to save the info and repopulate on a validation error. I've tried several how to's on various websites but perhaps I am missing something as they mostly have a lot of other PHP lines in them but the how to pages don't give specifics on the lines of code that are necessary to save data and repopulate. Below is the code and if anyone has any ideas or could point me to some place that works well I would greatly appreciate it, I've hit a wall thus far. Thanks in advance!!</p>
  
  <pre><code>    &lt;?php
      /*
      Template Name: Post Submit Form
      */
      ?&gt;
      &lt;?php if ( $user_ID &gt; 0) {  ?&gt;
      &lt;?php if( 'POST' == $_SERVER['REQUEST_METHOD'] &amp;&amp; !empty( $_POST['action'] ) &amp;&amp;  $_POST['action'] == "new_post") {
          // Do some minor form validation to make sure there is content
          if (isset($_POST['submit'])) {
                  $error = "";
  
              if (!empty($_POST['title'])) {
                  $title = $_POST['title'];
           } else {
              $error .= "Please add a title&lt;br /&gt;";
          }
  
              if (!empty($_POST['description'])) {
                  $description = $_POST['description'];
           } else {
              $error .= "Please add a description&lt;br /&gt;";
          }
  
              if (!empty($_POST['post_tags'])) {
                  $post_tags = $_POST['post_tags'];
           } else {
              $error .= "Please add some keywords&lt;br /&gt;";
          }
              if (!empty($_POST['externalurl'])) {
                  $externalurl = $_POST['externalurl'];
           } else {
              $error .= "Please add a URL to post&lt;br /&gt;";
          }
              // IMAGE VALIDATION - CHECK IF THERE IS AN IMAGE AND THAT ITS THE RIGHT FILE TYPE AND RIGHT SIZE
              if ($_FILES) {
                  foreach ($_FILES as $file =&gt; $array) {
                      //Check if the $_FILES is set and if the size is &gt; 0 (if =0 it's empty)
  
                      if(isset($_FILES[$file]) &amp;&amp; ($_FILES[$file]['size'] &gt; 0)) {
  
                          $tmpName = $_FILES[$file]['tmp_name'];
                          list($width, $height, $type, $attr) = getimagesize($tmpName);
  
                      if($width&lt;=899 || $height&lt;=299)
                      {
                          $error .= "Image is to small. Minimum 900 pixels wide.&lt;br /&gt;";
                          unlink($_FILES[$file]['tmp_name']); 
                      }
  
                      // Get the type of the uploaded file. This is returned as "type/extension"
                      $arr_file_type = wp_check_filetype(basename($_FILES[$file]['name']));
                      $uploaded_file_type = $arr_file_type['type'];
  
                       // Set an array containing a list of acceptable formats
                      $allowed_file_types = array('image/jpg','image/jpeg');
  
                       // If the uploaded file is the right format
                      if(in_array($uploaded_file_type, $allowed_file_types)) {
  
                      } else { // wrong file type
                      $error .= "Please upload a .jpg type image&lt;br /&gt;";
                           }
  
                      } else {
                      $error .= "Please add an image&lt;br /&gt;";
                      }
                  } // end for each
              } // end if
  
              $tags = $_POST['post_tags'];
              $externalurl = $_POST['externalurl'];
  
              // ADD THE FORM INPUT TO $new_post ARRAY
              if (empty($error)) {
                  $new_post = array(
                  'post_title'    =&gt;  $title,
                  'post_content'  =&gt;  $description,
                  'post_category' =&gt;  array($_POST['cat']),  // Usable for custom taxonomies too
                  'tags_input'    =&gt;  array($tags),
                  'post_status'   =&gt;  'preview',           // Choose: publish, preview, future, draft, etc.
                  'post_type' =&gt;  'post',  //'post',page' or use a custom post type if you want to
                  'externalurl'   =&gt;  $externalurl
              );
  
              //SAVE THE POST
              $pid = wp_insert_post($new_post);
  
              //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
              wp_set_post_tags($pid, $_POST['post_tags']);
  
              //REDIRECT TO THE NEW POST ON SAVE
              $link = get_permalink( $pid );
              wp_redirect( $link );
  
              //ADD OUR CUSTOM FIELDS 
              add_post_meta($pid, 'externalurl', $externalurl, true); 
  
                  //INSERT OUR MEDIA ATTACHMENTS
                  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 =&gt; $array) {
                      if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                          return "upload error : " . $_FILES[$file]['error'];
                      }
                      $attach_id = media_handle_upload( $file, $pid );
                  }   
              }
              if ($attach_id &gt; 0){
                  //and if you want to set that image as Post  then use:
                  update_post_meta($pid,'_thumbnail_id',$attach_id);
              }
              } // END SAVING POST
          } // END VALIDATION
      } // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
  
      //POST THE POST YO
      do_action('wp_insert_post', 'wp_insert_post');
  
      ?&gt;
  
  
      &lt;?php get_header(); ?&gt;
      &lt;?php get_template_part('wrapper', 'start'); ?&gt;
  
  
              &lt;article id="post-&lt;?php the_ID(); ?&gt;" &lt;?php post_class('box mb20'); ?&gt;&gt;
  
  
      &lt;?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?&gt;
  
                      &lt;div id="post-&lt;?php the_ID(); ?&gt;" &lt;?php post_class(); ?&gt;&gt;
                          &lt;?php if ( is_front_page() ) { ?&gt;
                              &lt;h2 class="entry-title"&gt;&lt;?php the_title(); ?&gt;&lt;/h2&gt;
                          &lt;?php } else { ?&gt;
                              &lt;h1 class="entry-title"&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;
                          &lt;?php } ?&gt;
  
                          &lt;div class="form-content"&gt;
                           &lt;?php
                              if (!empty($error)) {
                                  echo '&lt;p class="error"&gt;&lt;strong&gt;Your message was NOT sent&lt;br/&gt; The following error(s) returned:&lt;/strong&gt;&lt;br/&gt;' . $error . '&lt;/p&gt;';
                              } elseif (!empty($success)) {
                                  echo '&lt;p class="success"&gt;' . $success . '&lt;/p&gt;';
                              }
                          ?&gt;
                              &lt;div class="entry-content clearfix mt20"&gt;
                                  &lt;?php the_content(); ?&gt;
                              &lt;/div&gt;
                              &lt;div class="dashedline"&gt;&lt;/div&gt;
  
              &lt;!-- Start FORM --&gt;
  
              &lt;div class="submitpost-form"&gt;
              &lt;form id="new_post" name="new_post" method="post" action="" class="submitpost-form" enctype="multipart/form-data"&gt;
                  &lt;!-- post name --&gt;
                  &lt;fieldset name="name"&gt;
                      &lt;label for="title"&gt;Post Title:&lt;/label&gt;
                      &lt;input type="text" id="submitpost-entry" value="&lt;?php echo $_SERVER['title']; ?&gt;" tabindex="5" name="title" /&gt;
                       &lt;p class="submitpost-entry-infotext"&gt;Choose a descriptive title that highlights the most important thing about your project. This will also be part of the URL. Do not use your name or genre in it.&lt;/p&gt;
                  &lt;/fieldset&gt;
  
                  &lt;!-- External URL --&gt;
                  &lt;fieldset class="externalurl"&gt;
                      &lt;label for="externalurl"&gt;Direct link (URL) to post page: (must include http://)&lt;/label&gt;
                      &lt;input type="text" value="&lt;?php echo $_SERVER['externalurl']; ?&gt;" id="submitpost-entry" tabindex="10" name="externalurl" onFocus="this.value=''"/&gt;
                      &lt;p class="submitpost-entry-infotext"&gt;This will be the URL linked to by the post. This should be a direct url to the webpage / article about the project. Do not link to your homepage or main portfolio page. The image submited below must appear on this page.&lt;/p&gt;
                  &lt;/fieldset&gt;
  
                  &lt;!-- post tags --&gt;
                  &lt;fieldset class="tagsentry"&gt;
                      &lt;label for="post_tags"&gt;Tags (comma separated):&lt;/label&gt;
                      &lt;input type="text" value="" tabindex="15" name="post_tags" id="submitpost-entry" /&gt;
                      &lt;p class="submitpost-entry-infotext"&gt;Use a few descriptive words (all lowercase) to allow users to discover your work via exploring tags. Avoid using genre terms. Example: environmental, b&amp;w, lifestyle. Keep below about ten.&lt;/p&gt;
                  &lt;/fieldset&gt;
  
  
                  &lt;!-- post Category --&gt;
                  &lt;fieldset class="category"&gt;
                      &lt;label for="cat"&gt;Genre:&lt;/label&gt;
                      &lt;?php wp_dropdown_categories( 'tab_index=20&amp;taxonomy=category&amp;hide_empty=0&amp;exclude=1' ); ?&gt;
                      &lt;p class="submitpost-entry-infotext" style="position:relative; top:-14px;"&gt;Select the most suitable genre for your work. If you have a genre suggestion please let us know via the contact page.&lt;/p&gt;
                  &lt;/fieldset&gt;
  
                  &lt;!-- authors --&gt;
                  &lt;fieldset class="images"&gt;
                      &lt;label for="bottle_front"&gt;Image&lt;/label&gt;
                      &lt;input type="file" name="Image" id="image" tabindex="25" /&gt;
                      &lt;p class="submitpost-entry-infotext"&gt;Images must be .jpg format with a minimum of 900 pixels wide and be less than 600kb in size.&lt;/p&gt;
                  &lt;/fieldset&gt;
  
                  &lt;!-- post Content --&gt;
                  &lt;fieldset class="submitpostcontent"&gt;
                      &lt;label for="description"&gt;Description:&lt;/label&gt;
                      &lt;textarea id="submitpost-message" tabindex="30" name="description" rows="10"&gt;&lt;/textarea&gt;
                      &lt;p class="submitpost-entry-infotext" &gt;Enter more detail about the project or piece, the first 40 words will be displayed on the grid page. Any extra text will show on the post page. Do not compose in this field to avoid loosing text. Spell check before submiting! Profanity or offensive text will not be published. &lt;/p&gt;
                  &lt;/fieldset&gt;
  
  
                  &lt;fieldset class="submit"&gt;
                      &lt;input type="submit" value="Post Review" tabindex="40" id="submit" name="submit" /&gt;
                  &lt;/fieldset&gt;
  
                  &lt;input type="hidden" name="action" value="new_post" /&gt;
                  &lt;?php wp_nonce_field( 'new-post' ); ?&gt;
              &lt;/form&gt;
  
  
              &lt;/div&gt; &lt;!-- END WPCF7 --&gt;
  
              &lt;!-- END OF FORM --&gt;
                              &lt;?php wp_link_pages( array( 'before' =&gt; '&lt;div class="page-link"&gt;' . __( 'Pages:', 'twentyten' ), 'after' =&gt; '&lt;/div&gt;' ) ); ?&gt;
                              &lt;?php edit_post_link( __( 'Edit', 'twentyten' ), '&lt;span class="edit-link"&gt;', '&lt;/span&gt;' ); ?&gt;
                          &lt;/div&gt;&lt;!-- .entry-content --&gt;
                      &lt;/div&gt;&lt;!-- #post-## --&gt;
  
                      &lt;?php comments_template( '', true ); ?&gt;
  
      &lt;?php endwhile; // end of the loop. ?&gt;
  
          &lt;/article&gt;
  
      &lt;?php get_template_part('wrapper', 'end'); ?&gt;
  
  
      &lt;?php get_footer(); ?&gt;
  
  
  
      &lt;?php } else { ?&gt;
  
  
      &lt;?php get_header(); ?&gt;
  
      &lt;?php get_template_part('wrapper', 'start'); ?&gt;
  
          &lt;?php while (have_posts()) : the_post(); ?&gt;
  
      &lt;article id="post-&lt;?php the_ID(); ?&gt;" &lt;?php post_class('box mb20'); ?&gt;&gt;
  
          &lt;header class="entry-header"&gt;
              &lt;h1 class="entry-title"&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;
          &lt;/header&gt;
  
              &lt;div class="entry-content clearfix mt20"&gt;
                  Message for non logged in users.
              &lt;/div&gt;
  
  
  
      &lt;/article&gt;  
  
              &lt;?php comments_template(); ?&gt;
  
          &lt;?php endwhile; ?&gt;
  
      &lt;?php get_template_part('wrapper', 'end'); ?&gt;
  
      &lt;?php get_footer(); ?&gt;
      &lt;?php }; ?&gt;
  </code></pre>