Advertisement
blackimpala

Custom Form Last Backup

Mar 15th, 2021
1,553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.06 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.  
  18.  
  19.  
  20.  
  21. // On send - works but prob not best practice validate recaptcha https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)
  22.  
  23. /*if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  24.    $captcha = $_POST['g-recaptcha-response'];
  25.  
  26.    //Fields to sent
  27.    $fields = array(
  28.         'secret' => '6Ld61NkUAAAAAI0JuA0dp_RL5_T9EucRdgLX2nVj',
  29.         'response' => '$captcha',
  30.         'remoteip' => $_SERVER['REMOTE_ADDR']
  31.      );
  32.  
  33.    //Start Sesion in CURL or file_get_contents
  34.    $ch = curl_init('https://www.google.com/recaptcha/api/siteverify');
  35.  
  36.    // Configurate CURL options
  37.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  38.    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  39.  
  40.    // Generate array code for URL
  41.    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
  42.  
  43.    // Get response
  44.    $answer = json_decode(curl_exec($ch));
  45.    if ($answer->success) {
  46.      # code here
  47.  
  48.    }
  49.  
  50.  
  51. }
  52. */
  53.  
  54. // Validate Fields Contact Form
  55. function gym_contact_validate_form(){
  56.    if (isset($_POST['gymclub_nonce_field']) && wp_verify_nonce( $_POST['gymclub_nonce_field'], 'custom_action_nonce')){
  57.  
  58.  
  59.           /* $email_invalid   = "Email Address Invalid.";
  60.           $name_required   = "Name Required.";
  61.           $email_required  = "Email Address Required.";
  62.           $phone_required  = "Phone Required.";
  63.           $text_required   = "Message Text Required.";
  64.           $missing_content = "Please supply all information.";
  65.           $message_unsent  = "Message was not sent. Try Again.";
  66.           $message_sent    = "Thanks! Your message has been sent.";
  67.           $recaptcha_required = "Are you robot?";
  68.         */
  69.  
  70.           //user posted variables
  71.        
  72.         $name = isset ($_POST['message_name'])? esc_sql(sanitize_text_field($_POST['message_name'])):"";
  73.         $email = isset($_POST['message_email'])? esc_sql(sanitize_text_field(sanitize_email($_POST['message_email']))):"";
  74.         $phone = isset($_POST['message_phone'])? esc_sql(sanitize_text_field($_POST['message_phone'])):"";
  75.         $message = isset($_POST['message_text'])? esc_sql(sanitize_text_field($_POST['message_text'])):"";
  76.  
  77.         //The Name field is required, we check that it is not empty and if not we create an error record
  78.             if ( empty( $name ) ) {
  79.                $reg_errors->add('empty-name', esc_html__('The field name is required', 'gymclub'));
  80.              }
  81.  
  82.         //The Email field is required, we check that it is not empty and if not we create an error record
  83.             if ( empty( $email ) ) {
  84.                $reg_errors->add('empty-email', esc_html__('The field email is required', 'gymclub'));
  85.              }
  86.  
  87.         //We check that the data is in e-mail format with the WordPress
  88.             if ( !is_email( $email ) ) {
  89.                $reg_errors->add('invalid-email', esc_html__('The e-mail is not in a valid format', 'gymclub'));
  90.               }
  91.        
  92.         //The Message field is required, we check that it is not empty and if not we create an error record
  93.             if ( empty( $message ) ) {
  94.                 $reg_errors->add('empty-message', esc_html__('The field message is required', 'gymclub'));
  95.               }
  96.  
  97.  
  98.         //If there are no errors we send the form
  99.                 if (count($reg_errors->get_error_messages()) == 0) {
  100.                   //Recipient
  101.                   $to = get_option('gym_contact_admin_email');
  102.  
  103.                   //Subject of the email
  104.                   $subject = esc_html__('Someone sent a message from', 'gymclub') . get_bloginfo( 'name' );
  105.  
  106.                   //The email address is that of our blog so by adding this header we can respond to the original sender
  107.                   $headers = "Reply-to: " . $name . " <" . $email . ">\r\n";
  108.  
  109.                   //We mount the body of our e-mail
  110.                   $message = "Name: " . $name . "<br>";
  111.                   $message .= "Email: " . $email . "<br>";
  112.                   $message .= "Phone: " . $phone . "<br>";
  113.                   $message .= "Message: " . nl2br($message) . "<br>";
  114.                                    
  115.                   //Filter to indicate which email should be sent in HTM modeL
  116.                   add_filter('wp_mail_content_type', create_function('', 'return "text/html";'));
  117.                                    
  118.                   //Finally we send the email
  119.                   $sent = wp_mail( $recipient, $subject, $message, $headers, $attachments);
  120.  
  121.                 }//end if(count)       
  122.                  
  123.    }//end if(!iiset)
  124.  
  125. }// end function
  126. add_action( 'init', 'gym_contact_validate_form' );
  127.  
  128. // Show errors in data fields
  129. function gym_error_messages(){
  130.   global $reg_errors;
  131.   if (!isset($reg_errors)):
  132.      $reg_errors = new WP_Error;
  133.   endif;
  134.  
  135.   return $reg_errors;    
  136. }
  137.  
  138. // Register errors fields
  139. function gym_register_errors(){
  140.     global $reg_errors;
  141.        $reg_errors = new WP_Error;
  142.   if (is_wp_error( $reg_errors )) {
  143.      if (count($reg_errors->get_error_message()) > 0 ) {
  144.         foreach ($reg_errors->get_error_messages() as $error ) { ?>
  145.             <div class="row margin-button-small">
  146.                 <div class="col-md-12 alert alert-warning">
  147.                     <button type="button" class="close" data-dismiss="alert" aria-label="close">
  148.                         <span aria-hidden="true">&times;</span>
  149.                     </button>
  150.                     <p class="message"><?php echo __($error, 'gymclub'); ?></p>
  151.                 </div>
  152.             </div>
  153.         <?php}
  154.      }
  155.   }
  156. }
  157.  
  158.  
  159. // Ajax insert data contact entry
  160. function gym_contact_create_entry($name, $email, $phone, $message ) {
  161.   global $wpdb;
  162.   $table_name = $wpdb->prefix . 'contact';
  163.  
  164.   $wpdb->insert(
  165.         $table_name,
  166.         array(
  167.             'name' => $name,
  168.             'email' => $email,
  169.             'phone' => $phone,
  170.             'message' => $message,
  171.             'time' => current_time( 'mysql' )
  172.         )
  173.     );
  174. }
  175.  
  176. // WordPress Ajax
  177. add_action( 'wp_ajax_gym_contact_create_entry', 'gym_contact_create_entry' );
  178. add_action( 'wp_ajax_nopriv_my_contact', 'gym_contact_create_entry' );
  179.  
  180.  
  181.  ?>
  182.  
  183.  
  184.  <?php get_header(); ?>  
  185.      
  186.      
  187. <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  188.  
  189. <?php
  190.   if (isset( $_POST['gymclub_nonce_field'] )):
  191.     gym_register_errors();
  192.  endif;
  193.  ?>
  194.  
  195. <article class="container">
  196.     <?php if (have_posts()):
  197.     while (have_posts()): the_post();  ?>
  198.   <div class="row contact">
  199.     <div class="col-md-6">
  200.       <div class="form-area">
  201.             <div class="text-center contact-h"><?php the_title();?></div>
  202.            
  203.             <form id="contact-form" action="<?php the_permalink(); ?>" method="post">
  204.                   <div class="group form-group">
  205.                       <input class="form-control" id="name" type="text" name="message_name" value="<?php if (isset($_POST['message_name'])) { echo esc_attr($_POST['message_name']);} ?>">
  206.                       <span class="highlight"></span>
  207.                       <span class="bar"></span>
  208.                       <label for="name"><?php echo __('Name', 'gymclub'); ?></label>
  209.                   </div><!-- end div group form-group -->
  210.                   <div class="group form-group">
  211.                       <input class="form-control"  id="email" type="email" name="message_email" value="<?php if (isset($_POST['message_email'])) { echo esc_attr($_POST['message_email']);} ?>">
  212.                       <span class="highlight"></span>
  213.                       <span class="bar"></span>
  214.                       <label for="message_email"><?php echo __('Email', 'gymclub'); ?></label>
  215.                   </div><!-- end div group form-group -->
  216.                   <div class="group form-group">
  217.                       <input class="form-control"  id="phone" type="tel" name="message_phone" value="<?php if (isset($_POST['message_phone'])) { echo esc_attr( $_POST['message_phone']); } ?>">
  218.                       <span class="highlight"></span>
  219.                       <span class="bar"></span>
  220.                       <label for="message_phone"><?php echo __('Phone', 'gymclub'); ?></label>
  221.                   </div><!-- end div group form-group -->
  222.                   <div class="group form-group">
  223.                       <div class="text-group">
  224.                           <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>
  225.                           <label for="message_text" class="input-label"><?php echo __('Message', 'gymclub'); ?></label>
  226.                           <i class="bar"></i>
  227.                       </div><!-- end div text-group -->
  228.                   </div><!-- end div group form-group -->
  229.                   <p class="message_success" id="message_success">Hola desde el formulario</p>
  230.                   <p class="message_error" id="message_error">Hola desde el formulario</p>
  231.                   <div class="g-recaptcha" data-sitekey="6Ld61NkUAAAAAJJ60gH6Ku38xJwj8nzKWbYiaecs"></div>
  232.                   <!--<input type="hidden" name="submitted" value="custom_action">-->
  233.                   <?php wp_nonce_field( 'custom_action_nonce', 'gymclub_nonce_field' ); ?>
  234.                   <button class="btn btn-primary" id="submit" type="submit" id="gymclub-submit" name="submit"><?php echo __('Send', 'gymclub'); ?></button>
  235.             </form><!-- end form -->
  236.       </div><!--end respond -->
  237.    </div><!-- end div -->  
  238.  </div><!-- end div contact -->
  239.  
  240.        <div class="col-md-6" itemscope itemtype="http://schema.org/LocalBusiness">
  241.           <h3><?php echo __('Dates', 'gymclub'); ?></h3>
  242.           <span class="dates_contact" itemprop="name"><i class="fas fa-building"></i><?php echo esc_attr( get_option('gym_contact_name_company') ); ?></span>
  243.           <div  itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
  244.             <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>
  245.              <span class="dates_contact" itemprop="telephone"><i class="fas fa-phone"></i><?php echo esc_attr( get_option('gym_contact_phone_company') ); ?></span>
  246.              <span class="dates_contact" itemprop="email"><i class="far fa-envelope"></i><?php echo esc_attr( get_option('gym_contact_admin_email') ); ?></span>
  247.              <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>
  248.           </div><!-- end div itemprop -->
  249.        </div><!--end div col-md-6 div itemscope -->
  250.    
  251.   <?php endwhile;
  252.   endif;?>    
  253.  
  254. </article><!-- end section -->
  255.  
  256.        
  257. <?php get_footer(); ?>
  258.  
  259.  
  260.  
  261.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement