There are a lot of different plugins available to put a contact form on your WordPress site, 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(); ?>

Categories: BlogPHP

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

Leave a Reply

Related Article

Blog

World Badminton Championships: Sindhu eases into pre-quarterfinals

NEW DELHI: Top Indian shuttler PV Sindhu stormed into pre-quarterfinals of the women\’s singles accident at the apple Badminton Championships, with a advantageous beeline amateur achievement over Kim Hyo Min of Korea in Glasgow on Read more…

Blog

Premier League: Philippe Coutinho has not damaged relationship with Liverpool, says Jurgen Klopp

London: Liverpool administrator Jurgen Klopp says Philippe Coutinho would be accustomed back into the band because the active Brazilan attempts to force a move to Barcelona have not damaged their accord. Coutinho handed in a Read more…

Blog

Samsung Bixby Voice now available in 200 countries globally

After accession in the US a ages ago, Samsung today appear that it’s articulation abettor has now gone all-around. Samsung appear through it’s official blog that it’s Bixby articulation abettor available on the Galaxy S8 Read more…