Skip to content

Instantly share code, notes, and snippets.

@setuix
Last active August 29, 2015 14:00
Show Gist options
  • Save setuix/ac87edc261b5e7699564 to your computer and use it in GitHub Desktop.
Save setuix/ac87edc261b5e7699564 to your computer and use it in GitHub Desktop.
?php
/*
Template Name: Contact
*/
?
?php
if(isset($_POST['submitted'])) {
if(trim($_POST['contactName']) === ”) {
$nameError = ‘Please enter your name.’;
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
if(trim($_POST['email']) === ”) {
$emailError = ‘Please enter your email address.’;
$hasError = true;
} else if (!preg_match( quot;/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i quot;, trim($_POST['email']))) {
$emailError = ‘You entered an invalid email address.’;
$hasError = true;
} else {
$email = trim($_POST['email']);
}
if(trim($_POST['comments']) === ”) {
$commentError = ‘Please enter a message.’;
$hasError = true;
} else {
if(function_exists(‘stripslashes’)) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
if(!isset($hasError)) {
$emailTo = get_option(‘tz_email’);
if (!isset($emailTo) || ($emailTo == ”) ){
$emailTo = get_option(‘admin_email’);
}
$subject = ‘[PHP Snippets] From ‘.$name;
$body = quot;Name: $name \n\nEmail: $email \n\nComments: $comments quot;;
$headers = ‘From: ‘.$name.’ ’.$emailTo.’ ’ . quot;\r\n quot; . ‘Reply-To: ‘ . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
} ?
?php get_header(); ?
div id= quot;container quot;
div id= quot;content quot;
?php if (have_posts()) : while (have_posts()) : the_post(); ?
div ?php post_class() ? id= quot;post- ?php the_ID(); ? quot;
h1 ?php the_title(); ? /h1
div
?php if(isset($emailSent) amp; amp; $emailSent == true) { ?
div
p Thanks, your email was sent successfully. /p
/div
?php } else { ?
?php the_content(); ?
?php if(isset($hasError) || isset($captchaError)) { ?
p Sorry, an error occured. p
?php } ?
form action= quot; ?php the_permalink(); ? quot; id= quot;contactForm quot; method= quot;post quot;
ul
li
label for= quot;contactName quot; Name: /label
input type= quot;text quot; name= quot;contactName quot; id= quot;contactName quot; value= quot; ?php if(isset($_POST['contactName'])) echo $_POST['contactName'];? quot; /
?php if($nameError != ”) { ?
span ?=$nameError;? /span
?php } ?
/li
li
label for= quot;email quot; Email /label
input type= quot;text quot; name= quot;email quot; id= quot;email quot; value= quot; ?php if(isset($_POST['email'])) echo $_POST['email'];? quot; /
?php if($emailError != ”) { ?
span ?=$emailError;? /span
?php } ?
/li
li label for= quot;commentsText quot; Message: /label
textarea name= quot;comments quot; id= quot;commentsText quot; rows= quot;20 quot; cols= quot;30 quot; ?php if(isset($_POST['comments'])) { if(function_exists(‘stripslashes’)) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ? /textarea
?php if($commentError != ”) { ?
span ?=$commentError;? /span
?php } ?
/li
li
input type= quot;submit quot; Send email /input
/li
/ul
input type= quot;hidden quot; name= quot;submitted quot; id= quot;submitted quot; value= quot;true quot; /
/form
?php } ?
/div !– .entry-content –
/div !– .post –
?php endwhile; endif; ?
/div !– #content –
/div !– #container –
?php get_sidebar(); ?
?php get_footer(); ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment