将 v3 Recaptcha 添加到自定义 Wordpress 注册表单

问题描述 投票:0回答:0

我有一个自定义的 wordpress 注册表,效果很好。然而,我真的很难获得一个 recaptcha。我更喜欢 v3,但如果它更容易实现的话,v2 也可以。

我知道该表格适用于此代码(无需重新验证):

HTML

<form method="post" id="registration-form" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">
  <input type="hidden" name="action" value="custom_registration_form">
  
  <label for="first_name"><?php _e('First Name', 'textdomain'); ?></label>
  <input type="text" name="first_name" id="first_name" required>

  <label for="last_name"><?php _e('Last Name', 'textdomain'); ?></label>
  <input type="text" name="last_name" id="last_name" required>

  <label for="title"><?php _e('Title', 'textdomain'); ?></label>
  <input type="text" name="title" id="title" required>

  <label for="organization"><?php _e('Organization', 'textdomain'); ?></label>
  <input type="text" name="organization" id="organization" required>

  <label for="email"><?php _e('Email', 'textdomain'); ?></label>
  <input type="email" name="email" id="email" required>

  <label for="password"><?php _e('Password', 'textdomain'); ?></label>
  <input type="password" name="password" id="password" required>
          
  <input type="submit" id="wp-submit" value="<?php //_e('Register', 'textdomain'); ?>">

</form>

函数.php

add_action( 'admin_post_custom_registration_form', 'custom_registration_form' );
add_action( 'admin_post_nopriv_custom_registration_form', 'custom_registration_form' );

function custom_registration_form() {
  if ( isset( $_POST['first_name'] ) && isset( $_POST['last_name'] ) && isset( $_POST['title'] ) && isset( $_POST['organization'] ) && isset( $_POST['email'] ) && isset( $_POST['password'] ) ) {

    $username = $_POST['email'];
    $password = $_POST['password'];
    $email = $_POST['email'];
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $title = $_POST['title'];
    $organization = $_POST['organization'];

    $user_id = wp_create_user( $username, $password, $email );

    if ( ! is_wp_error( $user_id ) ) {
      // Set user meta data
      update_user_meta( $user_id, 'first_name', $first_name );
      update_user_meta( $user_id, 'last_name', $last_name );
      update_user_meta( $user_id, 'mepr_title', $title );
      update_user_meta( $user_id, 'mepr_organization', $organization );

      // Log the user in
      wp_set_current_user( $user_id );
      wp_set_auth_cookie( $user_id );

      // Redirect to the homepage
      wp_redirect( home_url() );
      exit;
    }
  }
}

但是,当我尝试添加验证码时,表单没有正确提交。通过将代码更改为以下代码,我可以在不填写任何字段的情况下单击注册按钮,它只会转到:“mydomain.com/wp-admin/admin-post.php”和一个白页。

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

<form method="post" id="registration-form" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">
  <input type="hidden" name="action" value="custom_registration_form">
  
  <label for="first_name"><?php _e('First Name', 'textdomain'); ?></label>
  <input type="text" name="first_name" id="first_name" required>

  <label for="last_name"><?php _e('Last Name', 'textdomain'); ?></label>
  <input type="text" name="last_name" id="last_name" required>

  <label for="title"><?php _e('Title', 'textdomain'); ?></label>
  <input type="text" name="title" id="title" required>

  <label for="organization"><?php _e('Organization', 'textdomain'); ?></label>
  <input type="text" name="organization" id="organization" required>

  <label for="email"><?php _e('Email', 'textdomain'); ?></label>
  <input type="email" name="email" id="email" required>

  <label for="password"><?php _e('Password', 'textdomain'); ?></label>
  <input type="password" name="password" id="password" required>
    
  <!-- add the recaptcha button -->
  <button class="g-recaptcha" 
        data-sitekey="I ADD MY SITEKEY HERE" 
        data-callback='onSubmit' 
        data-action='submit'>Submit</button>          

</form>

<script>
   <!-- add the submit code -->
  function onSubmit(token) {
    document.getElementById("registration-form").submit();
  }
</script>

我做错了什么?

php html wordpress recaptcha recaptcha-v3
© www.soinside.com 2019 - 2024. All rights reserved.