【问题标题】:Adding reCaptcha in wordpress protected page form以 wordpress 保护的页面形式添加 reCaptcha
【发布时间】:2018-04-12 10:52:31
【问题描述】:

我正在尝试在受密码保护的页面上显示的表单上实施 reCaptcha 验证。我尝试了不同的插件,但在受密码保护的页面上没有显示选项。 我找到了一种在页面上显示 reCaptcha 字段的方法,其中包含我在主题文件夹的 functions.php 文件中添加的自定义表单功能。但我不知道如何在提交信息之前验证 reCaptcha。

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

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

【问题讨论】:

    标签: php wordpress recaptcha


    【解决方案1】:

    我不是专家,但您可以尝试将其添加到您的 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');
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-24
      • 2012-11-06
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      • 2021-06-10
      • 2011-06-21
      • 2011-10-23
      • 1970-01-01
      相关资源
      最近更新 更多