This repository has been archived by the owner on May 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinsert-code-function.php
79 lines (68 loc) · 3.21 KB
/
insert-code-function.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// RECAPTCHA V3
// MENU OPTIONS
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");
//LOGIN PAGE
function login_recaptcha_script() {
$recaptcha_key= get_option('captcha_site_key');
wp_register_script("recaptcha_login", "https://www.google.com/recaptcha/api.js?render=". $recaptcha_key ."");
wp_enqueue_script("recaptcha_login");
}
add_action("login_enqueue_scripts", "login_recaptcha_script");
function display_login_captcha() { ?>
<script>grecaptcha.ready(function () {
grecaptcha.execute('<?php echo get_option('captcha_site_key'); ?>',
{ action: 'login' }).then(function (token) {
var recaptchaResponse = document.getElementById('recaptchaResponse');
recaptchaResponse.value = token;
});
});
</script>
<input type="hidden" name="recaptcha_response" id="recaptchaResponse">
<?php }
add_action( "login_form", "display_login_captcha" );
function verify_login_captcha($user, $password) {
if (isset($_POST['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['recaptcha_response']);
$response = json_decode($response);
if ($response->score >= 0.5) {
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);