Skip to content

Instantly share code, notes, and snippets.

@tomharrigan
Created February 9, 2014 17:41
Show Gist options
  • Select an option

  • Save tomharrigan/8902781 to your computer and use it in GitHub Desktop.

Select an option

Save tomharrigan/8902781 to your computer and use it in GitHub Desktop.
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Template Name: Contact Form
*
* The contact form page template displays the a
* simple contact form in your website's content area.
*
* @package WooFramework
* @subpackage Template
*/
global $woo_options;
get_header();
$nameError = '';
$emailError = '';
$commentError = '';
$verifyError = '';
//If the form is submitted
if( isset( $_POST['submitted'] ) ) {
//Check to see if the honeypot captcha field was filled in
if( trim( $_POST['checking'] ) !== '' ) {
$captchaError = true;
} else {
//Check to make sure that the name field is not empty
if( trim( $_POST['contactName'] ) === '' ) {
$nameError = __( 'You forgot to enter your name.', 'woothemes' );
$hasError = true;
} else {
$name = trim( $_POST['contactName'] );
}
//Check to make sure sure that a valid email address is submitted
if( trim( $_POST['email'] ) === '' ) {
$emailError = __( 'You forgot to enter your email address.', 'woothemes' );
$hasError = true;
} else if ( ! eregi( "^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email'] ) ) ) {
$emailError = __( 'You entered an invalid email address.', 'woothemes' );
$hasError = true;
} else {
$email = trim( $_POST['email'] );
}
//Check to make sure that the verify field is not empty or incorrect
if ( ( ! trim($_POST['contact-verify'] ) || ( 19 != intval( trim($_POST['contact-verify']) ) ) ) ) {
$verifyError = __( 'The verification code entered is incorrect. Please try again.', 'woothemes' );
$hasError = true;
}
//Check to make sure comments were entered
if( trim( $_POST['comments'] ) === '' ) {
$commentError = __( 'You forgot to enter your comments.', 'woothemes' );
$hasError = true;
} else {
$comments = stripslashes( trim( $_POST['comments'] ) );
}
//If there is no error, send the email
if( ! isset( $hasError ) ) {
$emailTo = get_option( 'woo_contactform_email' );
$subject = __( 'Contact Form Submission from ', 'woothemes' ).$name;
$sendCopy = trim( $_POST['sendCopy'] );
$body = __( "Name: $name \n\nEmail: $email \n\nComments: $comments", 'woothemes' );
$headers = __( 'From: ', 'woothemes') . "$name <$email>" . "\r\n" . __( 'Reply-To: ', 'woothemes' ) . $email;
wp_mail( $emailTo, $subject, $body, $headers );
if( $sendCopy == true ) {
$subject = __( 'You emailed ', 'woothemes' ) . get_bloginfo( 'title' );
$headers = __( 'From: ', 'woothemes' ) . "$name <$emailTo>";
wp_mail( $email, $subject, $body, $headers );
}
$emailSent = true;
}
}
}
?>
<script type="text/javascript">
<!--//--><![CDATA[//><!--
jQuery(document).ready(function() {
jQuery( 'form#contactForm').submit(function() {
jQuery( 'form#contactForm .error').remove();
var hasError = false;
jQuery( '.requiredField').each(function() {
if(jQuery.trim(jQuery(this).val()) == '') {
var labelText = jQuery(this).prev( 'label').text();
jQuery(this).parent().append( '<span class="error"><?php _e( 'You forgot to enter your', 'woothemes' ); ?> '+labelText+'.</span>' );
jQuery(this).addClass( 'inputError' );
hasError = true;
} else if(jQuery(this).hasClass( 'email')) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if(!emailReg.test(jQuery.trim(jQuery(this).val()))) {
var labelText = jQuery(this).prev( 'label').text();
jQuery(this).parent().append( '<span class="error"><?php _e( 'You entered an invalid', 'woothemes' ); ?> '+labelText+'.</span>' );
jQuery(this).addClass( 'inputError' );
hasError = true;
}
} else if(jQuery(this).hasClass( 'verify')) {
var verifyNum = 19;
if(!verifyNum.test(jQuery.trim(jQuery(this).val()))) {
var labelText = jQuery(this).prev( 'label').text();
jQuery(this).parent().append( '<span class="error"><?php _e( 'You entered an invalid', 'woothemes' ); ?> '+labelText+'.</span>' );
jQuery(this).addClass( 'inputError' );
hasError = true;
}
}
});
if(!hasError) {
var formInput = jQuery(this).serialize();
jQuery.post(jQuery(this).attr( 'action'),formInput, function(data){
jQuery( 'form#contactForm').slideUp( "fast", function() {
jQuery(this).before( '<p class="tick"><?php _e( '<strong>Thanks!</strong> Your email was successfully sent.', 'woothemes' ); ?></p>' );
});
});
}
return false;
});
});
//-->!]]>
</script>
<div id="content">
<div class="col-full">
<?php woo_main_before(); ?>
<section id="main" class="col-left">
<article id="contact-page" class="type-page">
<?php if( isset( $emailSent ) && $emailSent == true ) { ?>
<p class="info"><?php _e( 'Your email was successfully sent.', 'woothemes' ); ?></p>
<?php } else { ?>
<?php if ( have_posts() ) { ?>
<?php while ( have_posts() ) { the_post(); ?>
<header>
<h1><?php the_title(); ?></h1>
</header>
<section class="entry">
<?php the_content(); ?>
<div class="location-twitter fix">
<?php if ( isset( $woo_options['woo_contact_panel'] ) && $woo_options['woo_contact_panel'] == 'true' ) { ?>
<section id="office-location"<?php if ( ( isset( $woo_options['woo_contact_twitter'] ) && $woo_options['woo_contact_twitter'] != '' ) || ( isset($woo_options['woo_contact_subscribe_and_connect']) && $woo_options['woo_contact_subscribe_and_connect'] == 'true' ) ) { ?> class="col-left"<?php } ?>>
<?php if (isset($woo_options['woo_contact_title'])) { ?><h3><?php echo esc_html( $woo_options['woo_contact_title'] ); ?></h3><?php } ?>
<ul>
<?php if (isset($woo_options['woo_contact_title']) && $woo_options['woo_contact_title'] != '' ) { ?><li><?php echo nl2br( esc_html( $woo_options['woo_contact_address'] ) ); ?></li><?php } ?>
<?php if (isset($woo_options['woo_contact_number']) && $woo_options['woo_contact_number'] != '' ) { ?><li><?php _e('Tel:','woothemes'); ?> <?php echo esc_html( $woo_options['woo_contact_number'] ); ?></li><?php } ?>
<?php if (isset($woo_options['woo_contact_fax']) && $woo_options['woo_contact_fax'] != '' ) { ?><li><?php _e('Fax:','woothemes'); ?> <?php echo esc_html( $woo_options['woo_contact_fax'] ); ?></li><?php } ?>
<?php if (isset($woo_options['woo_contactform_email']) && $woo_options['woo_contactform_email'] != '' ) { ?><li><?php _e('Email:','woothemes'); ?> <a href="mailto:<?php echo esc_attr( $woo_options['woo_contactform_email'] ); ?>"><?php echo esc_html( $woo_options['woo_contactform_email'] ); ?></a></li><?php } ?>
</ul>
</section>
<?php } ?>
<div class="contact-social<?php if ( ( isset( $woo_options['woo_contact_panel'] ) && $woo_options['woo_contact_panel'] == 'true' ) && ( ( isset( $woo_options['woo_contact_twitter'] ) && $woo_options['woo_contact_twitter'] != '' ) || ( isset($woo_options['woo_contact_subscribe_and_connect']) && $woo_options['woo_contact_subscribe_and_connect'] == 'true' ) ) ) { ?> col-right<?php } ?>">
<?php if ( isset( $woo_options['woo_contact_twitter'] ) && $woo_options['woo_contact_twitter'] != '' ) { ?>
<section id="twitter">
<h3>Twitter</h3>
<ul id="twitter_update_list_123"><li></li></ul>
<?php echo woo_twitter_script(123, $woo_options['woo_contact_twitter'],1); ?>
</section>
<?php } ?>
<?php if ( isset($woo_options['woo_contact_subscribe_and_connect']) && $woo_options['woo_contact_subscribe_and_connect'] == 'true' ) { woo_subscribe_connect(); } ?>
</div>
</div><!-- /.location-twitter -->
</section>
<?php if ( isset($woo_options['woo_contactform_map_coords']) && $woo_options['woo_contactform_map_coords'] != '' ) { $geocoords = $woo_options['woo_contactform_map_coords']; } else { $geocoords = ''; } ?>
<?php if ($geocoords != '') { ?>
<?php woo_maps_contact_output("geocoords=$geocoords"); ?>
<?php echo do_shortcode( '[hr]' ); ?>
<?php } ?>
<?php if( isset( $hasError ) || isset( $captchaError ) ) { ?>
<p class="alert"><?php _e( 'There was an error submitting the form.', 'woothemes' ); ?></p>
<?php } ?>
<?php if ( get_option( 'woo_contactform_email' ) == '' ) { ?>
<?php echo do_shortcode( '[box type="alert"]' . __( 'E-mail has not been setup properly. Please add your contact e-mail!', 'woothemes' ) . '[/box]' ); ?>
<?php } ?>
<form action="<?php the_permalink(); ?>" id="contactForm" method="post">
<ol class="forms">
<li><label for="contactName"><?php _e( 'Name', 'woothemes' ); ?></label>
<input type="text" name="contactName" id="contactName" value="<?php if( isset( $_POST['contactName'] ) ) { echo esc_attr( $_POST['contactName'] ); } ?>" class="txt requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?php echo $nameError;?></span>
<?php } ?>
</li>
<li><label for="email"><?php _e( 'Email', 'woothemes' ); ?></label>
<input type="text" name="email" id="email" value="<?php if( isset( $_POST['email'] ) ) { echo esc_attr( $_POST['email'] ); } ?>" class="txt requiredField email" />
<?php if($emailError != '') { ?>
<span class="error"><?php echo $emailError;?></span>
<?php } ?>
</li>
<li class="textarea"><label for="commentsText"><?php _e( 'Message', 'woothemes' ); ?></label>
<textarea name="comments" id="commentsText" rows="20" cols="30" class="requiredField"><?php if( isset( $_POST['comments'] ) ) { echo esc_textarea( $_POST['comments'] ); } ?></textarea>
<?php if( $commentError != '' ) { ?>
<span class="error"><?php echo $commentError; ?></span>
<?php } ?>
</li>
<li>
<label for="contact"><?php _e( '7 + 12 = ?', 'woothemes' ); ?></label>
<input type="text" name="contact-verify" value="<?php if( isset( $_POST['contact-verify'] ) ) { echo esc_attr( $_POST['contact-verify'] ); } ?>" onfocus="if ( this.value == '<?php esc_attr_e( '7 + 12 = ?', 'woothemes' ); ?>' ) { this.value = ''; }" onblur="if ( this.value == '' ) { this.value = '<?php esc_attr_e( '7 + 12 = ?', 'woothemes' ); ?>'; }" class="txt verify requiredField" />
<?php if($verifyError != '') { ?>
<span class="error"><?php echo $verifyError;?></span>
<?php } ?>
</li>
<li class="inline"><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', 'woothemes' ); ?></label></li>
<li class="screenReader"><label for="checking" class="screenReader"><?php _e( 'If you want to submit this form, do not enter anything in this field', 'woothemes' ); ?></label><input type="text" name="checking" id="checking" class="screenReader" value="<?php if( isset( $_POST['checking'] ) ) { echo esc_attr( $_POST['checking'] ); } ?>" /></li>
<li class="buttons"><input type="hidden" name="submitted" id="submitted" value="true" /><input class="submit button" type="submit" value="<?php esc_attr_e( 'Submit', 'woothemes' ); ?>" /></li>
</ol>
</form>
<?php
} // End WHILE Loop
}
}
?>
</article><!-- /#contact-page -->
</section><!-- /#main -->
<?php woo_main_after(); ?>
<?php get_sidebar(); ?>
</div><!-- /.col-full -->
</div><!-- /#content -->
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment