Guest User

Untitled

a guest
May 26th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. <?php
  2.  
  3. //response generation function
  4.  
  5. $response = "";
  6.  
  7. //function to generate response
  8. function my_contact_form_generate_response($type, $message){
  9.  
  10. global $response;
  11.  
  12. if($type == "success") $response = "<div class='success'>{$message}</div>";
  13. else $response = "<div class='error'>{$message}</div>";
  14.  
  15. }
  16.  
  17. //response messages
  18. $not_human = "Human verification incorrect.";
  19. $missing_content = "Please supply all information.";
  20. $email_invalid = "Email Address Invalid.";
  21. $message_unsent = "Message was not sent. Try Again.";
  22. $message_sent = "Thanks! Your message has been sent.";
  23.  
  24. //user posted variables
  25. $name = $_POST['message_name'];
  26. $email = $_POST['message_email'];
  27. $message = $_POST['message_text'];
  28. $human = $_POST['message_human'];
  29.  
  30. //php mailer variables
  31. $to = get_option('admin_email');
  32. $subject = "Someone sent a message from ".get_bloginfo('name');
  33. $headers = 'From: '. $email . "\r\n" .
  34. 'Reply-To: ' . $email . "\r\n";
  35.  
  36. if(!$human == 0){
  37. if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
  38. else {
  39.  
  40. //validate email
  41. if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  42. my_contact_form_generate_response("error", $email_invalid);
  43. else //email is valid
  44. {
  45. //validate presence of name and message
  46. if(empty($name) || empty($message)){
  47. my_contact_form_generate_response("error", $missing_content);
  48. }
  49. else //ready to go!
  50. {
  51. $sent = wp_mail($to, $subject, strip_tags($message), $headers);
  52. if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
  53. else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
  54. }
  55. }
  56. }
  57. }
  58. else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
  59.  
  60. ?>
  61.  
  62. <?php get_header(); ?>
  63.  
  64. <div id="primary" class="site-content">
  65. <div id="content" role="main">
  66.  
  67. <?php while ( have_posts() ) : the_post(); ?>
  68.  
  69. <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  70.  
  71. <header class="entry-header">
  72. <h1 class="entry-title"><?php the_title(); ?></h1>
  73. </header>
  74.  
  75. <div class="entry-content">
  76. <?php the_content(); ?>
  77.  
  78. <style type="text/css">
  79. .error{
  80. padding: 5px 9px;
  81. border: 1px solid red;
  82. color: red;
  83. border-radius: 3px;
  84. }
  85.  
  86. .success{
  87. padding: 5px 9px;
  88. border: 1px solid green;
  89. color: green;
  90. border-radius: 3px;
  91. }
  92.  
  93. form span{
  94. color: red;
  95. }
  96. </style>
  97.  
  98. <div id="respond">
  99. <?php echo $response; ?>
  100. <form action="<?php the_permalink(); ?>" method="post">
  101. <p><label for="name">Name: <span>*</span> <br><input type="text" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>"></label></p>
  102. <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>
  103. <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>
  104. <p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>
  105. <input type="hidden" name="submitted" value="1">
  106. <p><input type="submit"></p>
  107. </form>
  108. </div>
  109.  
  110.  
  111. </div><!-- .entry-content -->
  112.  
  113. </article><!-- #post -->
  114.  
  115. <?php endwhile; // end of the loop. ?>
  116.  
  117. </div><!-- #content -->
  118. </div><!-- #primary -->
  119.  
  120. <?php get_sidebar(); ?>
  121. <?php get_footer(); ?>
Add Comment
Please, Sign In to add comment