Recently, I’ve been asked to create a Contact Author Form in one of the sites I am working on. Honestly, I don’t know if it is really called Contact Author Form, I just thought about it lol! I thought there are plugins that can do that, but after spending an hour looking for the said plugin my luck totally run out. So I decided to create a custom contact form for the author, I found Cats Who Code tuts which is really helpful for creating a basic contact form.
If you wanted to display a list of authors in wordpress, I also created a tutorial about that.
Today I will show you how I achieve the Contact Author Form; this is helpful especially if you’re running a multi-author blog, and some readers might want to get in touch with your authors, also if you are the admin you might want to receive a copy of the email that the author will get.
The form should:
- Form should be in the author page
- Form should send a copy to the admin every time the author receives an email
First download the js file and paste the whole folder in your theme folder. It should be like this js/verify.js and then open and paste this on your header.php next to the stylesheet.
<?php if( is_page('author') ){ ?> <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery.validate.min.js"></script> <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/verif.js"></script> <?php }?>
Here is the Contact form from Cats Who Code; I modify it a little bit so it’ll fit in our requirements.
<?php /* Template Name: Author */ ?> <?php if(isset($_POST['submitted'])) { if(trim($_POST['contactName']) === '') { $nameError = 'Please enter your name.'; $hasError = true; } else { $name = trim($_POST['contactName']); } if(trim($_POST['email']) === '') { $emailError = 'Please enter your email address.'; $hasError = true; } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,4}$", trim($_POST['email']))) { $emailError = 'You entered an invalid email address.'; $hasError = true; } else { $email = trim($_POST['email']); } if(trim($_POST['comments']) === '') { $commentError = 'Please enter a message.'; $hasError = true; } else { if(function_exists('stripslashes')) { $comments = stripslashes(trim($_POST['comments'])); } else { $comments = trim($_POST['comments']); } } if(!isset($hasError)) { $emailTo = get_option('tz_email'); if (!isset($emailTo) || ($emailTo == '') ){ $emailTo = get_option('admin_email'); } $subject = '[YOURDOMAINNAME.com - Contact Author Form] From '.$name; $body = "Name: $name nnEmail: $email nnComments: $comments"; $headers = 'From: '.$name.' <'.$emailTo.'>' . "rn" . 'Reply-To: ' . $email; mail($emailTo, $subject, $body, $headers); mail(get_the_author_meta( 'user_email' ), $subject, $body, $headers); $emailSent = true; } } ?> <?php if(isset($emailSent) && $emailSent == true) { ?> <div class="thanks"> <p>Thanks, your email was sent successfully.</p> </div> <?php } else { ?> <?php //the_content(); ?> <?php if(isset($hasError) || isset($captchaError)) { ?> <p class="error">Sorry, an error occured.<p> <?php } ?> <form action="<?php //the_permalink(); ?>" id="contactForm" method="post"> <ul class="contactform"> <li> <label for="contactName">Name:</label> <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" /> <?php if($nameError != '') { ?> <span class="error"><?=$nameError;?></span> <?php } ?> </li> <li> <label for="email">Email:</label> <input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="required requiredField email" /> <?php if($emailError != '') { ?> <span class="error"><?=$emailError;?></span> <?php } ?> </li> <li><label for="commentsText">Message:</label> <textarea name="comments" id="commentsText" rows="20" cols="20" class="required requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea> <?php if($commentError != '') { ?> <span class="error"><?=$commentError;?></span> <?php } ?> </li> <li> <input type="submit" value="Send email"></input> </li> </ul> <input type="hidden" name="submitted" id="submitted" value="true" /> </form> <?php } ?> <?php //endwhile; endif; ?>
The magic starts in this line of code
This will grab the admin email, admin can receive the email but what about the author?
$emailTo = get_option('admin_email');
With this one we will grab the author’s email. Because we have a different author on your blog, we need to set the get_author_meta(‘user_email’).
mail(get_the_author_meta( 'user_email' ), $subject, $body, $headers);
Just copy the code and name it Custom-contact.php, in your theme folder open author.php and Copy and Paste this code
<?php include (TEMPLATEPATH . '/custom-contactform.php'); ?>
to where ever you want the form to appear, and do the following.
Open your author.php and change
$curauth->user_email
to
< ?php the_author_meta('user_email'); ?>
You just have to check the alignment / style of the form depending on your stylesheet.
Conclusion
I think using a Contact Author Form is useful for Corporate websites and multi-author blog that has a lot of emails coming, if you are planning to use this in your blog make sure that the author know that as an admin you’ll also receive a copy of the email.
What you do use to reach out to your authors? How do you help your readers reach their favorite authors? You can list them below in the comments.