Skip to content

Instantly share code, notes, and snippets.

@johnathan-sewell
Created September 19, 2011 08:55
Show Gist options
  • Save johnathan-sewell/1226169 to your computer and use it in GitHub Desktop.
Save johnathan-sewell/1226169 to your computer and use it in GitHub Desktop.
Sample code for a Wordpress contact page which emails a form submission
<?php
/*
Template Name: Contact Page
*/
/**
* The template for displaying the Contact page.
*
*
* @package WordPress
* @subpackage MyTemplate
* @since MyTemplate 1.0
*/?>
<?php
if(isset($_POST['submitted'])) {
if(trim($_POST['contactName']) === '') {
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
if(trim($_POST['role-title']) === '') {
$hasError = true;
} else {
$role = trim($_POST['role-title']);
}
if(trim($_POST['company']) === '') {
$hasError = true;
} else {
$company = trim($_POST['company']);
}
if(trim($_POST['email']) === '') {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
if(!isset($hasError)) {
$sep = "\r\n";
$to ="[email protected]";
$subject = "New enquiry";
$body = "Name: " .$name .$sep
."Role/title: " .$role .$sep
."Company: " .$company .$sep
."Email: " .$email .$sep .$sep
."This email was generated from http://" .$_SERVER['SERVER_NAME'] .$_SERVER['REQUEST_URI'];
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$success = mail($to ,$subject ,$body, $headers);
}
}
?>
<?php get_header(); ?>
<div class="content" id="wrapper">
<?php if($success) { ?>
<strong>Thank you for contacting us. A response will be sent to you soon.</strong><br>
<?php }else{ ?>
<form action="<?php the_permalink() ?>" method="post">
<input type="hidden" name="submitted" id="submitted" value="true" />
<?php if($hasError) { ?>
<strong>Please fill in all the required details and try again.</strong>
<?php } ?>
<label>Name:*</label>
<input type="text" name="contactName" class="input" value="<?php echo($name) ?>"><br>
<label>Role/title:*</label>
<input type="text" name="role-title" class="input" value="<?php echo($role) ?>"><br>
<label>Company:*</label>
<input type="text" name="company"class="input" value="<?php echo($company) ?>"><br>
<label>Email:*</label>
<input type="text" name="email" class="input" value="<?php echo($email) ?>"><br>
<input type="image" src="/images/register.jpg" class="button"><br>
</form>
<?php } ?>
</div>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment