Advertisement
blackimpala

Formulario Personalizado

Nov 5th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.91 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. global $reg_errors;
  12. $reg_errors = new WP_error;
  13.  
  14. $response = "";
  15.  
  16. //response messages
  17. //$not_human       = "Human verification incorrect.";
  18. $email_invalid   = "Email Address Invalid.";
  19. $name_required   = "Name Required.";
  20. $email_required  = "Email Address Required.";
  21. $phone_required  = "Phone Required.";
  22. $text_required   = "Message Text Required.";
  23. $missing_content = "Please supply all information.";
  24. $message_unsent  = "Message was not sent. Try Again.";
  25. $message_sent    = "Thanks! Your message has been sent.";
  26. $recaptcha_required = "Are you robot?";
  27.  
  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. //$human = $_POST['message_human'];
  36.  
  37. //php mailer variables
  38. function my_contact_form(){
  39. if (isset($_POST['gymclub_nonce_field']) && wp_verify_nonce( $_POST['gymclub_nonce_field'], 'custom_action_nonce')){
  40.        
  41.          $to = get_option('gym_contact_admin_email');
  42.          $name     = sanitize_text_field($_POST['message_name']);
  43.          $email    = sanitize_email($_POST['message_email']);
  44.          $phone    = sanitize_text_field($_POST['message_phone']);
  45.          $message    = wp_kses_data($_POST['message_text']);
  46.          $subject = "Someone sent a message from " . get_bloginfo('name');
  47.  
  48.          $headers = 'From: '. $name . ' <' . $email . '>';
  49.          $sent = wp_mail( $to, $subject, $message, $headers );
  50.            
  51.            if ($sent){
  52.          
  53.             $r = array(
  54.                   'name'  => $name,
  55.                   'email' => $email,
  56.                   'phone' => $phone,
  57.                   'message' => $message,
  58.                   'time' => current_time( 'mysql' )
  59.             );
  60.              wp_send_json_success($r);
  61.            } else {
  62.             $r = array('message' => 'Mail Error');
  63.             wp_send_json_error($r);
  64.            }  
  65.        
  66.     }
  67.        $r = array('message' => 'Validate Error' );
  68.            wp_send_json_error($r);
  69.    
  70.    }
  71.  
  72. // WordPress Ajax
  73. add_action( 'wp_ajax_my_contact', 'my_contact_form' );
  74. add_action( 'wp_ajax_nopriv_my_contact', 'my_contact_form' );
  75.  
  76.  
  77.  
  78. // On send - works but prob not best practice https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)
  79.  
  80. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  81.    $captcha = $_POST['g-recaptcha-response'];
  82.  
  83.    //Fields to sent
  84.    $fields = array(
  85.         'secret' => '6Ld61NkUAAAAAI0JuA0dp_RL5_T9EucRdgLX2nVj',
  86.         'response' => '$captcha',
  87.         'remoteip' => $_SERVER['REMOTE_ADDR']
  88.      );
  89.  
  90.    //Start Sesion in CURL or file_get_contents
  91.    $ch = curl_init('https://www.google.com/recaptcha/api/siteverify');
  92.  
  93.    // Configurate CURL options
  94.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  95.    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  96.  
  97.    // Generate array code for URL
  98.    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
  99.  
  100.    // Get response
  101.    $answer = json_decode(curl_exec($ch));
  102.    if ($answer->success) {
  103.      # code...
  104.   }
  105.  
  106.  
  107. }
  108.  
  109.  
  110.  if (isset($_POST['submit']) && isset($_POST['submitted'])) {
  111.  
  112.   // Recaptcha fail
  113.   if ( empty($_POST['g-recaptcha-response']) ) {
  114.     $reg_errors->add("error", $recaptcha_required);
  115.      }
  116.  
  117.   if (empty($_POST['message_name'])) {
  118.     $reg_errors->add("error", $name_required);
  119.   }
  120.  
  121.   if (empty($_POST['message_email'])) {
  122.     $reg_errors->add("error", $email_required);
  123.   }
  124.  
  125.   if (empty($_POST['message_phone'])) {
  126.     $reg_errors->add("error", $phone_required);
  127.   }
  128.  
  129.   if (empty($_POST['message_text'])) {
  130.     $reg_errors->add("error", $text_required);
  131.   }
  132.  
  133.   //We check that the field Email is valid
  134.   if(!is_email($_POST['message_email'])) {
  135.    $reg_errors->add("error", $email_invalid);
  136.    }
  137.  
  138.   //validate presence of name and message
  139.  
  140.   if (count($reg_errors->get_error_messages()) == 0) {
  141.  
  142.        $subject = "Someone sent a message from " . get_bloginfo('name');
  143.  
  144.        $headers = 'From: '. $name . ' <' . $email . '>';          
  145.  
  146.       $sent = wp_mail($to, $subject, $message, $headers);
  147.  
  148.         gym_contact_create_entry($name, $email, $phone, $message );
  149.  
  150.         if($sent) {
  151.           unset($name);
  152.           unset($email);
  153.           unset($phone);
  154.           unset($message);?>
  155.           <div class="alert alert-danger" role="alert">
  156.                <?php echo $message_sent; ?>
  157.           </div>
  158.            
  159.         <?php }else { ?>
  160.           <div class="alert alert-success" role="alert">
  161.              <?php echo $message_unsent; ?>
  162.          </div>
  163.         <?php }
  164.      
  165.    }
  166.  
  167.  }
  168.  
  169.  
  170. function gym_contact_create_entry($name, $email, $phone, $message ) {
  171.   global $wpdb;
  172.   $table_name = $wpdb->prefix . 'contact';
  173.  
  174.   $wpdb->insert(
  175.         $table_name,
  176.         array(
  177.             'name' => $name,
  178.             'email' => $email,
  179.             'phone' => $phone,
  180.             'message' => $message,
  181.             'time' => current_time( 'mysql' )
  182.         )
  183.     );
  184. }
  185.  
  186.  ?>
  187.  
  188.  
  189.  <?php get_header(); ?>  
  190.        
  191.  
  192.            <?php
  193. // Get address defaults
  194. $c_addr_1 = get_post_meta(get_the_ID(), '_contact_addr_1', true);
  195. $c_addr_2 = get_post_meta(get_the_ID(), '_contact_addr_2', true);
  196. $c_addr = trim( $c_addr_1 . ' ' . $c_addr_2 );
  197.  
  198. // Get phone default
  199. $c_tel = get_post_meta(get_the_ID(), '_contact_phone', true);
  200.  
  201. // Get email default
  202. $c_email = get_post_meta(get_the_ID(), '_contact_email', true);
  203.  
  204. // Get text defaults
  205. $c_text_1 = get_post_meta(get_the_ID(), '_contact_text_1', true);
  206. $c_text_2 = get_post_meta(get_the_ID(), '_contact_text_2', true);
  207.  
  208. ?>
  209.  
  210.  
  211. <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  212.  
  213. <div class="container">
  214.   <div class="contact">
  215.     <div class="col-md-6 col-md-offset-3">
  216.       <div class="form-area">
  217.               <div class="text-center contact-h"><?php the_title();?></div>
  218.             <?php echo $response; ?>
  219.             <form id="contact-form" action="<?php the_permalink(); ?>" method="post">
  220.                   <div class="group form-group">
  221.                       <input class="form-control" id="name" type="text" name="message_name" value="<?php if (isset($_POST['message_name'])) { echo esc_attr($_POST['message_name']);} ?>">
  222.                       <span class="highlight"></span>
  223.                       <span class="bar"></span>
  224.                       <label for="name">Name</label>
  225.                   </div><!-- end div group form-group -->
  226.                   <div class="group form-group">
  227.                       <input class="form-control"  id="email" type="email" name="message_email" value="<?php if (isset($_POST['message_email'])) { echo esc_attr($_POST['message_email']);} ?>">
  228.                       <span class="highlight"></span>
  229.                       <span class="bar"></span>
  230.                       <label for="message_email">Email</label>
  231.                   </div><!-- end div group form-group -->
  232.                   <div class="group form-group">
  233.                       <input class="form-control"  id="phone" type="tel" name="message_phone" value="<?php if (isset($_POST['message_phone'])) { echo esc_attr( $_POST['message_phone']); } ?>">
  234.                       <span class="highlight"></span>
  235.                       <span class="bar"></span>
  236.                       <label for="message_phone">Phone</label>
  237.                   </div><!-- end div group form-group -->
  238.                   <div class="group form-group">
  239.                       <div class="text-group">
  240.                           <textarea class="form-control" type="text" name="message_text" rows="4"><?php if (isset($_POST['message_text'])) { echo esc_textarea($_POST['message_text']); } ?></textarea>
  241.                           <label for="message_text" class="input-label">Message</label>
  242.                           <i class="bar"></i>
  243.                       </div><!-- end div text-group -->
  244.                   </div><!-- end div group form-group -->
  245.                   <div class="g-recaptcha" data-sitekey="6Ld61NkUAAAAAJJ60gH6Ku38xJwj8nzKWbYiaecs"></div>
  246.                   <input type="hidden" name="submitted" value="custom_action">
  247.                   <?php wp_nonce_field( 'custom_action_nonce', 'gymclub_nonce_field' ); ?>
  248.                   <button class="btn btn-primary" id="submit" type="submit" id="gymclub-submit" name="submit">Send</button>
  249.             </form><!-- end form -->
  250.       </div><!--end respond -->
  251.    </div><!-- end div -->
  252.  </div><!-- end div contact -->
  253. </div><!-- end container -->
  254.        
  255. <?php get_footer(); ?>
  256.  
  257.  
  258.  
  259.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement