Advertisement
blackimpala

My Custom Form Wordpress March

Mar 17th, 2021
985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.75 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.  * @link https://developer.wordpress.org/reference/functions/wp_is_mobile/
  8.  * @link https://developer.wordpress.org/reference/functions/wp_kses_data/
  9.  * @link https://developer.wordpress.org/reference/functions/wp_remote_retrieve_response_code/
  10.  * @link https://developer.wordpress.org/reference/functions/wp_get_referer/
  11.  */
  12.  
  13.  
  14. defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
  15.  
  16.  
  17. // Validate Fields Contact Form
  18. function gym_contact_validate_form(){
  19.   if (isset($_POST['submit']) && isset($_POST['gymclub_nonce_field'])) {
  20.    
  21.      if (wp_verify_nonce( $_POST['gymclub_nonce_field'], 'custom_action_nonce')) {
  22.          //user posted variables
  23.         $to = get_option('gym_contact_admin_email');
  24.         $name = isset ($_POST['message_name'])? esc_sql(sanitize_text_field($_POST['message_name'])):"";
  25.         $email = isset($_POST['message_email'])? esc_sql(sanitize_text_field(sanitize_email($_POST['message_email']))):"";
  26.         $phone = isset($_POST['message_phone'])? esc_sql(sanitize_text_field($_POST['message_phone'])):"";
  27.         $message = isset($_POST['message_text'])? esc_sql(sanitize_text_field($_POST['message_text'])):"";
  28.  
  29.  
  30.         // If any field is left empty, add the error message to the error array
  31.         if ( empty($name) || empty($email) || empty($phone) || empty($message) ) {
  32.             array_push( $this->form_errors, 'No field should be left empty' );
  33.         }
  34.  
  35.         // if the name field isn't alphabetic, add the error message
  36.         if ( strlen($name) < 4 ) {
  37.             array_push( $this->form_errors, 'Name should be at least 4 characters' );
  38.         }
  39.  
  40.         // Check if the email is valid
  41.         if ( !is_email($email) ) {
  42.             array_push( $this->form_errors, 'Email is not valid' );
  43.         }
  44.  
  45.  
  46.         $headers[] = 'From: '. $name . ' <' . $email . '>';
  47.         $sent = wp_mail( $to, $subject, $message, $headers );
  48.  
  49.         if ($sent){
  50.          
  51.             $r = array(
  52.                   'name'  => $name,
  53.                   'email' => $email,
  54.                   'to' => $to,
  55.                   'subject' => $subject,
  56.                   'message' => $message);
  57.              wp_send_json_success($r);
  58.            } else {
  59.             $r = array('message' => 'Mail Error');
  60.             wp_send_json_error($r);
  61.            }
  62.  
  63.            $r = array('message' => 'Validate Error' );
  64.            wp_send_json_error($r);
  65.  
  66.      }// end verify nonce                  
  67.        
  68.     } //end isset
  69.        
  70. }// end function
  71.  
  72. // WordPress Ajax
  73. add_action( 'wp_ajax_gym_contact_create_entry', 'gym_contact_create_entry' );
  74. add_action( 'wp_ajax_nopriv_my_contact', 'gym_contact_create_entry' );
  75.  
  76.  
  77. // Ajax insert data contact entry
  78. function gym_contact_create_entry($name, $email, $phone, $message ) {
  79.   global $wpdb;
  80.   $table_name = $wpdb->prefix . 'contact';
  81.  
  82.   $wpdb->insert(
  83.         $table_name,
  84.         array(
  85.             'name' => $name,
  86.             'email' => $email,
  87.             'phone' => $phone,
  88.             'message' => $message,
  89.             'time' => current_time( 'mysql' )
  90.         )
  91.     );
  92. }
  93.  
  94.  
  95.  
  96.  ?>
  97.  
  98.  
  99.  <?php get_header(); ?>  
  100.      
  101.      
  102. <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  103.  
  104. <article class="container">
  105.     <?php if (have_posts()):
  106.     while (have_posts()): the_post();  ?>
  107.   <div class="row contact">
  108.     <div class="col-md-6">
  109.       <div class="form-area">
  110.             <div class="text-center contact-h"><?php the_title();?></div>
  111.            
  112.             <form id="contact-form" action="<?php the_permalink(); ?>" method="post">
  113.                   <div class="group form-group">
  114.                       <input class="form-control" id="name" type="text" name="message_name" value="<?php if (isset($_POST['message_name'])) { echo esc_attr($_POST['message_name']);} ?>">
  115.                       <span class="highlight"></span>
  116.                       <span class="bar"></span>
  117.                       <label for="name"><?php echo __('Name', 'gymclub'); ?></label>
  118.                   </div><!-- end div group form-group -->
  119.                   <div class="group form-group">
  120.                       <input class="form-control"  id="email" type="email" name="message_email" value="<?php if (isset($_POST['message_email'])) { echo esc_attr($_POST['message_email']);} ?>">
  121.                       <span class="highlight"></span>
  122.                       <span class="bar"></span>
  123.                       <label for="message_email"><?php echo __('Email', 'gymclub'); ?></label>
  124.                   </div><!-- end div group form-group -->
  125.                   <div class="group form-group">
  126.                       <input class="form-control"  id="phone" type="tel" name="message_phone" value="<?php if (isset($_POST['message_phone'])) { echo esc_attr( $_POST['message_phone']); } ?>">
  127.                       <span class="highlight"></span>
  128.                       <span class="bar"></span>
  129.                       <label for="message_phone"><?php echo __('Phone', 'gymclub'); ?></label>
  130.                   </div><!-- end div group form-group -->
  131.                   <div class="group form-group">
  132.                       <div class="text-group">
  133.                           <textarea class="form-control" id="message" type="text" name="message_text" rows="4"><?php if (isset($_POST['message_text'])) { echo esc_textarea($_POST['message_text']); } ?></textarea>
  134.                           <label for="message_text" class="input-label"><?php echo __('Message', 'gymclub'); ?></label>
  135.                           <i class="bar"></i>
  136.                       </div><!-- end div text-group -->
  137.                   </div><!-- end div group form-group -->
  138.                   <p class="message_success" id="message_success">Hola desde el formulario</p>
  139.                   <p class="message_error" id="message_error">Hola desde el formulario</p>
  140.                   <div class="g-recaptcha" data-sitekey="6Ld61NkUAAAAAJJ60gH6Ku38xJwj8nzKWbYiaecs"></div>
  141.                   <!--<input type="hidden" name="submitted" value="custom_action">-->
  142.                   <?php wp_nonce_field( 'custom_action_nonce', 'gymclub_nonce_field' ); ?>
  143.                   <button class="btn btn-primary" id="submit" type="submit" id="gymclub-submit" name="submit"><?php echo __('Send', 'gymclub'); ?></button>
  144.             </form><!-- end form -->
  145.       </div><!--end respond -->
  146.    </div><!-- end div col-md-6 -->  
  147.  
  148.     <div class="col-md-6" itemscope itemtype="http://schema.org/LocalBusiness">
  149.         <h3><?php echo __('Dates', 'gymclub'); ?></h3>
  150.         <span class="dates_contact" itemprop="name"><i class="fas fa-building"></i><?php echo esc_attr( get_option('gym_contact_name_company') ); ?></span>
  151.         <div  itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
  152.           <span class="dates_contact" itemprop="streetAddress"><i class="fas fa-map-marked-alt"></i><?php echo esc_attr( get_option('gym_contact_address_company') ); ?></span>
  153.            <span class="dates_contact" itemprop="telephone"><i class="fas fa-phone"></i><?php echo esc_attr( get_option('gym_contact_phone_company') ); ?></span>
  154.            <span class="dates_contact" itemprop="email"><i class="far fa-envelope"></i><?php echo esc_attr( get_option('gym_contact_admin_email') ); ?></span>
  155.            <span class="dates_contact" itemprop="postalCode"><i class="fas fa-mail-bulk"></i><?php echo esc_attr( get_option('gym_contact_code_postal_company') ); ?></span>
  156.         </div><!-- end div itemprop -->
  157.      </div><!--end div col-md-6 div itemscope -->
  158.  
  159.  </div><!-- end div contact -->
  160.    
  161.   <?php endwhile;
  162.   endif;?>    
  163.  
  164. </article><!-- end section -->
  165.  
  166.        
  167. <?php get_footer(); ?>
  168.  
  169.  
  170.  
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement