Use the WordPress comment form as your contact form
Thursday, August 13th, 2009It seems peculiar to me that you need a plugin to create a contact form when the WordPress comment form does a good job of collecting comments, filtering out spam (with the help of Akismet) and e-mailing you when you get a message. Unless you require extra fields, I’m perfectly happy to use the normal comment form. There are problems with this; firstly pages don’t have the comments template by default and secondly, it usually has a header called “Leave a reply” or something similar. Here’s how to get round the issues.
Include the comments form on a Page
To enable the comments template, simply open page.php (in the wp-themes/theme-name directory) and paste
<?php comments_template(); ?>
before the
<?php endwhile; endif; ?>
statement. If you don’t want the comments template on every Page, you can switch them off when creating pages in the admin panel or you could create a new template just for the contact page. To do this, save the page in your theme directory as “page-contact.php” with this code at the very top:
<?php /* Template Name: Contact Page */ ?>
When you create your contact page in the admin panel, be sure to select the template ‘Contact Page’.
Change the header
To change what the header says (instead of “Leave a comment”) open comments.php (again, in your wp-themes/theme-name directory) and replace
<h3 id="respond">Leave a Reply</h3>
with this code:
<?php if ( is_page('Contact') ) { ?>
<h3 id="respond">Contact Us</h3>
<?php } else { ?>
<h3 id="respond">Leave a Reply</h3>
<?php } ?>
Your page name must be ‘Contact’ for this to work. You can also use this “if” statement to remove fields or change the submit buttons for example.
That’s it!
You can see my contact page in action here, the header reads “Contact Phil” and the submit button says “Submit Message”. Look at the comments form below and spot the differences.
With a couple of tweaks, you too can use the comments form to allow visitors to contact you without going through the hassle of finding a contact form plugin and styling it to look like the rest of your WordPress site. Simple!
