Skip to content

Instantly share code, notes, and snippets.

@marco-van-zomeren
Last active January 5, 2020 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marco-van-zomeren/f051f2b46c45e92ee8a70876d8649f6c to your computer and use it in GitHub Desktop.
Save marco-van-zomeren/f051f2b46c45e92ee8a70876d8649f6c to your computer and use it in GitHub Desktop.
Wordpress contact form without a plugin
<?php include("header.php"); ?>
<div class="page-container span_12 xs-md:p-0 pg">
<section class="span_12 container grid">
<main class="span_12 relative pg">
<?php
//response generation function
$response = "";
//function to generate response
function my_contact_form_generate_response($type, $message){
global $response;
if($type == "success") $response = "<div class='pg border border_3 currentColor color_succes border-radius_5'><p class='color_inherit m_0'>{$message}</p></div>";
else $response = "<div class='pg border border_3 currentColor color_error border-radius_5'><p class='color_inherit m_0'>{$message}</p></div>";
}
//response messages
$missing_content = "Please supply all information.";
$email_invalid = "Email address invalid.";
$message_unsent = "Message was not sent. Try Again.";
$message_sent = "Thanks! Your message has been sent.";
//user posted variables
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$message = $_POST['message_text'];
$human = $_POST['message_email'];
//php mailer variables
$to = get_option('admin_email');
$subject = "Someone sent a message from ".get_bloginfo('name');
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
if(!$human == (strlen(trim($string)) > 0)){
if($human == (strlen(trim($string)) > 1)) my_contact_form_generate_response("error", $not_human); //not human!
else {
//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
my_contact_form_generate_response("error", $email_invalid);
else //email is valid
{
//validate presence of name and message
if(empty($name) || empty($message)){
my_contact_form_generate_response("error", $missing_content);
}
else //ready to go!
{
$sent = wp_mail($to, $subject, strip_tags($message), $headers);
if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
}
}
}
}
else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="text_center">
<h1><?php the_title(); ?></h1>
</header>
<?php the_content(); ?>
<div class="span_12 grid row-gap" id="respond">
<form class="span_12 grid row-gap" action="<?php the_permalink(); ?>" method="post">
<div class="span_12 md-xl:span_6">
<label for="name">Name
<input type="text" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>"> </label>
</div>
<div class="span_12 md-xl:span_6">
<label for="message_email">Email
<input type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>"></label>
</div>
<div class="span_12">
<label for="message_text">Message</label>
<textarea class="js__textarea" type="text" name="message_text"><?php echo esc_textarea($_POST['message_text']); ?></textarea>
</div>
<input type="hidden" name="submitted" value="1">
<div class="span_12">
<?php echo $response; ?>
</div>
<div class="span_12">
<input class="float_right w_auto" type="submit"></p>
</div>
</form>
</div>
</article><!-- #post -->
<?php endwhile; // end of the loop. ?>
</div>
</main>
</section>
</div>
<?php include("footer.php"); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment