Advertisement
blackimpala

Formulario Contacto Personalizado

Dec 23rd, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.52 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.         $to = get_option('gym_contact_admin_email');
  31.         $name = isset ($_POST['message_name'])? esc_sql(sanitize_text_field($_POST['message_name'])):"";
  32.         $email = isset($_POST['message_email'])? esc_sql(sanitize_text_field(sanitize_email($_POST['message_email']))):"";
  33.         $phone = isset($_POST['message_phone'])? esc_sql(sanitize_text_field($_POST['message_phone'])):"";
  34.         $message = isset($_POST['message_text'])? esc_sql(sanitize_text_field($_POST['message_text'])):"";
  35.        
  36.         $headers = 'From: '. $name . ' <' . $email . '>';
  37.         $sent = wp_mail( $to, $subject, $message, $headers );
  38.            
  39.            if ($sent){
  40.          
  41.             $r = array(
  42.                   'name'  => $name,
  43.                   'email' => $email,
  44.                   'phone' => $phone,
  45.                   'message' => $message,
  46.                   'time' => current_time( 'mysql' )
  47.             );
  48.              wp_send_json_success($r);
  49.            } else {
  50.             $r = array('message' => 'Mail Error');
  51.             wp_send_json_error($r);
  52.            }        
  53.        
  54.     }
  55.        $r = array('message' => 'Validate Error' );
  56.            wp_send_json_error($r);
  57.    
  58.    }
  59.  
  60. // WordPress Ajax
  61. add_action( 'wp_ajax_my_contact', 'my_contact_form' );
  62. add_action( 'wp_ajax_nopriv_my_contact', 'my_contact_form' );
  63.  
  64.  
  65.  
  66. // On send - works but prob not best practice validate recaptcha https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)
  67.  
  68. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  69.    $captcha = $_POST['g-recaptcha-response'];
  70.  
  71.    //Fields to sent
  72.    $fields = array(
  73.         'secret' => '6Ld61NkUAAAAAI0JuA0dp_RL5_T9EucRdgLX2nVj',
  74.         'response' => '$captcha',
  75.         'remoteip' => $_SERVER['REMOTE_ADDR']
  76.      );
  77.  
  78.    //Start Sesion in CURL or file_get_contents
  79.    $ch = curl_init('https://www.google.com/recaptcha/api/siteverify');
  80.  
  81.    // Configurate CURL options
  82.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  83.    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  84.  
  85.    // Generate array code for URL
  86.    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
  87.  
  88.    // Get response
  89.    $answer = json_decode(curl_exec($ch));
  90.    if ($answer->success) {
  91.      # code here
  92.  
  93.    }
  94.  
  95.  
  96. }
  97.  
  98.  
  99.  
  100. function gym_contact_create_entry($name, $email, $phone, $message ) {
  101.   global $wpdb;
  102.   $table_name = $wpdb->prefix . 'contact';
  103.  
  104.   $wpdb->insert(
  105.         $table_name,
  106.         array(
  107.             'name' => $name,
  108.             'email' => $email,
  109.             'phone' => $phone,
  110.             'message' => $message,
  111.             'time' => current_time( 'mysql' )
  112.         )
  113.     );
  114. }
  115.  
  116.  ?>
  117.  
  118.  
  119.  <?php get_header(); ?>  
  120.      
  121.  
  122. <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  123.  
  124. <div class="container">
  125.  
  126.   <div class="contact">
  127.     <div class="col-md-6">
  128.       <div class="form-area">
  129.             <div class="text-center contact-h"><?php the_title();?></div>
  130.             <form id="contact-form" action="<?php the_permalink(); ?>" method="post">
  131.                   <div class="group form-group">
  132.                       <input class="form-control" id="name" type="text" name="message_name" value="<?php if (isset($_POST['message_name'])) { echo esc_attr($_POST['message_name']);} ?>">
  133.                       <span class="highlight"></span>
  134.                       <span class="bar"></span>
  135.                       <label for="name">Name</label>
  136.                   </div><!-- end div group form-group -->
  137.                   <div class="group form-group">
  138.                       <input class="form-control"  id="email" type="email" name="message_email" value="<?php if (isset($_POST['message_email'])) { echo esc_attr($_POST['message_email']);} ?>">
  139.                       <span class="highlight"></span>
  140.                       <span class="bar"></span>
  141.                       <label for="message_email">Email</label>
  142.                   </div><!-- end div group form-group -->
  143.                   <div class="group form-group">
  144.                       <input class="form-control"  id="phone" type="tel" name="message_phone" value="<?php if (isset($_POST['message_phone'])) { echo esc_attr( $_POST['message_phone']); } ?>">
  145.                       <span class="highlight"></span>
  146.                       <span class="bar"></span>
  147.                       <label for="message_phone">Phone</label>
  148.                   </div><!-- end div group form-group -->
  149.                   <div class="group form-group">
  150.                       <div class="text-group">
  151.                           <textarea class="form-control" type="text" name="message_text" rows="4"><?php if (isset($_POST['message_text'])) { echo esc_textarea($_POST['message_text']); } ?></textarea>
  152.                           <label for="message_text" class="input-label">Message</label>
  153.                           <i class="bar"></i>
  154.                       </div><!-- end div text-group -->
  155.                   </div><!-- end div group form-group -->
  156.                   <div class="g-recaptcha" data-sitekey="6Ld61NkUAAAAAJJ60gH6Ku38xJwj8nzKWbYiaecs"></div>
  157.                   <input type="hidden" name="submitted" value="custom_action">
  158.                   <?php wp_nonce_field( 'custom_action_nonce', 'gymclub_nonce_field' ); ?>
  159.                   <button class="btn btn-primary" id="submit" type="submit" id="gymclub-submit" name="submit">Send</button>
  160.             </form><!-- end form -->
  161.       </div><!--end respond -->
  162.    </div><!-- end div -->  
  163.  </div><!-- end div contact -->
  164.  
  165.        <div itemscope itemtype="http://schema.org/LocalBusiness" class="col-md-6">
  166.           <h3><?php echo __('Dates', 'gymclub'); ?></h3>
  167.           <span class="dates_contact" itemprop="name"><i class="fas fa-building"></i><?php echo esc_attr( get_option('gym_contact_name_company') ); ?></span>
  168.           <div  itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
  169.             <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>
  170.              <span class="dates_contact" itemprop="telephone"><i class="fas fa-phone"></i><?php echo esc_attr( get_option('gym_contact_phone_company') ); ?></span>
  171.              <span class="dates_contact" itemprop="email"><i class="far fa-envelope"></i><?php echo esc_attr( get_option('gym_contact_admin_email') ); ?></span>
  172.              <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>
  173.           </div><!-- end div itemprop -->
  174.        </div><!--end div itemscope -->
  175.  
  176. </div><!-- end container -->
  177.        
  178. <?php get_footer(); ?>
  179.  
  180.  
  181.  
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement