Witam,
Szukałem w Google rozwiązań dla formularzy kontaktowych phpmailer z wykorzystaniem ReCaptcha i niestety żadna metoda nie pomogła mi poprawnie skonfigurować kodu.
<?php
add_shortcode('contact_form', 'ts_contact_form_func');
function ts_contact_form_func($atts, $content = null)
{
'animation' => '',
'name_label' => '',
'name_icon' => '',
'phone_label' => '',
'phone_icon' => '',
'email_label' => '',
'email_icon' => '',
'message_label' => '',
'send_label' => '',
'clear_label' => '',
'skin' => 1,
'button_align' => 'left',
), $atts)
);
$style = '';
if(!empty($button_align)){ $style = 'style="text-align: ' . $button_align . '";';
}
$skin_class = '';
if ($skin == 1) {
$skin_class = 'light';
}
$html = '
<form '.$style.' class="get-in-touch contact-form '. ts_get_animation_class($animation) . ' '.$skin_class.'" method="post">
'.wp_nonce_field( 'contact_form_submission', 'contact_nonce', true,false).'
<input type="hidden" name="contact-form-value" value="1" id=""/>
<div class="iconic-input">
<input type="text" name="name" placeholder="' . $name_label . '*">
<i class="icons ' . $name_icon . '"></i>
</div>
<div class="iconic-input">
<input type="text" name="phone" placeholder="' . $phone_label . '">
<i class="icons ' . $phone_icon . '"></i>
</div>
<div class="iconic-input">
<input type="text" name="email" placeholder="' . $email_label . '*">
<i class="icons ' . $email_icon . '"></i>
</div>
<textarea name="msg" placeholder="' . $message_label . '*"></textarea>
<div class="g-recaptcha" data-sitekey="6LcvBRcTAAAAAHyeXhYpckbzQA7FDHyTjTk0FZF_"></div>
<input type="submit" value="' . $send_label . '">
<div class="iconic-button">
<input type="reset" value="' . $clear_label . '">
<i class="icons icon-cancel-circle-1"></i>
</div>
</form>
<div id="msg"></div>';
return $html;
}
<?php
}
if (isset ( $_POST ['contact-form-value'] )) { function ts_process_contact_form() {
$headers = null;
if (! wp_verify_nonce ( $_POST ['contact_nonce'], 'contact_form_submission' )) {
$data ['status'] = 0;
$err [] = "Invalid Form Submission";
$data ['message'] = implode ( '<br>', $err ); }
if ($_POST ['contact-form-value'] == 1) {
$name = $field['Imię i nazwisko'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$captcha=$_POST['g-recaptcha-response'];
$phone = isset($_POST['phone']) ?
$_POST['phone'] : ''; $email = $field['Email'] = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$field['Message'] = filter_var($_POST['msg'], FILTER_SANITIZE_STRING);
foreach ( $field as $k => $v ) {
$err [] = $k . ' ' . __ ( 'is required', 'marine' );
}
}
{
$errMsg= '';
$google_url="https://www.google.com/recaptcha/api/siteverify";
$secret='---moj kod---';
$ip=$_SERVER['REMOTE_ADDR'];
$captchaurl=$google_url."?secret=".$secret."&response=".$captcha."&remoteip=".$ip;
$curl_init = curl_init();
curl_setopt($curl_init, CURLOPT_URL, $captchaurl);
curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_init, CURLOPT_TIMEOUT, 10);
$results = curl_exec($curl_init);
curl_close($curl_init);
$results= json_decode($results, true);
if($results['success']){
$errMsg="Valid reCAPTCHA code. You are human.";
}else{
$errMsg="Invalid reCAPTCHA code.";
}
}else{
$errMsg="Please re-enter your reCAPTCHA.";
}
if (! $field ['Email'] ) {
$err [] = __ ( 'Email is invalid', 'marine' );
}
$to = ot_get_option ( 'contact_form_email' );
$headers = "From: $name <$email>" . "\r\n";
add_filter ( 'wp_mail_content_type', 'set_html_content_type' );
function set_html_content_type() {
return 'text/html';
}
$subject = ot_get_option ( 'contact_form_subject' );
$subject = __('Query', 'marine');
}
$message_content = __('From:', 'marine').' '.$name."<br>";
$message_content .= __('Telefon:', 'marine').' '.$phone."<br>";
$message_content .= __('Email:', 'marine').' <a href="mailto'.esc_attr($email).'">'.$email.'</a>'."<br>";
$message_content .= __('Message:', 'marine').' '.$field['Message']."\n";
if (wp_mail ( $to, $subject, $message_content, $headers )) {
$data ['status'] = 1;
$data ['message'] = __( 'Message Sent', 'marine' );
} else {
$data ['status'] = 0;
$data ['message'] = __( 'An Error occured.', 'marine' );
}
remove_filter ( 'wp_mail_content_type', 'set_html_content_type' );
} else {
$data ['status'] = 0;
$data ['message'] = implode ( '<br>', $err ); }
echo json_encode
( $data ); }
}
add_action ( 'init', 'ts_process_contact_form' );
}
Ma ktoś pomysł, jak mi pomóc?