title: Reset form fields after form submission in wordpress custom contact form plugin
body: <p>I've my own custom contact form wordpress plugin, in which I'm unable to reset the form fields after the user submits the form. </p>
  
  <p>When the user fills up all the fields and submits, success message will be shown, and after that I need the form to reset all the fields. At present it shows the values entered by the user before submission.</p>
  
  <p>Any help is greatly appreciated. Thanks in advance. Here is my code.</p>
  
  <pre><code>&lt;?php
  /*
  Plugin Name: Custom Contact Form
  Plugin URI: 
  Description: &lt;code&gt;[contact email="your@email.address"]&lt;/code&gt;
  Version: 1.0
  Author: 
  Author URI: 
  */
  
  // function to get the IP address of the user
  function tw_get_the_ip() {
      if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
          return $_SERVER["HTTP_X_FORWARDED_FOR"];
  }
  elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
      return $_SERVER["HTTP_CLIENT_IP"];
  }
  else {
      return $_SERVER["REMOTE_ADDR"];
  }
  }
  
  // the shortcode
  function tw_contact_form_sc($atts) {
  extract(shortcode_atts(array(
      "email" =&gt; get_bloginfo('admin_email'),
      "subject" =&gt; '',
      "label_name" =&gt; 'Your Name',
      "label_email" =&gt; 'Your E-mail Address',
      "label_subject" =&gt; 'Subject',
      "label_message" =&gt; 'Your Message',
      "label_submit" =&gt; 'Submit',
      "error_empty" =&gt; 'Please fill in all the required fields.',
      "error_noemail" =&gt; 'Please enter a valid e-mail address.',
      "success" =&gt; 'Thanks for your e-mail! We\'ll get back to you as soon as we can.'
  ), $atts));
  
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      $error = false;
      $required_fields = array("your_name", "email", "message", "subject");
  
      foreach ($_POST as $field =&gt; $value) {
          if (get_magic_quotes_gpc()) {
              $value = stripslashes($value);
          }
          $form_data[$field] = strip_tags($value);
      }
  
      foreach ($required_fields as $required_field) {
          $value = trim($form_data[$required_field]);
          if(empty($value)) {
              $error = true;
              $result = $error_empty;
          }
      }
  
      if(!is_email($form_data['email'])) {
          $error = true;
          $result = $error_noemail;
      }
  
      if ($error == false) {
          $email_subject = "[" . get_bloginfo('name') . "] " . $form_data['subject'];
          $email_message = $form_data['message'] . "\n\nIP: " . tw_get_the_ip();
          $headers  = "From: ".$form_data['your_name']." &lt;".$form_data['email']."&gt;\n";
          $headers .= "Content-Type: text/plain; charset=UTF-8\n";
          $headers .= "Content-Transfer-Encoding: 8bit\n";
          wp_mail($email, $email_subject, $email_message, $headers);
          $result = $success;
          $sent = true;
      }
  }
  
  if($result != "") {
      $info = '&lt;div class="info"&gt;'.$result.'&lt;/div&gt;';
  }
  $email_form = '&lt;form class="contact-form" method="post" action="'.get_permalink().'"&gt;
      &lt;div&gt;
          &lt;label for="cf_name"&gt;'.$label_name.':&lt;/label&gt;
          &lt;input type="text" name="your_name" id="cf_name" size="50" maxlength="50" value="'.$form_data['your_name'].'" /&gt;
      &lt;/div&gt;
      &lt;div&gt;
          &lt;label for="cf_email"&gt;'.$label_email.':&lt;/label&gt;
          &lt;input type="text" name="email" id="cf_email" size="50" maxlength="50" value="'.$form_data['email'].'" /&gt;
      &lt;/div&gt;
      &lt;div&gt;
          &lt;label for="cf_subject"&gt;'.$label_subject.':&lt;/label&gt;
          &lt;input type="text" name="subject" id="cf_subject" size="50" maxlength="50" value="'.$subject.$form_data['subject'].'" /&gt;
      &lt;/div&gt;
      &lt;div&gt;
          &lt;label for="cf_message"&gt;'.$label_message.':&lt;/label&gt;
          &lt;textarea name="message" id="cf_message" cols="50" rows="15"&gt;'.$form_data['message'].'&lt;/textarea&gt;
      &lt;/div&gt;
      &lt;div&gt;
          &lt;input type="submit" value="'.$label_submit.'" name="send" id="cf_send" /&gt;
      &lt;/div&gt;
  &lt;/form&gt;';
  
  if($sent == true) {
          return $info.$email_form;
  } else {
          return $info.$email_form;
  }
  } add_shortcode('contact', 'tw_contact_form_sc');
  
  ?&gt;
  </code></pre>