Advertisement
Guest User

wordpress shortcode contactform

a guest
Aug 7th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.27 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: contact_shortcode
  4. Plugin URI: http://nathanrobjohn.com
  5. Description: contact form. Usage: <code>[contact email="your@email.address"]</code>
  6. Version: 1.0
  7. Author: Nathan Robjohn
  8. Author URI:http://nathanrobjohn.com
  9. */
  10.  
  11.  
  12. function nathan_shortcode_contact( $atts, $content = null)  {
  13.  
  14.  
  15.     extract( shortcode_atts( array(
  16.       'email' => get_bloginfo('admin_email')
  17.       ), $atts ) );
  18.  
  19.       $content .= '<script type="text/javascript">
  20. var $j = jQuery.noConflict();
  21. $j(window).load(function(){            
  22.     $j("#contact-form").submit(function() {
  23.       // validate and process form here
  24.         var str = $j(this).serialize();                  
  25.            $j.ajax({
  26.            type: "POST",
  27.            url: "' . get_template_directory_uri() . '/short/sendmail.php",
  28.            data: str,
  29.            success: function(msg){                      
  30.                 $j("#note").ajaxComplete(function(event, request, settings)
  31.                 {
  32.                     if(msg == "OK") // Message Sent? Show the Thank You message and hide the form
  33.                     {
  34.                         result = "Your message has been sent. Thank you!";
  35.                         $j("#fields").hide();
  36.                     }
  37.                     else
  38.                     {
  39.                         result = msg;
  40.                     }                                
  41.                     $j(this).html(result);                          
  42.                 });                  
  43.             }                    
  44.          });                    
  45.         return false;
  46.     });        
  47. });
  48. </script>';
  49.                    
  50.                    
  51.         // now we put all of the HTML for the form into a PHP string
  52.         $content .= '<div id="post-a-comment" class="span12 contactform">';
  53.             $content .= '<div id="fields">';
  54.                 $content .= '<h1>Enquire</h1>';
  55.             $content .= '<div id="note"></div> <!--notification area used by jQuery/Ajax -->';
  56.                 $content .= '<form id="contact-form" action="">';
  57.                     $content .= '<input name="to_email" type="hidden" id="to_email" value="' . $email . '"/>';
  58.                     $content .= '<p class="span2">';
  59.                         $content .= '<label class="error" for="name">Name *</label>';
  60.                         $content .= '<input name="name" type="text" id="name"/>';
  61.                     $content .= '</p>';
  62.                     $content .= '<p class="span2">';
  63.                         $content .= '<label for="email">Phone number *</label>';
  64.                         $content .= '<input name="phone" type="text" id="phone"/>';
  65.                     $content .= '</p>';
  66.                     $content .= '<p class="span2">';
  67.                         $content .= '<label for="subject">Best time to call</label>';
  68.                     $content .= '<select name="time" id="subject" role="select" aria-required="true">
  69.                                 <option></option>
  70.                                 <option>Morning</option>
  71.                                 <option>Afternoon</option>
  72.                                 <option>Anytime</option>
  73.                             </select>   ';              
  74.                     $content .= '</p>';    
  75.                     $content .= '<p class="span2">';
  76.                         $content .= '<label for="email">E-mail address *</label>';
  77.                         $content .= '<input name="email" type="text" id="email"/>';
  78.                     $content .= '</p>';
  79.                         $content .= '<p><label for="email">Enquiry message *</label></p>';
  80.                     $content .= '<p class="span12"><textarea rows="16" cols="" name="message"></textarea></p>';
  81.                     $content .= '<input type="submit" value="Submit" class="button" id="contact-submit" />';
  82.                 $content .= '</form>';
  83.             $content .= '</div><!--end fields-->';
  84.         $content .= '</div>';
  85.     return $content;
  86. }
  87. add_shortcode('contact', 'nathan_shortcode_contact');
  88.  
  89. <?php
  90. $plugin_name = 'contact_shortcode';
  91.  
  92.  
  93. function ValidateEmail($email)
  94. {
  95.     /*
  96.     (Name) Letters, Numbers, Dots, Hyphens and Underscores
  97.     (@ sign)
  98.     (Domain) (with possible subdomain(s) ).
  99.     Contains only letters, numbers, dots and hyphens (up to 255 characters)
  100.     (. sign)
  101.     (Extension) Letters only (up to 10 (can be increased in the future) characters)
  102.     */
  103.  
  104.     $regex = '/([a-z0-9_.-]+)'. # name
  105.  
  106.     '@'. # at
  107.  
  108.     '([a-z0-9.-]+){2,255}'. # domain & possibly subdomains
  109.  
  110.     '.'. # period
  111.  
  112.     '([a-z]+){2,10}/i'; # domain extension
  113.  
  114.     if($email == '') {
  115.         return false;
  116.     }
  117.     else {
  118.         $eregi = preg_replace($regex, '', $email);
  119.     }
  120.  
  121.     return empty($eregi) ? true : false;
  122. }
  123.  
  124. $post = (!empty($_POST)) ? true : false;
  125.  
  126. if($post)
  127. {
  128.     $name = stripslashes($_POST['name']);
  129.     $time = stripslashes($_POST['time']);
  130.     $phone = stripslashes($_POST['phone']);
  131.     $subject = 'Contact Form';
  132.     $email = trim($_POST['email']);
  133.     $to = trim($_POST['to_email']);
  134.     $message = stripslashes($_POST['message']);
  135.     $error = '';
  136.  
  137.     // Check name
  138.  
  139.     if(!$name)
  140.     {
  141.         $error .= 'Please enter your name.<br />';
  142.     }
  143.    
  144.     if(!$phone)
  145.     {
  146.         $error .= 'Please enter a phone number.<br />';
  147.     }
  148.    
  149.     if(!$time)
  150.     {
  151.         $error .= 'Please select a time.<br />';
  152.     }
  153.  
  154.     // Check email
  155.  
  156.     if(!$email)
  157.     {
  158.         $error .= 'Please enter an e-mail address.<br />';
  159.     }
  160.  
  161.     if($email && !ValidateEmail($email))
  162.     {
  163.         $error .= 'Please enter a valid e-mail address.<br />';
  164.     }
  165.  
  166.     // Check message (length)
  167.  
  168.     if(!$message || strlen($message) < 15)
  169.     {
  170.         $error .= "Please enter your message. It should have at least 15 characters.<br />";
  171.     }
  172.  
  173.     if(!$error) // send email
  174.     {
  175.         $contents = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nPhone: $phone \n\nTime: $time  \n\nMessage:\n $message";
  176.         $mail = mail($to, $subject, $contents,
  177.              'From: '.$name.' <'.$email.'>\r\n'
  178.             .'Reply-To: '.$email.'\r\n'
  179.             .'X-Mailer: PHP/' . phpversion());
  180.  
  181.         if($mail)
  182.         {
  183.             echo 'OK';
  184.         }
  185.  
  186.     }
  187.     else
  188.     {
  189.         echo '<div class="notification_error">'.$error.'</div>'; // set up error div for jQuery/Ajax
  190.     }
  191.  
  192. }
  193. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement