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
- */
- if ( ! defined( 'ABSPATH' ) ) exit;
- global $reg_errors;
- $reg_errors = new WP_error;
- $response = "";
- //response messages
- //$not_human = "Human verification incorrect.";
- $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'])):"";
- //$human = $_POST['message_human'];
- //php mailer variables
- function my_contact_form(){
- if (isset($_POST['gymclub_nonce_field']) && wp_verify_nonce( $_POST['gymclub_nonce_field'], 'custom_action_nonce')){
- $to = get_option('gym_contact_admin_email');
- $name = sanitize_text_field($_POST['message_name']);
- $email = sanitize_email($_POST['message_email']);
- $phone = sanitize_text_field($_POST['message_phone']);
- $message = wp_kses_data($_POST['message_text']);
- $subject = "Someone sent a message from " . get_bloginfo('name');
- $headers = 'From: '. $name . ' <' . $email . '>';
- $sent = wp_mail( $to, $subject, $message, $headers );
- if ($sent){
- $r = array(
- 'name' => $name,
- 'email' => $email,
- 'phone' => $phone,
- 'message' => $message,
- 'time' => current_time( 'mysql' )
- );
- 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);
- }
- // WordPress Ajax
- add_action( 'wp_ajax_my_contact', 'my_contact_form' );
- add_action( 'wp_ajax_nopriv_my_contact', 'my_contact_form' );
- // On send - works but prob not best practice 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...
- }
- }
- if (isset($_POST['submit']) && isset($_POST['submitted'])) {
- // Recaptcha fail
- if ( empty($_POST['g-recaptcha-response']) ) {
- $reg_errors->add("error", $recaptcha_required);
- }
- if (empty($_POST['message_name'])) {
- $reg_errors->add("error", $name_required);
- }
- if (empty($_POST['message_email'])) {
- $reg_errors->add("error", $email_required);
- }
- if (empty($_POST['message_phone'])) {
- $reg_errors->add("error", $phone_required);
- }
- if (empty($_POST['message_text'])) {
- $reg_errors->add("error", $text_required);
- }
- //We check that the field Email is valid
- if(!is_email($_POST['message_email'])) {
- $reg_errors->add("error", $email_invalid);
- }
- //validate presence of name and message
- if (count($reg_errors->get_error_messages()) == 0) {
- $subject = "Someone sent a message from " . get_bloginfo('name');
- $headers = 'From: '. $name . ' <' . $email . '>';
- $sent = wp_mail($to, $subject, $message, $headers);
- gym_contact_create_entry($name, $email, $phone, $message );
- if($sent) {
- unset($name);
- unset($email);
- unset($phone);
- unset($message);?>
- <div class="alert alert-danger" role="alert">
- <?php echo $message_sent; ?>
- </div>
- <?php }else { ?>
- <div class="alert alert-success" role="alert">
- <?php echo $message_unsent; ?>
- </div>
- <?php }
- }
- }
- 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(); ?>
- <?php
- // Get address defaults
- $c_addr_1 = get_post_meta(get_the_ID(), '_contact_addr_1', true);
- $c_addr_2 = get_post_meta(get_the_ID(), '_contact_addr_2', true);
- $c_addr = trim( $c_addr_1 . ' ' . $c_addr_2 );
- // Get phone default
- $c_tel = get_post_meta(get_the_ID(), '_contact_phone', true);
- // Get email default
- $c_email = get_post_meta(get_the_ID(), '_contact_email', true);
- // Get text defaults
- $c_text_1 = get_post_meta(get_the_ID(), '_contact_text_1', true);
- $c_text_2 = get_post_meta(get_the_ID(), '_contact_text_2', true);
- ?>
- <script src="https://www.google.com/recaptcha/api.js" async defer></script>
- <div class="container">
- <div class="contact">
- <div class="col-md-6 col-md-offset-3">
- <div class="form-area">
- <div class="text-center contact-h"><?php the_title();?></div>
- <?php echo $response; ?>
- <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">Name</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">Email</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">Phone</label>
- </div><!-- end div group form-group -->
- <div class="group form-group">
- <div class="text-group">
- <textarea class="form-control" 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">Message</label>
- <i class="bar"></i>
- </div><!-- end div text-group -->
- </div><!-- end div group form-group -->
- <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">Send</button>
- </form><!-- end form -->
- </div><!--end respond -->
- </div><!-- end div -->
- </div><!-- end div contact -->
- </div><!-- end container -->
- <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement