Snippets

WordPress All-in-one Contact Form

If your like myself and hate using loads of plugins to do the simplest of things then this ALL-IN-ONE contact form is perfect for you. Honestly you literally just need to COPY & PASTE the snippet directly into your template and of you go. This contact form automatically grabs and uses the admin email address, blog title and website url for the form, creates error responses and displays an alternative thank you page on submit. It also has the WordPress ‘the_content’ feature so you can add additional content if needed and comes with a send response feature.

I’d love to hear any feedback on this and welcome all comments. I hope you all like and enjoy this small snippet.

<?php
		//If the form is submitted
		if(isset($_POST['submitted'])) {

			if(trim($_POST['checking']) !== '') {
				$captchaError = true;
			} else {

			if(trim($_POST['contactName']) === '') {
				$nameError = 'Name Required';
				$hasError = true;
			} else {
				$name = trim($_POST['contactName']);
			}

			if(trim($_POST['email']) === '')  {
				$emailError = 'Email Address Required';
				$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.';
				$hasError = true;
			} else {
				$email = trim($_POST['email']);
			}

			if(trim($_POST['comments']) === '') {
				$commentError = 'Message Required ';
				$hasError = true;
			} else {
			if(function_exists('stripslashes')) {
				$comments = stripslashes(trim($_POST['comments']));
			} else {
				$comments = trim($_POST['comments']);
			}
		}

		if(!isset($hasError)) {

			$emailTo = ''.$admin_email = get_option('admin_email').'';
			$subject = 'Contact Form Submission, '.$name.' - '.$blog_title = get_bloginfo('name').'';
			$sendCopy = trim($_POST['sendCopy']);
			$body = "Contact Form Submission,nnName: $name n Email Address: $email n Message: $comments";
			$headers = 'From: '.$blog_title = get_bloginfo('name').' - Contact Form <'.$emailTo.'>' . "rn" . 'Reply-To: ' . $email;

			mail($emailTo, $subject, $body, $headers);

			if($sendCopy == true) {
				$subject = 'Auto Response - '.$blog_title = get_bloginfo('name').'';
				$headers = 'From: '.$blog_title = get_bloginfo('name').' ';
				$bloghome = get_bloginfo('home');
				$value = get_option('my_field' , '');
				$body = "Dear $name, nn Thank you for submitting your details with $blog_title nnn Details Submitted:nnName: $name n Email Address: $email n Message: $comments nnn Keep In Touch:nnURL: $bloghome nEmail: $admin_email nnnn Contact Form Developed By AWKREATIVnURL: https://www.awkreativ.comnnnn";
				mail($email, $subject, $body, $headers);
			}

			$emailSent = true;

		}
	}
}
?>

<div class="contact-wrapper" >
<?php if(isset($emailSent) && $emailSent == true) { ?>
	<div class="half-col">
		<div class="thanks">
			<h1>Thanks, <?=$name;?></h1>
			<p>Your email was successfully sent.</p>
		</div>
	</div>
	<?php } else { ?>

	<?php if (have_posts()) : ?>
		<?php while (have_posts()) : the_post(); ?>

			<?php the_content(); ?>

			<?php if(isset($hasError) || isset($captchaError)) { ?>
				<p class="error">There was an error submitting the form.<p>
			<?php } ?>

			<form action="<?php the_permalink(); ?>" id="contactForm" method="post">

				<ol class="forms">
					<li>
						<label class="label">Name Required</label>
						<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="requiredField input" />
						<?php if($nameError != '') { ?>
							<span class="error">Name Required</span>
						<?php } ?>
					</li>

					<li>
						<label class="label">Email Address Required</label>
						<input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="requiredField email input" />
						<?php if($emailError != '') { ?>
							<span class="error">Email Address Required</span>
						<?php } ?>
					</li>
					<li>
						<label class="label">Message Required</label>
						<textarea name="comments" id="commentsText"  class="requiredField textarea"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
						<?php if($commentError != '') { ?>
							<span class="error-textarea" >
								Message Required
							</span>
						<?php } ?>
					</li>
					</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">Send a copy of this email to yourself</label>
					</li>

					<li class="screenreader">
						<label for="checking" class="screenReader">If you want to submit this form, do not enter anything in this field</label>
						<input type="text" name="checking" id="checking" class="screenReader" value="<?php if(isset($_POST['checking']))  echo $_POST['checking'];?>" />
					</li>

					<li class="buttons">
						<input type="hidden" name="submitted" id="submitted" value="true" />
						<button type="submit" class="email-submit">Submit Form</button>
					</li>

				</ol>
			</form>

		</div>
		<?php endwhile; ?>
	<?php endif; ?>
<?php } ?>

Source: http://awkreativ.com/blog/wp-all-in-one-contact-form/...

Share this snippet

 
  • Emilio

    This doesn’t work on a front page, does it? It does need to be into a page template.

  • Stevensen Liu

    http://www.mageewp.com/magee-theme.html