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!' );
- // On send - works but prob not best practice validate recaptcha https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)
- /*if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- $captcha = $_POST['g-recaptcha-response'];
- //Fields to sent
- $fields = array(
- 'secret' => '6Ld61NkUAAAAAI0JuA0dp_RL5_T9EucRdgLX2nVj',
- 'response' => '$captcha',
- 'remoteip' => $_SERVER['REMOTE_ADDR']
- );
- //Start Sesion in CURL or file_get_contents
- $ch = curl_init('https://www.google.com/recaptcha/api/siteverify');
- // Configurate CURL options
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 15);
- // Generate array code for URL
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
- // Get response
- $answer = json_decode(curl_exec($ch));
- if ($answer->success) {
- # code here
- }
- }
- */
- // Validate Fields Contact Form
- function gym_contact_validate_form(){
- if (isset($_POST['gymclub_nonce_field']) && wp_verify_nonce( $_POST['gymclub_nonce_field'], 'custom_action_nonce')){
- /* $email_invalid = "Email Address Invalid.";
- $name_required = "Name Required.";
- $email_required = "Email Address Required.";
- $phone_required = "Phone Required.";
- $text_required = "Message Text Required.";
- $missing_content = "Please supply all information.";
- $message_unsent = "Message was not sent. Try Again.";
- $message_sent = "Thanks! Your message has been sent.";
- $recaptcha_required = "Are you robot?";
- */
- //user posted variables
- $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'])):"";
- //The Name field is required, we check that it is not empty and if not we create an error record
- if ( empty( $name ) ) {
- $reg_errors->add('empty-name', esc_html__('The field name is required', 'gymclub'));
- }
- //The Email field is required, we check that it is not empty and if not we create an error record
- if ( empty( $email ) ) {
- $reg_errors->add('empty-email', esc_html__('The field email is required', 'gymclub'));
- }
- //We check that the data is in e-mail format with the WordPress
- if ( !is_email( $email ) ) {
- $reg_errors->add('invalid-email', esc_html__('The e-mail is not in a valid format', 'gymclub'));
- }
- //The Message field is required, we check that it is not empty and if not we create an error record
- if ( empty( $message ) ) {
- $reg_errors->add('empty-message', esc_html__('The field message is required', 'gymclub'));
- }
- //If there are no errors we send the form
- if (count($reg_errors->get_error_messages()) == 0) {
- //Recipient
- $to = get_option('gym_contact_admin_email');
- //Subject of the email
- $subject = esc_html__('Someone sent a message from', 'gymclub') . get_bloginfo( 'name' );
- //The email address is that of our blog so by adding this header we can respond to the original sender
- $headers = "Reply-to: " . $name . " <" . $email . ">\r\n";
- //We mount the body of our e-mail
- $message = "Name: " . $name . "<br>";
- $message .= "Email: " . $email . "<br>";
- $message .= "Phone: " . $phone . "<br>";
- $message .= "Message: " . nl2br($message) . "<br>";
- //Filter to indicate which email should be sent in HTM modeL
- add_filter('wp_mail_content_type', create_function('', 'return "text/html";'));
- //Finally we send the email
- $sent = wp_mail( $recipient, $subject, $message, $headers, $attachments);
- }//end if(count)
- }//end if(!iiset)
- }// end function
- add_action( 'init', 'gym_contact_validate_form' );
- // Show errors in data fields
- function gym_error_messages(){
- global $reg_errors;
- if (!isset($reg_errors)):
- $reg_errors = new WP_Error;
- endif;
- return $reg_errors;
- }
- // Register errors fields
- function gym_register_errors(){
- global $reg_errors;
- $reg_errors = new WP_Error;
- if (is_wp_error( $reg_errors )) {
- if (count($reg_errors->get_error_message()) > 0 ) {
- foreach ($reg_errors->get_error_messages() as $error ) { ?>
- <div class="row margin-button-small">
- <div class="col-md-12 alert alert-warning">
- <button type="button" class="close" data-dismiss="alert" aria-label="close">
- <span aria-hidden="true">×</span>
- </button>
- <p class="message"><?php echo __($error, 'gymclub'); ?></p>
- </div>
- </div>
- <?php}
- }
- }
- }
- // 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' )
- )
- );
- }
- // 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' );
- ?>
- <?php get_header(); ?>
- <script src="https://www.google.com/recaptcha/api.js" async defer></script>
- <?php
- if (isset( $_POST['gymclub_nonce_field'] )):
- gym_register_errors();
- endif;
- ?>
- <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 -->
- </div><!-- end div contact -->
- <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 -->
- <?php endwhile;
- endif;?>
- </article><!-- end section -->
- <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement