Cara Menampilkan Recaptha Pada Login Form, Reset Password dan Comment WordPress Tanpa Plugin

Cara Menampilkan Recaptha Pada Login Form, Reset Password dan Comment WordPress Tanpa Plugin

Ini Cara Menampilkan Recaptha Pada Login Form, Reset Password dan Comment WordPress Tanpa Plugin. Kenapa tidak pakai plugin recaptcha saja? kalau anda mau pakai plugin silahkan tapi kalo tidak mau pake plugin bisa ikuti Cara Menampilkan Recaptha Pada Login Form, Reset Password dan Comment WordPress Tanpa Plugin ini simple kan?

Langkah-langkah Menampilkan Recaptha Pada Login Form, Reset Password dan Comment WordPress

Ikuti Langkah-langkah cara mudah memasang Recaptha Google pada Login Form, Reset Password dan Comment WordPress tanpa harus menginstall plugin.

Caranya sangat mudah yaitu anda hanya harus copy dan paste kode snippet dibawah ini. Paste pada file function.php yang ada di themes directory website.

Ata bisa juga menggunakan plugin Code Snippet Plugin.

PERTAMA

Copy kode snippet dibawah ini

<?php


//** RECAPTCHA  DASHBOARD SETTING MENU*/

function no_captcha_recaptcha_menu() { 
  add_menu_page("reCapatcha Options", "reCaptcha Options", "manage_options", "recaptcha-options", "recaptcha_options_page", "", 100); 
} 
function recaptcha_options_page() { ?> 
  <div class="wrap"> 
  <h1>reCaptcha Options</h1> 
  <form method="post" action="options.php"> 
  <?php settings_fields("header_section"); 
  do_settings_sections("recaptcha-options"); 
  submit_button(); ?>   
  </form> 
  </div> 
<?php } 
add_action("admin_menu", "no_captcha_recaptcha_menu"); 
function display_recaptcha_options() { 
  add_settings_section("header_section", "Keys", "display_recaptcha_content", "recaptcha-options"); 
  add_settings_field("captcha_site_key", __("Site Key"), "display_captcha_site_key_element", "recaptcha-options", "header_section"); 
  add_settings_field("captcha_secret_key", __("Secret Key"), "display_captcha_secret_key_element", "recaptcha-options", "header_section"); 
  register_setting("header_section", "captcha_site_key"); 
  register_setting("header_section", "captcha_secret_key"); 
} 
function display_recaptcha_content() { 
  echo __('<p>You need to <a href="https://www.google.com/recaptcha/admin" rel="external">register you domain</a> and get keys to make this plugin work.</p>'); 
  echo __("Enter the key details below"); 
} 
function display_captcha_site_key_element() { ?> 
  <input type="text" name="captcha_site_key" id="captcha_site_key" value="<?php echo get_option('captcha_site_key'); ?>" /> 
<?php } 
function display_captcha_secret_key_element() { ?> 
  <input type="text" name="captcha_secret_key" id="captcha_secret_key" value="<?php echo get_option('captcha_secret_key'); ?>" /> 
<?php } 
add_action("admin_init", "display_recaptcha_options"); 

Fungsi kode diatas adalah untuk membuat menu baru di sidebar WordPress dengan nama menu "reCaptcha Options".

Kemudian copy dan paste kode dibawah ini untuk menampilkan Recaptcha Google berdasarkan kebutuhan.

ReCaptcha Untuk Login Form

Kode untuk memasang captcha untuk login form:

//** RECAPTCHA  LOGIN FORM*/

function login_recaptcha_script() { 
  wp_register_script("recaptcha_login", "https://www.google.com/recaptcha/api.js"); 
  wp_enqueue_script("recaptcha_login"); 
} 
add_action("login_enqueue_scripts", "login_recaptcha_script"); 
function display_login_captcha() { ?> 
  <div class="g-recaptcha" data-sitekey="<?php echo get_option('captcha_site_key'); ?>"></div> 
<?php } 
add_action( "login_form", "display_login_captcha" ); 
function verify_login_captcha($user, $password) { 
  if (isset($_POST['g-recaptcha-response'])) { 
  $recaptcha_secret = get_option('captcha_secret_key'); 
  $response = wp_remote_get("https://www.google.com/recaptcha/api/siteverify?secret=". $recaptcha_secret ."&response=". $_POST['g-recaptcha-response']); 
  $response = json_decode($response["body"], true); 
  if (true == $response["success"]) { 
  return $user; 
  } else { 
  return new WP_Error("Captcha Invalid", __("<strong>ERROR</strong>: You are a bot")); 
  }  
  } else { 
  return new WP_Error("Captcha Invalid", __("<strong>ERROR</strong>: You are a bot. If not then enable JavaScript")); 
  }   
} 
add_filter("wp_authenticate_user", "verify_login_captcha", 10, 2);

Recaptcha Untuk Register Form

Copy kode dibawah dan paste pada file function.php

Code:function display_register_captcha() { ?> 
  <div class="g-recaptcha" data-sitekey="<?php echo get_option('captcha_site_key'); ?>"></div> 
<?php } 
add_action("register_form", "display_register_captcha"); 
function verify_registration_captcha($errors, $sanitized_user_login, $user_email) { 
  if (isset($_POST['g-recaptcha-response'])) { 
  $recaptcha_secret = get_option('captcha_secret_key'); 
  $response = wp_remote_get("https://www.google.com/recaptcha/api/siteverify?secret=". $recaptcha_secret ."&response=". $_POST['g-recaptcha-response']); 
  $response = json_decode($response["body"], true); 
  if (true == $response["success"]) { 
  return $errors; 
  } else { 
  $errors->add("Captcha Invalid", __("<strong>ERROR</strong>: You are a bot")); 
  } 
  } else { 
  $errors->add("Captcha Invalid", __("<strong>ERROR</strong>: You are a bot. If not then enable JavaScript")); 
  } 
  return $errors; 
} 
add_filter("registration_errors", "verify_registration_captcha", 10, 3); 

Recaptcha Untuk Register Form

function frontend_recaptcha_script() { 
  wp_register_script("recaptcha", "https://www.google.com/recaptcha/api.js"); 
  wp_enqueue_script("recaptcha"); 
  $plugin_url = plugin_dir_url(__FILE__); 
  wp_enqueue_style("no-captcha-recaptcha", $plugin_url ."style.css"); 
} 
add_action("wp_enqueue_scripts", "frontend_recaptcha_script"); 
function display_comment_recaptcha() { ?> 
  <div class="g-recaptcha" data-sitekey="<?php echo get_option('captcha_site_key'); ?>"></div> 
  <input name="submit" type="submit" value="Submit Comment"> 
<?php } 
add_action("comment_form", "display_comment_recaptcha"); 
function verify_comment_captcha($commentdata) { 
  if (isset($_POST['g-recaptcha-response'])) { 
  $recaptcha_secret = get_option('captcha_secret_key'); 
  $response = wp_remote_get("https://www.google.com/recaptcha/api/siteverify?secret=". $recaptcha_secret ."&response=". $_POST['g-recaptcha-response']); 
  $response = json_decode($response["body"], true); 
  if (true == $response["success"]) { 
  return $commentdata; 
  } else { 
  echo __("Bots are not allowed to submit comments."); 
  return null; 
  } 
  } else { 
  echo __("Bots are not allowed to submit comments. If you are not a bot then please enable JavaScript in browser."); 
  return null; 
  } 
} 
 add_filter("preprocess_comment", "verify_comment_captcha");