Posted on Leave a comment

Auto Generate Contact Form in WordPress Without Plugin

WordPress contact us page is an essential part of any business site, personal blog, or online portfolio page. A custom auto generate contact form use PHP without plugin that make your database and WordPress load less that boost your website, this contact form help your audience ask you for more details about your products or services.

This tutorial gives step-by-step instructions on creating a auto generate contact us WordPress page without using WordPress plugin. The interesting part is you can add more form input fields this PHP code will auto deduction this field and send it to you mail account.

Follow the instructions below in order to add contact page function for custom PHP or HTML form:

Step 1: Create Contact us page

Let’s see how to create a fully functional auto generate Contact Us page in WordPress without using plugin. Login your dashboard, go to page and create new page name it contact or contact us whatever you like.

Step 2: Add Custom form and input fields

A contact form is a simple element with standard fields. In this guide, we won’t focus on the styling part. Simply add the following code into the ‘Custom HTML’

<form action="Link_Your_PHP_file_Upload.php" method="get">
<p><label> Your name<br><span><input type="text" name="your-name" value="" size="40" required="required"></span></label></p>
<p><label> Your email<br><span><input type="email" name="your-email" value="" size="40" required="required"></span></label></p>
<p><label> Subject<br><span><input type="text" name="subject" value="" size="40"></span></label></p>
<p><label> Your message<br><textarea name="your-message" cols="40" rows="10" aria-invalid="false"></textarea></label></p>
<p><button type="submit">Submit</button></p>
</form>

Now, publish the page and add it to the menu.

Step 3: Add PHP Code for processing the Form input

PHP code will handle the Sending the Actual mail, with auto generate fields example you want to add more fields this will auto generate this fields through this PHP code.

Simply add the following code into your hosting file manager, create file name add this code.

<?php
$conPage='https://www.yourdomain.com/contact-us/'; // (EDIT) Your website contact page url,

$domNam=str_replace('www.','',$_SERVER['SERVER_NAME']);
$refEr=$_SERVER['HTTP_REFERER'];

if($conPage==$refEr){ // validate get only data from your mention website url;

$parameter = $_SERVER['QUERY_STRING'];
$idArray = explode('&',$parameter);
?><?php

$to = 'yougetmail@gmail.com'; // (EDIT) all contat field send to this mail;

$subject = "Contact - $domNam";

$from = 'yourcomapny@mail.com'; // (EDIT) your company mail address sending from
 
$headers  = 'MIME-Version: 1.0' . "\r\n";  // To send HTML mail, the Content-type header must be set
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 // Create email headers
$headers .= "From: '$domNam' ".$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
 
// Custome html form fiels visible into your email
$message = '<table width="690" cellspacing="0" cellpadding="0" border="0" align="center"><tbody><tr><td bgcolor="#F0F0F0">
<table width="640" cellspacing="0" cellpadding="0" border="0" align="center">
<tbody><tr><td height="16"></td></tr><tr>
<td style="font-family: arial,Arial,sans-serif;text-transform:uppercase;font-size: 25px;">
<b>'.str_replace('www.','',$_SERVER['SERVER_NAME']).'</b></td></tr><tr><td height="16"></td></tr>'; ?>
<?php
 foreach ($idArray as $index => $avPair) { list($ignore, $value) = explode("=", $avPair); $id[$index] = $value;  $message .='
<tr>
<td align="left" bgcolor="#FFFFFF"><div style="border-style:solid;border-width:1px;border-color:#cccccc;    margin-bottom: -1px;">
<table width="625" cellspacing="0" cellpadding="0" border="0" align="center"><tbody><tr><td height="15" colspan="3"></td>
</tr><tr><td width="15"></td><td width="600"><div style="font-family:arial,Arial,sans-serif"><table cellspacing="0" cellpadding="0" border="0">
<tbody><tr><td width="100" style="color:#787878;text-transform: capitalize;font-size:13px;"><b>'. str_replace('-',' ',$ignore).': </b>
</td><td width="15"></td><td><div style="color:#929292;line-height:24px;font-size: 14px;">'. urldecode(str_replace('+',' ',$id[$index])).'
</div></td></tr></tbody></table></div></td><td width="15"></td></tr><tr><td height="15" colspan="3"></td></tr></tbody></table>
</div></td></tr>'; } ?><?php
$message .= '<tr><td height="16"></td></tr><tr><td align="left"><table cellspacing="0" cellpadding="0" border="0" align="center">
<tbody><tr><td width="498"><div style="font-family:arial,Arial,sans-serif;font-size:11px;color:#999999;line-height:13px;text-align: center;"> ©  '.date('Y').' Developed by <a style="color: #999999;text-decoration: none;" href="https://techboto.com/" target="_blank">TECHboto</a></div>
</td></tr></tbody></table></td></tr><tr><td height="22"></td></tr></tbody></table></td></tr></tbody></table>';

//  Sending email
 if(mail($to, $subject, $message, $headers)){
echo '<h2 style="font-family: system-ui;"><b>Your message has been successfully sent. We will contact you very soon!</b></h2>';
echo"<br>";
echo "<p style='text-align: center;font-family: system-ui;'>Loading...</p>";
header( 'refresh:1;url='.$_SERVER["HTTP_REFERER"] ); // page waiting 1 seconds and redirect
 } else{ echo 'Error Unable to send message. Please try again.';  header( 'refresh:1;url='.$_SERVER["HTTP_REFERER"] );  // page waiting 1 seconds and redirect}
?><?php } else{ echo "<p>Sorry, You are not allowed to access this page!</p>";  exit(); } ?>

Step 4: Edit PHP file to auto validating

This step will send mail to your mail address, if you can’t edit your mail not getting to your mail address, you follow this steps carefully and very important, this will auto generate custom form fields and send it to your mail address.

Now, you need to edit some line of code, on line number one find ‘$conPage‘ and replace to your recent create contact page url example (https://yourdomain.com/contact-us/).

Two more line to be edit this will send mail to your mail address, find ‘$to‘and edit sending mail address example (yougetmail@gmail.com), and one more line you edit find ‘$from‘ edit to receving from example (yourcomapny@mail.com).

Step 5: WordPress form connect PHP code

This time we update our recent create contact page ‘action‘ this form sending data to php upload file directory on hosting connect example (https://yourdomain.com/uploadPHPfiles.php) for more reference you watch this image bellow.

Now, As a result, you get a auto generate, fully functional contact us page that you made without using a plugin. There are many other things that you can add to your WordPress contact us page. One thing that comes to mind is adding a CAPTCHA or some other. We can do next time to adding CAPTCHA to this contact-us page.

This is final look for sending message to mail inbox

If you have followed all steps, but the problem remains, we can help you to configure your custom form you can comment me on comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *