PDA

View Full Version : Wordpress Tip #320 (not really) Using the comments section for a contact form.



Harold Mansfield
10-27-2009, 10:55 PM
This is an oldie but a goodie.
Many times when you use WP as a static or informational website, you may disable the comments so as not to have that big comment box sitting below your posts or pages when you really aren't looking for comments.

The comments section has everything that a standard contact form has:
A field for "Name", "Email", "Website", and a box for additional comments..so why not use it ?

You can use it as a mini contact form that is visible below all of the content that you choose.

This takes just a little editing of the "comments.php" file of your theme.

First you want to look for the line of code that is the title (what makes it say "Comments"), it should look like, or similar to this:


<h3><?php comment_form_title( 'Comments', 'Comment &#37;s' ); ?></h3>

Just change 'Comments' to "Contact Me" or what ever you want it to say.

You can leave the input fields the same, they will probably look something like this:



<?php else : ?>

[I]<p><input type="text" name="author" id="author" value="<?php echo esc_attr($comment_author); ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="email" id="email" value="<?php echo esc_attr($comment_author_email); ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="email"><small>E-Mail <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="url" id="url" value="<?php echo esc_attr($comment_author_url); ?>" size="22" tabindex="3" />
<label for="url"><small>Website (if Applicable)</small></label></p>

<?php endif; ?>

But you probably want to change the Submit Comment button. Look for this code a little further down:

<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
<?php comment_id_fields(); ?>
and change "Submit Comment" (or whatever it says) to "Send" or what ever you want it to say...and save.

Now from your Dashboard ->Settings ->Discussion, set your comments to "Any User can comment" and "Comments Must be approved by Moderator", and that should do it.

Now you have a mini contact form that you can opt to display at the bottom of any page or post that should look like this (http://www.haroldmansfield.com/2009/10/wordpress-websites-and-blogs/) (in the post/page options), and you can see if you have any action from it in your dashboard the way you would normally see any comments left.

vangogh
10-27-2009, 11:59 PM
Interesting idea. I suppose it can mess up regular commenting, though not that much and it's probably not a big deal if you prefer the contact form anyway.

I've been using a plugin called Contact Form 7 lately and it's really nice. Very easy to use. It creates a shortcode you can drop into any post or page and you have a contact form.

On my site I just coded up the contact form and dropped it into the sidebar. It's easy enough for me to do anyway. The idea of letting them all come through WordPress is interesting though.

Harold Mansfield
10-28-2009, 12:44 AM
Interesting idea. I suppose it can mess up regular commenting, though not that much and it's probably not a big deal if you prefer the contact form anyway.

I've been using a plugin called Contact Form 7 lately and it's really nice. Very easy to use. It creates a shortcode you can drop into any post or page and you have a contact form.

On my site I just coded up the contact form and dropped it into the sidebar. It's easy enough for me to do anyway. The idea of letting them all come through WordPress is interesting though.

Yeah I use Contact form 7 as well. This is only useful (obviously) if you are using WP as a static site. If you are using it as a blog, then this wouldn't make any sense.
I just kind of figured since it's already there, and the basic fields are the same.
I do use a more in depth form on my contact page (Contact 7) that has more fields, but too much to drop on every page.

vangogh
10-28-2009, 01:19 AM
You could set up the comments the way you described for pages and still use the general comment system for posts. You'd need to include a different comments.php file for the pages and you'd have to deal with moderating all comments. Some blogs do that anyway though.

Actually you could avoid the comment moderation thing by leaving out the parts where the comments display in the code and only show the form and add a thank you message after the form has been submitted. You'd need to pay attention to your comments, but you should be able to search on pages as opposed to posts easily enough.

jamestl2
10-28-2009, 01:05 PM
I'm using something similar for one of my sites, although the one difference I made was I also installed a private comments plugin:
cleek WordPress Plugin: Semi-Private Comments (http://ok-cleek.com/blogs/?p=4061)

This way comments can be displayed to only the people who make them, and the admin.

vangogh
10-28-2009, 01:36 PM
Interesting plugin. You wouldn't want to use it for all commenting, but I can think of some good uses for hiding comments that way. In fact it might work really well for what eborg wants to do with the contact form.