Swatist
2/21/2017 - 1:43 PM

create contact form in wordpress

create contact form in wordpress

 <?php 
            
                   
                    if($_SERVER['REQUEST_METHOD'] == 'POST' ){
                      
                    $to = get_option( 'admin_email' ); // this is your Email address
                    $from = get_option( 'admin_email' ); // this is the sender's Email address
                    $fullname = $_POST['fullname'];
                    $contact_email = $_POST['txtemail'];
                    $contact_phone = $_POST['mobile-number'];
                    
					
                    
                    $errors = array();
                     
                      // Validate the input
                      if (!preg_match('/^[\pL\s]+$/u', $fullname)){
                     array_push($errors, __("Please enter your Full Name", "stackadroit"));}
            
                    if (!filter_var($contact_email, FILTER_VALIDATE_EMAIL)){
                     array_push($errors, __("Please specify a valid email address", "stackadroit"));}
                    
                    if (!preg_match('/(?:\(?\+\d{2}\)?\s*)?\d+(?:[ -]*\d+)*$/', $contact_phone)){
                      array_push($errors, __("Please enter your Mobile number", "stackadroit"));}
                    
                   if(!empty($errors)){
                      //Prepare errors for output
                      echo "<div class='error-msg'>";
                       foreach($errors as $val) {
                       echo "<div class='container'><div><p class='output alert-danger'>$val</p></div></div>";   
                       }
                       echo "</div>";
                      }
                      else{
                      
                       $subject = "Form submission - Contact Us ";
                       $message = "<table>";
                       $message .= "<tr><td><h1>Form submitted from Product Enquiry for </h1></tr></td>"."<tr><td><p>Full Name: " .$fullname . "</p></tr></td>"
                        ."<tr><td><p>Email ID: " . $contact_email . "</p></tr></td>" ."<tr><td><p> Phone No: " .$contact_phone."</p></tr></td>";
                       $message .="</table>";
                      
                      
                       $headers = "From:" . $from;
                      
                       add_filter('wp_mail_content_type', create_function('', 'return "text/html"; '));
                       wp_mail($to,$subject,$message,$headers);
                       
                       remove_filter('wp_mail_content_type', 'set_html_content_type');
                   ?>
    <p  class='bg-success'>
      <?php  _e("Mail Sent. Thank you ", "stackadroit"); ?>
      <?php echo   $fullname; ?>
      <?php  _e(" we will contact you shortly.", "stackadroit"); ?>
    </p>
    <?php   
                       
                    }
                   }
            
            
            ?>
    <form action="#"  method="post" class="clearfix">
      <input type="text" placeholder="<?php _e("Full Name" , "stackadroit"); ?>" name="fullname" class="pro-input">
      <input type="email" placeholder="<?php _e("Email" , "stackadroit"); ?>" name="txtemail" class="pro-input">
      <input type="tel" placeholder="<?php _e("Mobile" , "stackadroit"); ?>" name="mobile-number"  class="pro-input">
      <input type="hidden" name="title " value="<?php the_title();?>" class="pro-input">
      <input type="hidden" name="content " value="<?php the_content(); ?>" class="pro-input">
      <input type="submit" value="<?php _e("submit" , "stackadroit"); ?>" name="submit" class="pro-input submit-btn button-styles">
    </form>