Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Contact form functionality
- * Used within the loop
- * @link https://developer.wordpress.org/reference/functions/wp_nonce_field/
- * @link https://codex.wordpress.org/Class_Reference/WP_Error
- * @link https://developer.wordpress.org/reference/functions/wp_is_mobile/
- * @link https://developer.wordpress.org/reference/functions/wp_kses_data/
- * @link https://developer.wordpress.org/reference/functions/wp_remote_retrieve_response_code/
- * @link https://developer.wordpress.org/reference/functions/wp_get_referer/
- */
- defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
- // Validate Fields Contact Form
- function gym_contact_validate_form(){
- if (isset($_POST['submit']) && isset($_POST['gymclub_nonce_field'])) {
- if (wp_verify_nonce( $_POST['gymclub_nonce_field'], 'custom_action_nonce')) {
- //user posted variables
- $to = get_option('gym_contact_admin_email');
- $name = isset ($_POST['message_name'])? esc_sql(sanitize_text_field($_POST['message_name'])):"";
- $email = isset($_POST['message_email'])? esc_sql(sanitize_text_field(sanitize_email($_POST['message_email']))):"";
- $phone = isset($_POST['message_phone'])? esc_sql(sanitize_text_field($_POST['message_phone'])):"";
- $message = isset($_POST['message_text'])? esc_sql(sanitize_text_field($_POST['message_text'])):"";
- // If any field is left empty, add the error message to the error array
- if ( empty($name) || empty($email) || empty($phone) || empty($message) ) {
- array_push( $this->form_errors, 'No field should be left empty' );
- }
- // if the name field isn't alphabetic, add the error message
- if ( strlen($name) < 4 ) {
- array_push( $this->form_errors, 'Name should be at least 4 characters' );
- }
- // Check if the email is valid
- if ( !is_email($email) ) {
- array_push( $this->form_errors, 'Email is not valid' );
- }
- $headers[] = 'From: '. $name . ' <' . $email . '>';
- $sent = wp_mail( $to, $subject, $message, $headers );
- if ($sent){
- $r = array(
- 'name' => $name,
- 'email' => $email,
- 'to' => $to,
- 'subject' => $subject,
- 'message' => $message);
- wp_send_json_success($r);
- } else {
- $r = array('message' => 'Mail Error');
- wp_send_json_error($r);
- }
- $r = array('message' => 'Validate Error' );
- wp_send_json_error($r);
- }// end verify nonce
- } //end isset
- }// end function
- // WordPress Ajax
- add_action( 'wp_ajax_gym_contact_create_entry', 'gym_contact_create_entry' );
- add_action( 'wp_ajax_nopriv_my_contact', 'gym_contact_create_entry' );
- // Ajax insert data contact entry
- function gym_contact_create_entry($name, $email, $phone, $message ) {
- global $wpdb;
- $table_name = $wpdb->prefix . 'contact';
- $wpdb->insert(
- $table_name,
- array(
- 'name' => $name,
- 'email' => $email,
- 'phone' => $phone,
- 'message' => $message,
- 'time' => current_time( 'mysql' )
- )
- );
- }
- ?>
- <?php get_header(); ?>
- <script src="https://www.google.com/recaptcha/api.js" async defer></script>
- <article class="container">
- <?php if (have_posts()):
- while (have_posts()): the_post(); ?>
- <div class="row contact">
- <div class="col-md-6">
- <div class="form-area">
- <div class="text-center contact-h"><?php the_title();?></div>
- <form id="contact-form" action="<?php the_permalink(); ?>" method="post">
- <div class="group form-group">
- <input class="form-control" id="name" type="text" name="message_name" value="<?php if (isset($_POST['message_name'])) { echo esc_attr($_POST['message_name']);} ?>">
- <span class="highlight"></span>
- <span class="bar"></span>
- <label for="name"><?php echo __('Name', 'gymclub'); ?></label>
- </div><!-- end div group form-group -->
- <div class="group form-group">
- <input class="form-control" id="email" type="email" name="message_email" value="<?php if (isset($_POST['message_email'])) { echo esc_attr($_POST['message_email']);} ?>">
- <span class="highlight"></span>
- <span class="bar"></span>
- <label for="message_email"><?php echo __('Email', 'gymclub'); ?></label>
- </div><!-- end div group form-group -->
- <div class="group form-group">
- <input class="form-control" id="phone" type="tel" name="message_phone" value="<?php if (isset($_POST['message_phone'])) { echo esc_attr( $_POST['message_phone']); } ?>">
- <span class="highlight"></span>
- <span class="bar"></span>
- <label for="message_phone"><?php echo __('Phone', 'gymclub'); ?></label>
- </div><!-- end div group form-group -->
- <div class="group form-group">
- <div class="text-group">
- <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>
- <label for="message_text" class="input-label"><?php echo __('Message', 'gymclub'); ?></label>
- <i class="bar"></i>
- </div><!-- end div text-group -->
- </div><!-- end div group form-group -->
- <p class="message_success" id="message_success">Hola desde el formulario</p>
- <p class="message_error" id="message_error">Hola desde el formulario</p>
- <div class="g-recaptcha" data-sitekey="6Ld61NkUAAAAAJJ60gH6Ku38xJwj8nzKWbYiaecs"></div>
- <!--<input type="hidden" name="submitted" value="custom_action">-->
- <?php wp_nonce_field( 'custom_action_nonce', 'gymclub_nonce_field' ); ?>
- <button class="btn btn-primary" id="submit" type="submit" id="gymclub-submit" name="submit"><?php echo __('Send', 'gymclub'); ?></button>
- </form><!-- end form -->
- </div><!--end respond -->
- </div><!-- end div col-md-6 -->
- <div class="col-md-6" itemscope itemtype="http://schema.org/LocalBusiness">
- <h3><?php echo __('Dates', 'gymclub'); ?></h3>
- <span class="dates_contact" itemprop="name"><i class="fas fa-building"></i><?php echo esc_attr( get_option('gym_contact_name_company') ); ?></span>
- <div itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
- <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>
- <span class="dates_contact" itemprop="telephone"><i class="fas fa-phone"></i><?php echo esc_attr( get_option('gym_contact_phone_company') ); ?></span>
- <span class="dates_contact" itemprop="email"><i class="far fa-envelope"></i><?php echo esc_attr( get_option('gym_contact_admin_email') ); ?></span>
- <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>
- </div><!-- end div itemprop -->
- </div><!--end div col-md-6 div itemscope -->
- </div><!-- end div contact -->
- <?php endwhile;
- endif;?>
- </article><!-- end section -->
- <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement