template-contact.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * Template Name: Contact
  4. */
  5. ?>
  6. <?php
  7. $errors = array();
  8. $isError = false;
  9. $errorName = __( 'Please enter your name.', 'tuts' );
  10. $errorEmail = __( 'Please enter a valid email address.', 'tuts' );
  11. $errorMessage = __( 'Please enter a message.', 'tuts' );
  12. /* Get the posted variables and validate them. */
  13. if ( isset( $_POST['is-submitted'] ) ) {
  14. $name = $_POST['cName'];
  15. $email = $_POST['cEmail'];
  16. $message = $_POST['cMessage'];
  17. /* Check the name. */
  18. if ( ! tuts_validate_length( $name, 2 ) ) {
  19. $isError = true;
  20. $errors['errorName'] = $errorName;
  21. }
  22. /* Check the email. */
  23. if ( ! is_email( $email ) ) {
  24. $isError = true;
  25. $errors['errorEmail'] = $errorEmail;
  26. }
  27. /* Check the message. */
  28. if ( ! tuts_validate_length( $message, 2 ) ) {
  29. $isError = true;
  30. $errors['errorMessage'] = $errorMessage;
  31. }
  32. /* If there's no error, send email. */
  33. if ( ! $isError ) {
  34. /* Get admin email. */
  35. $emailReceiver = get_option( 'admin_email' );
  36. $emailSubject = sprintf( __( 'You have been contacted by %s', 'tuts' ), $name );
  37. $emailBody = sprintf( __( 'You have been contacted by %1$s. Their message is:', 'tuts' ), $name ) . PHP_EOL . PHP_EOL;
  38. $emailBody .= $message . PHP_EOL . PHP_EOL;
  39. $emailBody .= sprintf( __( 'You can contact %1$s via email at %2$s', 'tuts' ), $name, $email );
  40. $emailBody .= PHP_EOL . PHP_EOL;
  41. $emailHeaders[] = "Reply-To: $email" . PHP_EOL;
  42. $emailIsSent = wp_mail( $emailReceiver, $emailSubject, $emailBody, $emailHeaders );
  43. }
  44. }
  45. ?>
  46. <?php get_header(); ?>
  47. <!-- Jumbotron -->
  48. <div class="container-fluid text-center">
  49. <div class="jumbotron">
  50. <div class="row">
  51. <div class="col-md-10 col-md-offset-1">
  52. <h1><?php _e( 'Thanks for getting in touch.', 'tuts' ); ?></h1>
  53. <p class="lead">
  54. <?php _e( 'I can\'t wait to hear from you.', 'tuts' ); ?>
  55. </p>
  56. </div> <!-- end col -->
  57. </div> <!-- end row -->
  58. </div> <!-- end jumbotron -->
  59. </div> <!-- end container-fluid -->
  60. <!-- Contact Form -->
  61. <div class="page-contact container-fluid">
  62. <div class="row">
  63. <div class="col-md-6">
  64. <?php if ( isset( $emailIsSent ) && $emailIsSent ) : ?>
  65. <div class="alert alert-success">
  66. <?php _e( 'Your message has been sucessfully sent, thank you!', 'tuts' ); ?>
  67. </div> <!-- end alert -->
  68. <?php else : ?>
  69. <p>
  70. <?php _e( 'I\'m glad you\'ve decided to contact me. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nulla pariatur totam quam, repellendus, molestias id veniam. Aspernatur officiis, ducimus quasi.', 'tuts' ); ?>
  71. </p>
  72. <?php if ( isset( $isError ) && $isError ) : ?>
  73. <div class="alert alert-danger">
  74. <?php _e( 'Sorry, it seems there was an error.', 'tuts' ); ?>
  75. </div> <!-- end alert -->
  76. <?php endif; ?>
  77. <?php endif; ?>
  78. </div> <!-- end col -->
  79. </div> <!-- end row -->
  80. <?php if ( ! isset( $emailIsSent ) || isset( $isError ) && $isError ) : ?>
  81. <div class="row">
  82. <div class="contact-form col-md-6">
  83. <form role="form" action="<?php the_permalink(); ?>" method="post">
  84. <div class="form-group <?php if ( isset( $errors['errorName'] ) ) echo "has-error"; ?>">
  85. <label for="contact-name"><?php _e( 'Name', 'tuts' ); ?></label>
  86. <input type="text" class="form-control" id="contact-name" name="cName" placeholder="<?php _e( 'Enter your name', 'tuts' ); ?>" value="<?php if ( isset( $_POST['cName'] ) ) { echo $_POST['cName']; } ?>">
  87. <?php if ( isset( $errors['errorName'] ) ) : ?>
  88. <p class="help-block"><?php echo $errors['errorName']; ?></p>
  89. <?php endif; ?>
  90. </div> <!-- end form-group -->
  91. <div class="form-group <?php if ( isset( $errors['errorEmail'] ) ) echo "has-error"; ?>">
  92. <label for="contact-email"><?php _e( 'E-mail Address', 'tuts' ); ?></label>
  93. <input type="email" class="form-control" id="contact-email" name="cEmail" placeholder="<?php _e( 'Enter your e-mail address', 'tuts' ); ?>" value="<?php if ( isset( $_POST['cEmail'] ) ) { echo $_POST['cEmail']; } ?>">
  94. <?php if ( isset( $errors['errorEmail'] ) ) : ?>
  95. <p class="help-block"><?php echo $errors['errorEmail']; ?></p>
  96. <?php endif; ?>
  97. </div> <!-- end form-group -->
  98. <div class="form-group <?php if ( isset( $errors['errorMessage'] ) ) echo "has-error"; ?>">
  99. <label for="contact-message"><?php _e( 'Message', 'tuts' ); ?></label>
  100. <textarea class="form-control" rows="3" id="contact-message" name="cMessage" placeholder="<?php _e( 'Enter your message', 'tuts' ); ?>"><?php if ( isset( $_POST['cMessage'] ) ) { echo $_POST['cMessage']; } ?></textarea>
  101. <?php if ( isset( $errors['errorMessage'] ) ) : ?>
  102. <p class="help-block"><?php echo $errors['errorMessage']; ?></p>
  103. <?php endif; ?>
  104. </div> <!-- end form-group -->
  105. <input type="hidden" name="is-submitted" id="is-submitted" value="true">
  106. <button type="submit" class="btn btn-default"><?php _e( 'Submit', 'tuts' ); ?></button>
  107. </form>
  108. </div> <!-- end col -->
  109. </div> <!-- end row -->
  110. <?php endif; ?>
  111. </div> <!-- end container-fluid -->
  112. <?php
  113. /* Load footer.php */
  114. get_footer();
  115. ?>