Created
October 29, 2021 13:06
-
-
Save topmask/91052b17b7d180ad2284e4962bc9227d to your computer and use it in GitHub Desktop.
Contact form functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
*/ | |
defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); | |
if (isset($_POST['submitted']) && 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?"; | |
*/ | |
global $reg_errors; | |
$reg_errors = new WP_Error; | |
//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'])):""; | |
//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", "The field name is required"); | |
} | |
//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", "The field email is required"); | |
} | |
//We check that the data is in e-mail format with the WordPress | |
if ( !is_email( $email ) ) { | |
$reg_errors->add("invalid-email", "The e-mail is not in a valid format"); | |
} | |
//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", "The field message is required"); | |
} | |
//If there are no errors we send the form | |
if (count($reg_errors->get_error_messages()) == 0) { | |
//Recipient | |
$recipient = "destinatario@email.com"; | |
//Subject of the email | |
$subject = 'Contac Form' . 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); | |
//If the e-mail is sent correctly we show a message and empty the variables with the data. Otherwise we show an error message | |
if ($sent) { | |
$r = array( | |
'name' => $name, | |
'email' => $email, | |
'phone' => $phone, | |
'message' => $message | |
); | |
wp_send_json_success($r); | |
}else { | |
$r = array('message' => 'Mail Error'); | |
wp_send_json_error($r); | |
} | |
}//end if(count) | |
$r = array('message' => 'Validate Error' ); | |
wp_send_json_error($r); | |
}//end if(!iiset) | |
// 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 | |
} | |
} | |
*/ | |
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(); ?> | |
<?php | |
global $reg_errors; | |
$reg_errors = new WP_Error; | |
if (is_wp_error( $reg_errors )) { | |
if (count($reg_errors->get_error_messages()) > 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> | |
</div> | |
</div> | |
<?php } | |
} | |
} | |
?> | |
<script src="https://www.google.com/recaptcha/api.js" async defer></script> | |
<article class="container"> | |
<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" 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 --> | |
<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 --> | |
</article><!-- end section --> | |
<?php get_footer(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment