Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: contact_shortcode
- Plugin URI: http://nathanrobjohn.com
- Description: contact form. Usage: <code>[contact email="your@email.address"]</code>
- Version: 1.0
- Author: Nathan Robjohn
- Author URI:http://nathanrobjohn.com
- */
- function nathan_shortcode_contact( $atts, $content = null) {
- extract( shortcode_atts( array(
- 'email' => get_bloginfo('admin_email')
- ), $atts ) );
- $content .= '<script type="text/javascript">
- var $j = jQuery.noConflict();
- $j(window).load(function(){
- $j("#contact-form").submit(function() {
- // validate and process form here
- var str = $j(this).serialize();
- $j.ajax({
- type: "POST",
- url: "' . get_template_directory_uri() . '/short/sendmail.php",
- data: str,
- success: function(msg){
- $j("#note").ajaxComplete(function(event, request, settings)
- {
- if(msg == "OK") // Message Sent? Show the Thank You message and hide the form
- {
- result = "Your message has been sent. Thank you!";
- $j("#fields").hide();
- }
- else
- {
- result = msg;
- }
- $j(this).html(result);
- });
- }
- });
- return false;
- });
- });
- </script>';
- // now we put all of the HTML for the form into a PHP string
- $content .= '<div id="post-a-comment" class="span12 contactform">';
- $content .= '<div id="fields">';
- $content .= '<h1>Enquire</h1>';
- $content .= '<div id="note"></div> <!--notification area used by jQuery/Ajax -->';
- $content .= '<form id="contact-form" action="">';
- $content .= '<input name="to_email" type="hidden" id="to_email" value="' . $email . '"/>';
- $content .= '<p class="span2">';
- $content .= '<label class="error" for="name">Name *</label>';
- $content .= '<input name="name" type="text" id="name"/>';
- $content .= '</p>';
- $content .= '<p class="span2">';
- $content .= '<label for="email">Phone number *</label>';
- $content .= '<input name="phone" type="text" id="phone"/>';
- $content .= '</p>';
- $content .= '<p class="span2">';
- $content .= '<label for="subject">Best time to call</label>';
- $content .= '<select name="time" id="subject" role="select" aria-required="true">
- <option></option>
- <option>Morning</option>
- <option>Afternoon</option>
- <option>Anytime</option>
- </select> ';
- $content .= '</p>';
- $content .= '<p class="span2">';
- $content .= '<label for="email">E-mail address *</label>';
- $content .= '<input name="email" type="text" id="email"/>';
- $content .= '</p>';
- $content .= '<p><label for="email">Enquiry message *</label></p>';
- $content .= '<p class="span12"><textarea rows="16" cols="" name="message"></textarea></p>';
- $content .= '<input type="submit" value="Submit" class="button" id="contact-submit" />';
- $content .= '</form>';
- $content .= '</div><!--end fields-->';
- $content .= '</div>';
- return $content;
- }
- add_shortcode('contact', 'nathan_shortcode_contact');
- <?php
- $plugin_name = 'contact_shortcode';
- function ValidateEmail($email)
- {
- /*
- (Name) Letters, Numbers, Dots, Hyphens and Underscores
- (@ sign)
- (Domain) (with possible subdomain(s) ).
- Contains only letters, numbers, dots and hyphens (up to 255 characters)
- (. sign)
- (Extension) Letters only (up to 10 (can be increased in the future) characters)
- */
- $regex = '/([a-z0-9_.-]+)'. # name
- '@'. # at
- '([a-z0-9.-]+){2,255}'. # domain & possibly subdomains
- '.'. # period
- '([a-z]+){2,10}/i'; # domain extension
- if($email == '') {
- return false;
- }
- else {
- $eregi = preg_replace($regex, '', $email);
- }
- return empty($eregi) ? true : false;
- }
- $post = (!empty($_POST)) ? true : false;
- if($post)
- {
- $name = stripslashes($_POST['name']);
- $time = stripslashes($_POST['time']);
- $phone = stripslashes($_POST['phone']);
- $subject = 'Contact Form';
- $email = trim($_POST['email']);
- $to = trim($_POST['to_email']);
- $message = stripslashes($_POST['message']);
- $error = '';
- // Check name
- if(!$name)
- {
- $error .= 'Please enter your name.<br />';
- }
- if(!$phone)
- {
- $error .= 'Please enter a phone number.<br />';
- }
- if(!$time)
- {
- $error .= 'Please select a time.<br />';
- }
- // Check email
- if(!$email)
- {
- $error .= 'Please enter an e-mail address.<br />';
- }
- if($email && !ValidateEmail($email))
- {
- $error .= 'Please enter a valid e-mail address.<br />';
- }
- // Check message (length)
- if(!$message || strlen($message) < 15)
- {
- $error .= "Please enter your message. It should have at least 15 characters.<br />";
- }
- if(!$error) // send email
- {
- $contents = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nPhone: $phone \n\nTime: $time \n\nMessage:\n $message";
- $mail = mail($to, $subject, $contents,
- 'From: '.$name.' <'.$email.'>\r\n'
- .'Reply-To: '.$email.'\r\n'
- .'X-Mailer: PHP/' . phpversion());
- if($mail)
- {
- echo 'OK';
- }
- }
- else
- {
- echo '<div class="notification_error">'.$error.'</div>'; // set up error div for jQuery/Ajax
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement