There are a lot of different plugins available to put a contact form on your WordPress site for beginners, but here is a simple way to add a contact form, with existing page.php file.

Create your custom template

Copy the code from your page.php file into a new file and name it contact.php

<?php
/* Template Name: Contact */
?>

The contact.php file should now look something like this:

<?php

/*

Template Name: Contact

*/

?>

<?php get_header() ?>

<div id=”container”>

<div id=”content”>

<?php the_post() ?>

<div id=”post-<?php the_ID() ?>”>

<div>

</div><!– .entry-content ->

</div><!– .post–>

</div><!– #content –>

</div><!– #container –>

<?php get_sidebar() ?>

<?php get_footer() ?>

Build the Form

Now you’ll need to create a simple contact with allowing only name, email and comment fields.  It should be pasted into your contact.php file.

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

<ul>

<li>

<label for=”contactName”>Name:</label>

<input type=”text” name=”contactName” id=”contactName” value=”” />

</li>

<li>

<label for=”email”>Email</label>

<input type=”text” name=”email” id=”email” value=”” />

</li>

<li>

<label for=”comments”>Message:</label>

<textarea name=”comments” id=”comments” rows=”20″ cols=”30″></textarea>

</li>

<li>

<button type=”submit”>Send </button>

</li>

</ul>

<input type=”hidden” name=”submitted” id=”submitted” value=”true” />

</form>

Error Handling

now, you need it to verify submission and that required fields have been filled. If there’s a required field left blank, an error will be displayed.

<?php

if(isset($_POST[‘submitted’])) {

if(trim($_POST[‘contactName’]) === ”) {

$naError = ‘Please enter your name’;

$hasError = true;

} else {

$na = trim($_POST[‘contactName’]);

}

&nbsp;

if(trim($_POST[’email’]) === ”)  {

$emError = ‘Please enter your email address’;

$hasError = true;

} else if (!preg_match(“/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i”, trim($_POST[’email’]))) {

$emError = ‘You entered an invalid email address’;

$hasError = true;

} else {

$em = trim($_POST[’email’]);

}

&nbsp;

if(trim($_POST[‘comments’]) === ”) {

$comError = ‘Please enter a message’;

$hasError = true;

} else {

if(function_exists(‘stripslashes’)) {

$com = stripslashes(trim($_POST[‘comments’]));

} else {

$com = trim($_POST[‘comments’]);

}

}

&nbsp;

if(!isset($hasError)) {

$emailTo = get_option(‘tz_email’);

if (!isset($emailTo) || ($emailTo == ”) ){

$emailTo = get_option(‘admin_email’);

}

$subject = ‘[PHP Snippets] From ‘.$na;

$body = “Name: $name \n\nEmail: $em \n\nComments: $com”;

$headers = ‘From: ‘.$name.’ <‘.$emailTo.’>’ . “\r\n” . ‘Reply-To: ‘ . $email;

&nbsp;

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

$emailSent = true;

}

&nbsp;

} ?>

Now, you’ll get an error message beneath the applicable field of the form if an email address is incorrect  or a required field is left blank. Your entire contact.php file should now look like this:

<?php

/*

Template Name: Contact

*/

?>

&nbsp;

<?php

if(isset($_POST[‘submitted’])) {

if(trim($_POST[‘conName’]) === ”) {

$nameError = ‘Please enter your name.’;

$hasError = true;

} else {

$na = trim($_POST[‘conName’]);

}

&nbsp;

if(trim($_POST[’email’]) === ”)  {

$emError = ‘Please enter your email address.’;

$hasError = true;

} else if (!preg_match(“/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i”, trim($_POST[’email’]))) {

$emError = ‘You entered an invalid email address.’;

$hasError = true;

} else {

$em = trim($_POST[’email’]);

}

&nbsp;

if(trim($_POST[‘comments’]) === ”) {

$comError = ‘Please enter a message.’;

$hasError = true;

} else {

if(function_exists(‘stripslashes’)) {

$com = stripslashes(trim($_POST[‘comments’]));

} else {

$com = trim($_POST[‘comments’]);

}

}

&nbsp;

if(!isset($hasError)) {

$emailTo = get_option(‘tz_email’);

if (!isset($emailTo) || ($emailTo == ”) ){

$emailTo = get_option(‘admin_email’);

}

$subject = ‘[PHP Snippets] From ‘.$name;

$body = “Name: $name \n\nEmail: $em \n\nComments: $com”;

$headers = ‘From: ‘.$name.’ <‘.$emailTo.’>’ . “\r\n” . ‘Reply-To: ‘ . $email;

&nbsp;

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

$emailSent = true;

}

&nbsp;

} ?>

<?php get_header(); ?>

<div id=”container”>

<div id=”content”>

&nbsp;

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

<div <?php post_class() ?> id=”post-<?php the_ID(); ?>”>

<h1><?php the_title(); ?></h1>

<div>

<?php if(isset($emailSent) && $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 } ?>

&nbsp;

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

<ul>

<li>

<label for=”contactName”>Name:</label>

<input type=”text” name=”contactName” id=”contactName” value=”<?php if(isset($_POST[‘contactName’])) echo $_POST[‘contactName’];?>” />

<?php if($nameError != ”) { ?>

<span><?=$nameError;?></span>

<?php } ?>

</li>

&nbsp;

<li>

<label for=”email”>Email</label>

<input type=”text” name=”email” id=”email” value=”<?php if(isset($_POST[’email’]))  echo $_POST[’email’];?>” />

<?php if($emailError != ”) { ?>

<span><?=$emailError;?></span>

<?php } ?>

</li>

&nbsp;

<li><label for=”comments”>Message:</label>

<textarea name=”comments” id=”comments” rows=”20″ cols=”30″><?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>

&nbsp;

<li>

<input type=”submit”>Send</input>

</li>

</ul>

<input type=”hidden” name=”submitted” id=”submitted” value=”true” />

</form>

<?php } ?>

</div><!– .entry-content –>

</div><!– .post –>

&nbsp;

<?php endwhile; endif; ?>

</div><!– #content –>

</div><!– #container –>

&nbsp;

<?php get_sidebar(); ?>

<?php get_footer(); ?>

Related Article


0 thoughts on “how to create a contact form in wordpress without plugin”

Leave a Reply

Latest Article

Blog

WHO: World is facing diabetes

The quantity of grown-ups living with diabetes has almost quadrupled to 422 million more than 30 years, the WHO cautioned, including the world is confronting the ailment which now influences about one in 11 individuals. Read more…

Blog

Back Pain

Back torment influences a great many people sooner or later in their life, activated by terrible stance, twisting ungracefully or lifting inaccurately. Most instances of back agony enhance inside of a couple of weeks or Read more…

Blog

Bitcoin cracks $5,000 mark for first time

LONDON: Bitcoin surged through the $5,000 level on Thursday out of the blue since the dispatch of the unregulated virtual cash over 8 years back. The cryptographic money struck another record high of $5,183.97 around Read more…