Advertisement
blackimpala

Custom Contact Form

Dec 19th, 2020
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.88 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Contact form functionality
  4.  * Used within the loop
  5.  * @link https://developer.wordpress.org/reference/functions/wp_nonce_field/
  6.  * @link https://codex.wordpress.org/Class_Reference/WP_Error
  7.  */
  8.  
  9. if ( ! defined( 'ABSPATH' ) ) exit;
  10.  
  11.  
  12. function my_contact_form(){
  13.  
  14.   global $reg_errors;
  15.   $reg_errors = new WP_Error;
  16.  
  17.   $email_invalid   = "Email Address Invalid.";
  18.   $name_required   = "Name Required.";
  19.   $email_required  = "Email Address Required.";
  20.   $phone_required  = "Phone Required.";
  21.   $text_required   = "Message Text Required.";
  22.   $missing_content = "Please supply all information.";
  23.   $message_unsent  = "Message was not sent. Try Again.";
  24.   $message_sent    = "Thanks! Your message has been sent.";
  25.   $recaptcha_required = "Are you robot?";
  26.  
  27.  if (isset($_POST['submitted']) && wp_verify_nonce( $_POST['gymclub_nonce_field'], 'custom_action_nonce')){
  28.  
  29.           //user posted variables
  30.         $name = isset ($_POST['message_name'])? esc_sql(sanitize_text_field($_POST['message_name'])):"";
  31.         $email = isset($_POST['message_email'])? esc_sql(sanitize_text_field(sanitize_email($_POST['message_email']))):"";
  32.         $phone = isset($_POST['message_phone'])? esc_sql(sanitize_text_field($_POST['message_phone'])):"";
  33.         $message = isset($_POST['message_text'])? esc_sql(sanitize_text_field($_POST['message_text'])):"";
  34.  
  35.         //We check that the field Email is valid
  36.            if(!is_email($_POST['message_email'])) {
  37.              $reg_errors->add("error", $email_invalid);
  38.            }
  39.  
  40.            if (empty($_POST['message_name'])) {
  41.              $reg_errors->add("error", $name_required);
  42.            }
  43.  
  44.            if (empty($_POST['message_phone'])) {
  45.              $reg_errors->add("error", $phone_required);
  46.            }
  47.  
  48.            if (empty($_POST['message_text'])) {
  49.              $reg_errors->add("error", $text_required);
  50.            }
  51.        
  52.          $to = get_option('gym_contact_admin_email');
  53.          $name     = sanitize_text_field($_POST['message_name']);
  54.          $email    = sanitize_email($_POST['message_email']);
  55.          $phone    = sanitize_text_field($_POST['message_phone']);
  56.          $message  = sanitize_text_field($_POST['message_text']);
  57.          $subject  = "Someone sent a message from " . get_bloginfo('name');
  58.  
  59.          if ( is_wp_error( $reg_errors ) ) {
  60.             if (count($reg_errors->get_error_messages()) > 0) {
  61.               foreach ( $reg_errors->get_error_messages() as $error ) {?>
  62.                 <div class="alert alert-danger" role="alert">
  63.                  <p><?php echo $error;?></p>
  64.                 </div>
  65.               <?php }
  66.               }
  67.             }
  68.  
  69.          $response = wp_remote_post( "https://www.google.com/recaptcha/api/siteverify", array(
  70.             'method' => 'POST',
  71.             'timeout' => 15,
  72.             'redirection' => 5,
  73.             'httpversion' => '1.0',
  74.             'blocking' => true,
  75.             'headers' => array(),
  76.             'body' => array(
  77.               'secret' => '6Ld61NkUAAAAAI0JuA0dp_RL5_T9EucRdgLX2nVj',
  78.               'response' => esc_attr($_POST['g-recaptcha-response'])),
  79.             'cookies' => array()
  80.             )
  81.           );
  82.  
  83.                //We check if we have any kind of error in the connection with google
  84.           if ( is_wp_error( $response ) ) {
  85.             $reg_errors->add( "invalid-captcha", $recaptcha_required );
  86.           } else {
  87.               //If we have connected correctly with google, we check if the answer is true or false
  88.             $g_response = json_decode($response["body"]);
  89.           if ($g_response->success == false) {
  90.               $reg_errors->add( "invalid-captcha", $recaptcha_required );
  91.             }
  92.           }
  93.  
  94.          $headers = 'From: '. $name . ' <' . $email . '>';
  95.          $sent = wp_mail( $to, $subject, $message, $headers );
  96.            
  97.            if ($sent){
  98.          
  99.             $r = array(
  100.                   'name'  => $name,
  101.                   'email' => $email,
  102.                   'phone' => $phone,
  103.                   'message' => $message,
  104.                   'time' => current_time( 'mysql' )
  105.             );
  106.              wp_send_json_success($r);
  107.            } else {
  108.             $r = array('message' => 'Mail Error');
  109.             wp_send_json_error($r);
  110.            }  
  111.        
  112.     }
  113.        $r = array('message' => 'Validate Error' );
  114.            wp_send_json_error($r);
  115.    
  116.    }
  117.  
  118. // WordPress Ajax
  119. add_action( 'wp_ajax_my_contact', 'my_contact_form' );
  120. add_action( 'wp_ajax_nopriv_my_contact', 'my_contact_form' );
  121.  
  122.  
  123.  
  124. // On send - works but prob not best practice https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)
  125.  
  126.  
  127.  
  128. function gym_contact_create_entry($name, $email, $phone, $message ) {
  129.   global $wpdb;
  130.   $table_name = $wpdb->prefix . 'contact';
  131.  
  132.   $wpdb->insert(
  133.         $table_name,
  134.         array(
  135.             'name' => $name,
  136.             'email' => $email,
  137.             'phone' => $phone,
  138.             'message' => $message,
  139.             'time' => current_time( 'mysql' )
  140.         )
  141.     );
  142. }
  143.  
  144.  ?>
  145.  
  146.  
  147.  <?php get_header(); ?>  
  148.        
  149. <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  150.  
  151. <div class="container">
  152.   <div class="contact">
  153.     <div class="col-md-6 col-md-offset-3">
  154.       <div class="form-area">
  155.             <div class="text-center contact-h"><?php the_title();?></div>
  156.             <form id="contact-form" action="<?php the_permalink(); ?>" method="post">
  157.                   <div class="group form-group">
  158.                       <input class="form-control" id="name" type="text" name="message_name" value="<?php if (isset($_POST['message_name'])) { echo esc_attr($_POST['message_name']);} ?>">
  159.                       <span class="highlight"></span>
  160.                       <span class="bar"></span>
  161.                       <label for="name">Name</label>
  162.                   </div><!-- end div group form-group -->
  163.                   <div class="group form-group">
  164.                       <input class="form-control"  id="email" type="email" name="message_email" value="<?php if (isset($_POST['message_email'])) { echo esc_attr($_POST['message_email']);} ?>">
  165.                       <span class="highlight"></span>
  166.                       <span class="bar"></span>
  167.                       <label for="message_email">Email</label>
  168.                   </div><!-- end div group form-group -->
  169.                   <div class="group form-group">
  170.                       <input class="form-control"  id="phone" type="tel" name="message_phone" value="<?php if (isset($_POST['message_phone'])) { echo esc_attr( $_POST['message_phone']); } ?>">
  171.                       <span class="highlight"></span>
  172.                       <span class="bar"></span>
  173.                       <label for="message_phone">Phone</label>
  174.                   </div><!-- end div group form-group -->
  175.                   <div class="group form-group">
  176.                       <div class="text-group">
  177.                           <textarea class="form-control" type="text" name="message_text" rows="4"><?php if (isset($_POST['message_text'])) { echo esc_textarea($_POST['message_text']); } ?></textarea>
  178.                           <label for="message_text" class="input-label">Message</label>
  179.                           <i class="bar"></i>
  180.                       </div><!-- end div text-group -->
  181.                   </div><!-- end div group form-group -->
  182.                   <div class="g-recaptcha" data-sitekey="6Ld61NkUAAAAAJJ60gH6Ku38xJwj8nzKWbYiaecs"></div>
  183.                   <input type="hidden" name="submitted" value="custom_action">
  184.                   <?php wp_nonce_field( 'custom_action_nonce', 'gymclub_nonce_field' ); ?>
  185.                   <button class="btn btn-primary" id="submit" type="submit" id="gymclub-submit" name="submit">Send</button>
  186.             </form><!-- end form -->
  187.       </div><!--end respond -->
  188.    </div><!-- end div -->
  189.  </div><!-- end div contact -->
  190. </div><!-- end container -->
  191.        
  192. <?php get_footer(); ?>
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement