blackimpala

Custom Form Conctact In Wordpress and Ajax

May 7th, 2020
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.46 KB | None | 0 0
  1. <?php
  2. /**
  3.  * The template for gym contact form.
  4.  * @link https://developer.wordpress.org/reference/functions/wp_nonce_field/
  5.  */
  6.  
  7. if ( ! defined( 'ABSPATH' ) ) exit;
  8.  
  9. $response = "";
  10.  
  11. //response messages
  12. $not_human       = "Human verification incorrect.";
  13. $missing_content = "Please supply all information.";
  14. $email_invalid   = "Email Address Invalid.";
  15. $message_unsent  = "Message was not sent. Try Again.";
  16. $message_sent    = "Thanks! Your message has been sent.";
  17.  
  18.  
  19. //user posted variables
  20. //$name = isset ($_POST['message_name'])? esc_sql(sanitize_text_field($_POST['message_name'])):"";
  21. //$email = isset($_POST['message_email'])? esc_sql(sanitize_text_field(sanitize_email($_POST['message_email']))):"";
  22. //$phone = isset($_POST['message_phone'])? esc_sql(sanitize_text_field($_POST['message_phone'])):"";
  23. //$message = isset($_POST['message_text'])? esc_sql(sanitize_text_field($_POST['message_text'])):"";
  24.  
  25. //$human = $_POST['message_human'];
  26.  
  27. //php mailer variables
  28. function my_contact_form(){
  29. if (isset($_POST['gymclub_nonce_field']) && wp_verify_nonce( $_POST['gymclub_nonce_field'], 'custom_action_nonce')){
  30.        
  31.          $to = get_option('gym_contact_admin_email');
  32.          $name     = sanitize_text_field($_POST['message_name']);
  33.          $email    = sanitize_email($_POST['message_email']);
  34.          $phone    = sanitize_text_field($_POST['message_phone']);
  35.          $message    = wp_kses_data($_POST['message_text']);
  36.          $subject = "Someone sent a message from " . get_bloginfo('name');
  37.  
  38.          $headers = 'From: '. $name . ' <' . $email . '>' .
  39.          $sent = wp_mail( $to, $subject, $message, $headers );
  40.            
  41.            if ($sent){
  42.          
  43.             $r = array(
  44.                   'name'  => $name,
  45.                   'email' => $email,
  46.                   'phone' => $phone,
  47.                   'message' => $message,
  48.                   'time' => current_time( 'mysql' )
  49.             );
  50.              wp_send_json_success($r);
  51.            } else {
  52.             $r = array('message' => 'Mail Error');
  53.             wp_send_json_error($r);
  54.            }  
  55.        
  56.     }
  57.        $r = array('message' => 'Validate Error' );
  58.            wp_send_json_error($r);
  59.    
  60.    }
  61.  
  62. // WordPress Ajax
  63. add_action( 'wp_ajax_my_contact', 'my_contact_form' );
  64. add_action( 'wp_ajax_nopriv_my_contact', 'my_contact_form' );
  65.  
  66.  
  67. //function to generate response
  68. function my_contact_form_generate_response($type, $message) {
  69.    global $response;
  70.  
  71.   if( $type == "success" ) {
  72.     $response = "<div class='alert alert-success'>{$message}</div>";
  73.   }
  74.   else {
  75.     $response = "<div class='alert alert-danger'>{$message}</div>";
  76.   }
  77. }
  78.  
  79. // On send - works but prob not best practice https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)
  80.  if (!empty($_POST) && !empty($_POST['g-recaptcha-response'])) {
  81.   //validate email
  82.  if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  83.    my_contact_form_generate_response("error", $email_invalid);
  84.    }
  85.    else {
  86.     //validate presence of name and message
  87.     if(empty($message || empty($email))) {
  88.       my_contact_form_generate_response("error", $missing_content);
  89.      }
  90.    else {
  91.      $sent = wp_mail($to, $subject, strip_tags($message), $headers);
  92.  
  93.     gym_contact_create_entry($name, $email, $phone, $message );
  94.  
  95.     if($sent) {
  96.        my_contact_form_generate_response("success", $message_sent); //message sent!
  97.       }
  98.      else {
  99.        my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
  100.      }
  101.    }
  102.   }
  103.  }
  104.  else {
  105.    if ( empty($_POST['g-recaptcha-response']) ) {
  106.       // Recaptcha fail
  107.        my_contact_form_generate_response("error", 'Are you a robot?');
  108.   }
  109. }
  110.  
  111. function gym_contact_create_entry($name, $email, $phone, $message ) {
  112.   global $wpdb;
  113.   $table_name = $wpdb->prefix . 'contact';
  114.  
  115.   $wpdb->insert(
  116.         $table_name,
  117.         array(
  118.             'name' => $name,
  119.             'email' => $email,
  120.             'phone' => $phone,
  121.             'message' => $message,
  122.             'time' => current_time( 'mysql' )
  123.         )
  124.     );
  125. }
  126.  
  127. get_header(); ?>
  128.  
  129. <?php
  130. // Get address defaults
  131. $c_addr_1 = get_post_meta(get_the_ID(), '_contact_addr_1', true);
  132. $c_addr_2 = get_post_meta(get_the_ID(), '_contact_addr_2', true);
  133. $c_addr = trim( $c_addr_1 . ' ' . $c_addr_2 );
  134.  
  135. // Get phone default
  136. $c_tel = get_post_meta(get_the_ID(), '_contact_phone', true);
  137.  
  138. // Get email default
  139. $c_email = get_post_meta(get_the_ID(), '_contact_email', true);
  140.  
  141. // Get text defaults
  142. $c_text_1 = get_post_meta(get_the_ID(), '_contact_text_1', true);
  143. $c_text_2 = get_post_meta(get_the_ID(), '_contact_text_2', true);
  144.  
  145.  
  146.  
  147.  
  148. ?>
  149.  
  150. <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  151.  
  152. <div class="container">
  153.   <div class="contact">
  154.     <div class="col-md-6 col-md-offset-3">
  155.       <div class="form-area">
  156.               <div class="text-center contact-h"><?php the_title();?></div>
  157.             <?php echo $response; ?>
  158.             <form id="contact-form" action="<?php the_permalink(); ?>" method="post">
  159.                   <div class="group form-group">
  160.                       <input class="form-control" id="name" type="text" name="message_name" value="<?php if (isset($_POST['message_name'])) { echo esc_attr($_POST['message_name']);} ?>">
  161.                       <span class="highlight"></span>
  162.                       <span class="bar"></span>
  163.                       <label for="name">Name</label>
  164.                   </div><!-- end div group form-group -->
  165.                   <div class="group form-group">
  166.                       <input class="form-control"  id="email" type="email" name="message_email" value="<?php if (isset($_POST['message_email'])) { echo esc_attr($_POST['message_email']);} ?>">
  167.                       <span class="highlight"></span>
  168.                       <span class="bar"></span>
  169.                       <label for="message_email">Email</label>
  170.                   </div><!-- end div group form-group -->
  171.                   <div class="group form-group">
  172.                       <input class="form-control"  id="phone" type="tel" name="message_phone" value="<?php if (isset($_POST['message_phone'])) { echo esc_attr( $_POST['message_phone']); } ?>">
  173.                       <span class="highlight"></span>
  174.                       <span class="bar"></span>
  175.                       <label for="message_phone">Phone</label>
  176.                   </div><!-- end div group form-group -->
  177.                   <div class="group form-group">
  178.                       <div class="text-group">
  179.                           <textarea class="form-control" type="text" name="message_text" rows="4"><?php if (isset($_POST['message_text'])) { echo esc_textarea($_POST['message_text']); } ?></textarea>
  180.                           <label for="message_text" class="input-label">Message</label>
  181.                           <i class="bar"></i>
  182.                       </div><!-- end div text-group -->
  183.                   </div><!-- end div group form-group -->
  184.                   <div class="g-recaptcha" data-sitekey="6Ld61NkUAAAAAJJ60gH6Ku38xJwj8nzKWbYiaecs"></div>
  185.                   <input type="hidden" name="submitted" value="1">
  186.                   <?php wp_nonce_field( 'custom_action_nonce', 'gymclub_nonce_field' ); ?>
  187.                   <button class="btn btn-primary" id="submit" type="submit" id="gymclub-submit" name="submit">Send</button>
  188.             </form><!-- end form -->
  189.       </div><!--end respond -->
  190.    </div><!-- end div -->
  191.  </div><!-- end div contact -->
  192. </div><!-- end container -->
  193. <?php get_footer();
Add Comment
Please, Sign In to add comment