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.

  1. <?php
  2.  
  3. add_shortcode('contact_form', 'ts_contact_form_func');
  4.  
  5. function ts_contact_form_func($atts, $content = null)
  6. {
  7.  
  8. shortcode_atts(array(
  9. 'animation' => '',
  10. 'name_label' => '',
  11. 'name_icon' => '',
  12. 'phone_label' => '',
  13. 'phone_icon' => '',
  14. 'email_label' => '',
  15. 'email_icon' => '',
  16. 'message_label' => '',
  17. 'send_label' => '',
  18. 'clear_label' => '',
  19. 'skin' => 1,
  20. 'button_align' => 'left',
  21. ), $atts)
  22.  
  23. );
  24.  
  25. $style = '';
  26. if(!empty($button_align)){
  27. $style = 'style="text-align: ' . $button_align . '";';
  28. }
  29.  
  30. $skin_class = '';
  31. if ($skin == 1) {
  32. $skin_class = 'light';
  33. }
  34.  
  35. $html = '
  36. <form '.$style.' class="get-in-touch contact-form '. ts_get_animation_class($animation) . ' '.$skin_class.'" method="post">
  37. '.wp_nonce_field( 'contact_form_submission', 'contact_nonce', true,false).'
  38. <input type="hidden" name="contact-form-value" value="1" id=""/>
  39. <div class="iconic-input">
  40. <input type="text" name="name" placeholder="' . $name_label . '*">
  41. <i class="icons ' . $name_icon . '"></i>
  42. </div>
  43. <div class="iconic-input">
  44. <input type="text" name="phone" placeholder="' . $phone_label . '">
  45. <i class="icons ' . $phone_icon . '"></i>
  46. </div>
  47. <div class="iconic-input">
  48. <input type="text" name="email" placeholder="' . $email_label . '*">
  49. <i class="icons ' . $email_icon . '"></i>
  50. </div>
  51. <textarea name="msg" placeholder="' . $message_label . '*"></textarea>
  52. <div class="g-recaptcha" data-sitekey="6LcvBRcTAAAAAHyeXhYpckbzQA7FDHyTjTk0FZF_"></div>
  53. <input type="submit" value="' . $send_label . '">
  54. <div class="iconic-button">
  55. <input type="reset" value="' . $clear_label . '">
  56. <i class="icons icon-cancel-circle-1"></i>
  57. </div>
  58. </form>
  59. <div id="msg"></div>';
  60. return $html;
  61. }


  1. <?php
  2. }
  3. if (isset ( $_POST ['contact-form-value'] )) {
  4. function ts_process_contact_form() {
  5. $data = array ();
  6. $err = array ();
  7. $headers = null;
  8. if (! wp_verify_nonce ( $_POST ['contact_nonce'], 'contact_form_submission' )) {
  9. $data ['status'] = 0;
  10. $err = array ();
  11. $err [] = "Invalid Form Submission";
  12. $data ['message'] = implode ( '<br>', $err );
  13. exit ( 0 );
  14. }
  15.  
  16. if ($_POST ['contact-form-value'] == 1) {
  17. $name = $field['Imię i nazwisko'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
  18. $captcha=$_POST['g-recaptcha-response'];
  19. $phone = isset($_POST['phone']) ? $_POST['phone'] : '';
  20. $email = $field['Email'] = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
  21. $field['Message'] = filter_var($_POST['msg'], FILTER_SANITIZE_STRING);
  22.  
  23. foreach ( $field as $k => $v ) {
  24. if (trim ( $v ) == '') {
  25. $err [] = $k . ' ' . __ ( 'is required', 'marine' );
  26. }
  27. }
  28. if(!empty($captcha))
  29. {
  30. $errMsg= '';
  31. $google_url="https://www.google.com/recaptcha/api/siteverify";
  32. $secret='---moj kod---';
  33. $ip=$_SERVER['REMOTE_ADDR'];
  34. $captchaurl=$google_url."?secret=".$secret."&response=".$captcha."&remoteip=".$ip;
  35.  
  36. $curl_init = curl_init();
  37. curl_setopt($curl_init, CURLOPT_URL, $captchaurl);
  38. curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, 1);
  39. curl_setopt($curl_init, CURLOPT_TIMEOUT, 10);
  40. $results = curl_exec($curl_init);
  41. curl_close($curl_init);
  42.  
  43. $results= json_decode($results, true);
  44. if($results['success']){
  45. $errMsg="Valid reCAPTCHA code. You are human.";
  46. }else{
  47. $errMsg="Invalid reCAPTCHA code.";
  48. }
  49. }else{
  50. $errMsg="Please re-enter your reCAPTCHA.";
  51. }
  52. if (! $field ['Email'] ) {
  53. $err [] = __ ( 'Email is invalid', 'marine' );
  54. }
  55.  
  56. if (empty ( $err )) {
  57.  
  58. $to = ot_get_option ( 'contact_form_email' );
  59. $headers = "From: $name <$email>" . "\r\n";
  60.  
  61. add_filter ( 'wp_mail_content_type', 'set_html_content_type' );
  62. function set_html_content_type() {
  63. return 'text/html';
  64. }
  65.  
  66. $subject = ot_get_option ( 'contact_form_subject' );
  67. if (empty($subject)) {
  68. $subject = __('Query', 'marine');
  69. }
  70.  
  71. $message_content = __('From:', 'marine').' '.$name."<br>";
  72. $message_content .= __('Telefon:', 'marine').' '.$phone."<br>";
  73. $message_content .= __('Email:', 'marine').' <a href="mailto'.esc_attr($email).'">'.$email.'</a>'."<br>";
  74. $message_content .= __('Message:', 'marine').' '.$field['Message']."\n";
  75.  
  76. if (wp_mail ( $to, $subject, $message_content, $headers )) {
  77. $data ['status'] = 1;
  78. $data ['message'] = __( 'Message Sent', 'marine' );
  79. } else {
  80. $data ['status'] = 0;
  81. $data ['message'] = __( 'An Error occured.', 'marine' );
  82. }
  83.  
  84. remove_filter ( 'wp_mail_content_type', 'set_html_content_type' );
  85. } else {
  86. $data ['status'] = 0;
  87. $data ['message'] = implode ( '<br>', $err );
  88. }
  89. echo json_encode ( $data );
  90. exit ( 0 );
  91. }
  92. }
  93. add_action ( 'init', 'ts_process_contact_form' );
  94. }


Ma ktoś pomysł, jak mi pomóc?