Custom wordpress contact form

I am trying to make a custom wordpress contact form. I’ve got so far:

My function for the contact form itself:

Read More
<?php
if( isset( $_POST['submitted'] ) ) {
  //response generation function

  $response = "";

  //function to generate response
  function my_contact_form_generate_response($type, $message){

    global $response;

    if($type == "success") $response = "<div class='success'>{$message}</div>";
    else $response = "<div class='error'>{$message}</div>";

  }

  //response messages
  $not_human       = "Human verification incorrect.";
  $missing_content = "Please supply all information.";
  $email_invalid   = "Email Address Invalid.";
  $message_unsent  = "Message was not sent. Try Again.";
  $message_sent    = "Thanks! Your message has been sent.";

  //user posted variables
  $name = $_POST['message_name'];
  $email = $_POST['message_email'];
  $message = $_POST['message_text'];
  $human = $_POST['message_human'];

  //php mailer variables
  $to = get_option('admin_email');
  $subject = "Someone sent a message from ".get_bloginfo('name');
  $headers = 'From: '. $email . "rn" .
    'Reply-To: ' . $email . "rn";

  if(!$human == 0){
    if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
    else {

      //validate email
      if(!filter_var($email, FILTER_VALIDATE_EMAIL))
        my_contact_form_generate_response("error", $email_invalid);
      else //email is valid
      {
        //validate presence of name and message
        if(empty($name) || empty($message)){
          my_contact_form_generate_response("error", $missing_content);
        }
        else //ready to go!
        {
          $sent = wp_mail($to, $subject, strip_tags($message), $headers);
          if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
          else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
        }
      }
    }
  }
  else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
}
?>

Secondly:

<div id="respond">
          <?php echo $response; ?>
          <form action="<?php the_permalink(); ?>" method="post">
            <p><label for="name">Name: <span>*</span> <br><input type="text" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>"></label></p>
            <p><label for="message_email">Email: <span>*</span> <br><input type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>"></label></p>
            <p><label for="message_text">Message: <span>*</span> <br><textarea type="text" name="message_text"><?php echo esc_textarea($_POST['message_text']); ?></textarea></label></p>
            <p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>
            <input type="hidden" name="submitted" value="1">
            <p><input type="submit"></p>
          </form>
        </div>

This works fine, but it’s quite basic. I didn’t want to use a plugin as they all seem really bloated and quite messy. Maybe I’ve just looked at bad choices. Anyway, i wanted to know if it’s possible to edit how the message is formatted when it’s email to the admin. At the moment all i get is the message with no other information like the persons name, email..

Am i making a bad choice using this?

It would be great to format any emails sent via the form to be formatted something like:

Sender: Joe Blogs

Email: joe@blogs.com

Message: I love you.

Related posts

Leave a Reply

2 comments

  1. If it’s fast, lightweight, and does what you want (after you work the kinks out), then that’s great. I can see it being faster and less burdensome than a plugin.

    My concern would be that some of the more popular contact form plugins like Contact Form 7 have been field-tested for usability, exploits, etc., and have had the code added to handle these cases.

    If you’re rolling your own, you might (likely will?) get hit by something, and if you end up sending a lot of spam due to an exploit in your form, it’s on you.

    Not to discourage your efforts — and not that I can see anything wrong with what you’ve done! — but just a gut feeling.

  2. Why not simply extend your current $message variable?

    //user posted variables
    $name = $_POST['message_name'];
    $email = $_POST['message_email'];
    $message = $_POST['message_text'];
    $human = $_POST['message_human'];
    
    // this is the new part
    $temp_message = $message;
    $message = 'Sender: '.$name.'<br/>';
    $message .= 'Email: '.$email.'<br/>';
    $message .= 'Human: '.$human.'<br/>';
    $message .= 'Message: '.$temp_message;