Once you’ve got an established blog you’ll most likely want a contact page with a contact form on it. Whilst there are a few plugins that do this for you, I tend to find that they either bloat your pages with additional CSS code in the header (CSS code should always be put in an external stylesheet whenever possible), badly written form markup, or probably the worst culprit, the PHP code doesn’t validate the information in the form to help prevent spam or email header injection.
First of all we need to create a new page template. Take your page.php template file (or index.php if you don’t have a specific page template file) and save it as contact.php. At the top of the file, just after the opening PHP tag, insert the following:
/*
* Template Name: Contact Page
*/
This will now show up as a page template when you come to create your static Page in WordPress.
In your template file, find the template tag the_content(). It may differ slightly to the basic tag, or it may even be the_excerpt() (if it is then change that to the_content();). Just below this tag you want to insert your form markup. You may want to have additional fields, if so just duplicate the appropriate code and edit it for each additional field. Do not remove the fields already in the form however, else the PHP code to validate the form will not work.
<form id="contactform" method="post" action="/2008/08/24/create-a-contact-page-i/"> <fieldset><legend>Contact Form</legend> <div> <label for="cfname">Name: <em>(required)</em></label> <input type="text" name="cfname" id="cfname" size="30" /> </div> <div> <label for="cfemail">Email: <em>(required)</em></label> <input type="text" name="cfemail" id="cfemail" size="30" /> </div> <!-- Add any more form fields that you want to add here --> <div> <label for="cfmessage">Your Message: <em>(required)</em></label> <textarea name="cfmessage" id="cfmessage" cols="30" rows="8"></textarea> </div> </fieldset> <div><input type="submit" value="Submit" name="cfsubmit" id="cfsubmit" /></div> </form>
To explain a couple of points.
For those who are not sure of styling their form, here’s some recommended CSS code:
fieldset {
width: 500px;
border:none;
border-top: 1px solid #999;
padding: 10px;
margin-top: 10px;
}
legend { font-weight: bold; padding: 0 5px; }
#contactform div { margin: 5px 0; clear: both; }
#contactform label { width: 150px; float: left; margin-right: 10px }
Next week I’ll go through the validation code that we’ll use to ensure your form is secure and protected.
Author comments are in a darker gray color for you to easily identify the posts author in the comments
Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums
It’s another good tip to try. Thank you.
I never really thought about putting a contact page on my blog. I guess it is something worth doing and making it easier for [eople to contact me.
Very simple form …
This looks like it could be really helpful, but … stupid question #1: How do you create a php page template? I have no idea!
Thanks for the tip, however apart of my lack of PHP knowledge, so far I’ve been very pleased with cforms II plug-ins.
@Deb, you’ll need to read the earlier post of Page Templates – Create a Links Page for an indepth look at creating a page template.
@Budhi, cforms II looks alright but is quite bloated codewise for a basic form. Also, my ‘WordPress Design and Coding’ posts wouldn’t amount to much if I just said use a plugin
These posts are for people who would rather learn how to do things themselves or at least learn how to modify what they already have.