在wordpress受保护的页面表单中添加reCaptcha

时间:2018-04-12 10:52:31

标签: php wordpress recaptcha

我正在尝试在受密码保护的页面上显示的表单上实施reCaptcha验证。我尝试了不同的插件,并且在密码保护页面上没有显示选项。 我找到了一种方法来显示页面上的reCaptcha字段,其中包含我在theme.php文件中添加的自定义表单函数。但是我不知道如何在提交信息之前验证reCaptcha。< / p>

function my_custom_js() {
echo '
 <script src="https://www.google.com/recaptcha/api.js" async defer></script>
 <script>
   function onSubmit(token) {
     document.getElementById("form1").submit();
   }
 </script>';
}

add_action('wp_head', 'my_custom_js');
add_filter( 'the_password_form', 'custom_password_form' );
function custom_password_form() {
global $post;
$label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
$o = '<form class="protected-post-form" id="form1" action="' . get_option('siteurl') . '/wp-login.php?action=postpass" method="post" >
' . __( "This is a password protected page. Please enter the password below:" ) . '
<label class="pass-label" for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password"  />
<input type="submit" name="Submit" class="button" value="' . esc_attr__( "Submit" ) . '" />
<button
class="g-recaptcha button"
data-sitekey="6Ld2wVIUAAAAAPnw70ZMDA9Auxu_Tl1rqV-W4kxv"
data-callback="onSubmit">
Submit
</button>
</form>
';
return $o;
}

我也收到错误消息:

Invalid reCAPTCHA client id: null
    at Object.Cr [as reset] (recaptcha__en.js:391)
    at (index):1158
    at wp_recaptchaLoadCallback ((index):1160)
    at recaptcha__en.js:394
    at ir (recaptcha__en.js:378)
    at recaptcha__en.js:394
    at recaptcha__en.js:405

我被困住了。我不知道接下来该怎么做。 谢谢您阅读此篇。如果有人能指出我正确的方向,那对我来说意味着世界。

1 个答案:

答案 0 :(得分:0)

我不是专家,但是您可以尝试将其添加到header.php

<script src="https://www.google.com/recaptcha/api.js" async defer></script>

然后您可以使用function.php中的函数调用js脚本

add_action('wp_enqueue_scripts','password_js');
function password_js() {
  wp_enqueue_script( 'password', get_template_directory_uri() . '/js/password.js');
}