How to create contact us template page in wordpress

Dont use the plugins for contact us page in wordpress which will and unwanted code to your site.we given the simple code snippet for create contact us template page in wordpress.

create contact us template page in wordpress

Contact us page is important for every website. Many wordpress developers use the contact7 wordpress plugin for creating the contact page.

Dont use the plugins for contact us page in wordpress

If you use any contact us plugin then your site became little bit slow. because due to database custom quires you will some bandwidth.  My suggestion is use simple php code for that.

You need to just create the contact.php page in your wordpress theme folder and put the following code in that file.


<?php
/*
Template Name: Contact Us
*/
if($_POST[sent]){
 $error = "";
 if(!trim($_POST[your_name])){
 $error .= "<p>Please enter your name</p>";
 }
 if(!filter_var(trim($_POST[your_email]),FILTER_VALIDATE_EMAIL)){
 $error .= "<p>Please enter a valid email address</p>";
 }
 if(!trim($_POST[your_message])){
 $error .= "<p>Please enter a message</p>";
 }
 if(!trim($_POST[your_subject])){
 $error .= "<p>Please enter a message</p>";
 }
 if(!$error){
 $email = wp_mail(get_option("admin_email"),trim($_POST[your_name])." sent you a message from ".get_option("blogname"),stripslashes(trim($_POST[your_message])),"From: ".trim($_POST[your_name])." <".trim($_POST[your_email]).">\r\nReply-To:".trim($_POST[your_email]));
 }
}
?>
<?php get_header(); ?>

 <section id="primary">
 <div id="content" role="main">

<div id="archives">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
 <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
 <h1><?php the_title(); ?></h1>
 <div>
 <?php if($email){ ?>
 <p><strong>Message succesfully sent. I'll reply as soon as I can</strong></p>
 <?php } else { if($error) { ?>
 <p><strong>Your messange hasn't been sent</strong><p>
 <?php echo $error; ?>
 <?php } else { the_content(); } ?>
 <form action="<?php the_permalink(); ?>" id="contact_me" method="post">
 <input type="hidden" name="sent" id="sent" value="1" />
 <div id="form">
 <div id="lebel">Your Name (required)</div>
 <div id="input-field"><input type="text" name="your_name" id="your_name" value="<?php echo $_POST[your_name];?>" /></div>
 <div id="lebel">Your Email (required)</div>
 <div id="input-field"><input type="text" name="your_email" id="your_email" value="<?php echo $_POST[your_email];?>" /></div>
 <div id="lebel">Subject</div>
 <div id="input-field"><input type="text" name="your_subject" id="your_subject" value="<?php echo $_POST[your_subject];?>" /></div>
 <div id="lebel">Your Message(required)</div>
 <div id="input-field"><textarea name="your_message" id="your_message"><?php echo stripslashes($_POST[your_message]); ?></textarea></div>
 <div id="lebel"> </div>
 <div id="input-field"><input type="submit" name = "send" value = "Contact Us" /></div>
 </div>

 </form>
 <?php } ?>
 </div><!-- .entry-content -->
 </div><!-- #post-## -->
 <?php endwhile; ?>

</div>
 </div><!-- #content -->
 </section><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

create contact us template page in wordpress
create contact us template page in wordpress

 

 

If you need more information about then check following articles.

https://purabtech.in/create-contact-page-wihout-wordpress-plugin/

https://purabtech.in/send-smtp-email-wordpress-plugin/

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

4 thoughts on “How to create contact us template page in wordpress”

  1. How do I send this information to a databse instead – basically I would like to send to database, receive a notification and also send email to the person that contact me. Could this be achieved at one go? Thanks!

Leave a Reply to John Cancel reply

Your email address will not be published.