How to Create a Contact Page Template for your WordPress Theme

There are lots of wordpress plugin in wordpress plugin directory to add a contact form in your wordpress blog; less plugin added in your site also less of server space you’ve taken, creating a built-in page template in your wordpress site is very useful. In this tutorial I will explain how you can create and add Contact Form in your wordpress theme.

Step 1: Create a page template

The first step is to create an empty page template, page-contact.php, now, open the empty PHP file and add these comment code right at the beginning of the file, here’s the code.

We need to add the code at the beginning of the file to make sure WordPress will read our file as a valid custom Page Template.

Here’s how the Contact Page template looks.

The template should now available in template dropdown list, so now, lets add a Page in wordpress dashboard and under Page Attribute box select the Page Template we’ve created a while ago.

Step 2: Build the form

Building a form or Custom Page template form in wordpress is just like building in normal HTML form, so you don’t need to worry about.

The HTML code above is pretty simple and self-explanatory; I just add one hidden field to check if the form submitted is the contact form.

Step 3: Adding PHP code

Okay now lets add PHP code to validate and verify the data being submitted in the form, we need to add these PHP code above the HTML code in order the form to work.

If the fields are correctly field up then we will send email message to the site admin, otherwise it thrown an error message to the user.

That’s it; the form should now validate then inputted or empty data.

Below is the complete and running Contact form page template, you can copy and paste to test it yourself 😉

That’s it you now have a complete and running built-in contact form page added in your wordpress theme.

by Ryan Sutana

  • Lina

    Hi, I tried this and the page works but there is no email sent to admin. What is wrong?
    I have tried others that don’t work either, like this one
    so I would really like to know if there’s something I need to change for the email to really get sent. I don’t want to have to use a plugin for this.

    • Lina

      Ok, I got it working. The admin email was wrong in the database. But I would like to know if it’s possible to have the fields empty when the message is successfully sent? Now the filled-in text remains until you leave and return to the page.
      Thank you,

      • Yes, actually the message is not empty it’s the email body.

        Ryan S