dragunoff

[WP][Thematic] Template Name: Contact Form Without a Plugin

Mar 15th, 2011
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.94 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Template Name: Contact Form
  4.  *
  5.  * Contact form without a plugin
  6.  * http://trevordavis.net/blog/wordpress-jquery-contact-form-without-a-plugin/
  7.  *
  8.  */
  9.  
  10. //If the form is submitted
  11. if(isset($_POST['submitted'])) {
  12.  
  13.     //Check to see if the honeypot captcha field was filled in
  14.     if(trim($_POST['checking']) !== '') {
  15.         $captchaError = true;
  16.     } else {
  17.    
  18.         //Check to make sure that the name field is not empty
  19.         if(trim($_POST['contactName']) === '') {
  20.             $nameError = __('Error: please fill the required fields (name, email).');
  21.             $hasError = true;
  22.         } else {
  23.             $name = trim($_POST['contactName']);
  24.         }
  25.        
  26.         //Check to make sure sure that a valid email address is submitted
  27.         if(trim($_POST['email']) === '')  {
  28.             $emailError = __('Error: please fill the required fields (name, email).');
  29.             $hasError = true;
  30.         } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
  31.             $emailError = __('You entered an invalid email address.', 'ip4all');
  32.             $hasError = true;
  33.         } else {
  34.             $email = trim($_POST['email']);
  35.         }
  36.            
  37.         //Check to make sure comments were entered 
  38.         if(trim($_POST['comments']) === '') {
  39.             $commentError = __('Error: please type a comment.');
  40.             $hasError = true;
  41.         } else {
  42.             if(function_exists('stripslashes')) {
  43.                 $comments = stripslashes(trim($_POST['comments']));
  44.             } else {
  45.                 $comments = trim($_POST['comments']);
  46.             }
  47.         }
  48.            
  49.         //If there is no error, send the email
  50.         if(!isset($hasError)) {
  51.  
  52.             $emailTo = get_option('admin_email');
  53.             if(trim($_POST['subject']) === '') {
  54.                 $subject = '[' . get_bloginfo('name') . '] ' . __('Contact Form Submission from', 'ip4all') . ' ' . $name;
  55.             } else {
  56.                 $subject = '[' . get_bloginfo('name') . '] ' . $name . ': ' . trim($_POST['comments']);
  57.             }
  58.             $sendCopy = trim($_POST['sendCopy']);
  59.             $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
  60.             $headers = 'From: ' . get_bloginfo('name') . ' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
  61.            
  62.             mail($emailTo, $subject, $body, $headers);
  63.  
  64.             if($sendCopy == true) {
  65.                 $subject = 'You emailed ' . get_bloginfo('name') ;
  66.                 $headers = 'From: ' . get_bloginfo('name') . '<[email protected]>';
  67.                 mail($email, $subject, $body, $headers);
  68.             }
  69.  
  70.             $emailSent = true;
  71.  
  72.         }
  73.     }
  74. } ?>
  75.  
  76.  
  77. <?php
  78.  
  79.     // calling the header.php
  80.     get_header();
  81.  
  82.     // action hook for placing content above #container
  83.     thematic_abovecontainer();
  84.  
  85. ?>
  86.  
  87.     <div id="container">
  88.    
  89.         <?php thematic_abovecontent(); ?>
  90.    
  91.         <div id="content">
  92.  
  93.             <?php
  94.        
  95.             // calling the widget area 'page-top'
  96.             // get_sidebar('page-top');
  97.  
  98.             the_post();
  99.            
  100.             thematic_abovepost();
  101.        
  102.             ?>
  103.            
  104.             <div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class() ?>">
  105.            
  106.                 <?php
  107.                
  108.                 // creating the post header
  109.                 thematic_postheader();
  110.                
  111.                 ?>
  112.                
  113.                 <div class="entry-content">
  114.  
  115.                     <?php
  116.                    
  117.                     the_content();
  118.                    
  119.                     wp_link_pages("\t\t\t\t\t<div class='page-link'>".__('Pages: ', 'ip4all'), "</div>\n", 'number');
  120.                    
  121.                     edit_post_link(__('Edit'),'<span class="edit-link">','</span>') ?>
  122.  
  123.                 </div>
  124.                
  125.                 <form action="<?php the_permalink(); ?>" id="contactForm" method="post">
  126.                     <fieldset class="clear">
  127.                     <legend><?php _e('Contact our editor', 'ip4all'); ?></legend>
  128.                    
  129.                     <?php if(isset($emailSent) && $emailSent == true) { ?>
  130.  
  131.                         <p class="success">
  132.                             <?php _e('Your email was successfully sent. We will be in touch soon.', 'ip4all'); ?>
  133.                         </p>
  134.  
  135.                     <?php } ?>
  136.  
  137.                     <?php if(isset($hasError) || isset($captchaError)) { ?>
  138.                         <p class="error"><?php _e('There was an error submitting the form.', 'ip4all'); ?><p>
  139.                     <?php } ?>
  140.                    
  141.                         <p>
  142.                             <?php _e( 'Your email address will not be published. Fields marked with <span class="required">*</span> are mandatory. ', 'ip4all' ); ?>
  143.                         </p>
  144.                        
  145.                         <p>
  146.                             <label for="contactName"><?php _e('Name'); ?> <span class="required">*</span></label><br />
  147.                             <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="requiredField" />
  148.                             <?php if($nameError != '') { ?>
  149.                                 <span class="error"><?php echo $nameError;?></span>
  150.                             <?php } ?>
  151.                         </p>
  152.                        
  153.                         <p>
  154.                             <label for="email"><?php _e('Email'); ?> <span class="required">*</span></label><br />
  155.                             <input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="requiredField email" />
  156.                             <?php if($emailError != '') { ?>
  157.                                 <span class="error"><?php echo $emailError;?></span>
  158.                             <?php } ?>
  159.                         </p>
  160.                        
  161.                         <p>
  162.                             <label for="subject"><?php _e('Subject', 'ip4all'); ?></label><br />
  163.                             <input type="text" name="subject" id="subject" value="<?php if(isset($_POST['subject']) && !isset($emailSent) && $emailSent == false)  echo $_POST['subject'];?>" />
  164.                         </p>
  165.                        
  166.                         <p>
  167.                             <label for="commentsText"><?php _e('Comment'); ?> <span class="required">*</span></label><br />
  168.                             <textarea name="comments" id="commentsText" rows="10" cols="20" class="requiredField"><?php if(isset($_POST['comments']) && !isset($emailSent) && $emailSent == false) { echo stripslashes(trim($_POST['comments'])); } ?></textarea>
  169.                             <?php if($commentError != '') { ?>
  170.                                 <span class="error"><?php echo $commentError;?></span>
  171.                             <?php } ?>
  172.                         </p>
  173.                        
  174.                         <p>
  175.                             <input type="checkbox" name="sendCopy" id="sendCopy" value="true"<?php if(isset($_POST['sendCopy']) && $_POST['sendCopy'] == true) echo ' checked="checked"'; ?> /><label for="sendCopy"><?php _e('Send a copy of this email to yourself', 'ip4all'); ?></label><br />
  176.                         </p>
  177.                        
  178.                         <p class="screen-reader-text">
  179.                             <label for="checking"><?php _e('If you want to submit this form, do not enter anything in this field', 'ip4all'); ?></label><br />
  180.                             <input type="text" name="checking" id="checking" value="<?php if(isset($_POST['checking']))  echo $_POST['checking'];?>" />
  181.                         </p>
  182.                        
  183.                     </fieldset>
  184.                    
  185.                     <p>
  186.                         <input type="hidden" name="submitted" id="submitted" value="true" />
  187.                         <button type="submit"><?php _e('Send Message', 'ip4all'); ?></button>
  188.                     </p>
  189.                    
  190.                 </form>
  191.                
  192.             </div><!-- .post -->
  193.  
  194.         <?php thematic_belowpost(); ?>
  195.    
  196.             <?php
  197.             //if ( get_post_custom_values('comments') )
  198.                 //thematic_comments_template(); // Add a key/value of "comments" to enable comments on pages!
  199.            
  200.             // calling the widget area 'page-bottom'
  201.             get_sidebar('page-bottom');
  202.            
  203.             ?>
  204.    
  205.             </div><!-- #content -->
  206.            
  207.             <?php thematic_belowcontent(); ?>
  208.            
  209.         </div><!-- #container -->
  210.  
  211. <?php
  212.  
  213.     // action hook for placing content below #container
  214.     thematic_belowcontainer();
  215.  
  216.     // calling the standard sidebar
  217.     thematic_sidebar();
  218.    
  219.     // calling footer.php
  220.     get_footer();
  221.  
  222. ?>
Advertisement
Add Comment
Please, Sign In to add comment