ホームページ >CMS チュートリアル >&#&プレス >キャプチャをWordPress登録フォームと統合します
このチュートリアルでは、SPAM登録と戦うためにGoogleのRecaptchaをWordPress登録フォームに統合する方法を示しています。 WordPress HTTP APIを活用して、ユーザーの応答を確認します。
重要な概念:
プラグインの開発手順:
recaptchaキーを取得します:recaptchaのウェブサイト(recaptcha v2をお勧めします)にドメインを登録し、サイトキーとシークレットキーを取得します。
プラグインヘッダー:標準のプラグインヘッダーでプラグインファイル()を開始します:
recaptcha-registration.php
PHPクラス:
<code class="language-php"><?php /** * Plugin Name: reCAPTCHA Registration * Plugin URI: [Your Plugin URI] * Description: Adds reCAPTCHA to the WordPress registration form. * Version: 1.0.0 * Author: [Your Name] * Author URI: [Your Website] * License: GPL2 * License URI: https://www.gnu.org/licenses/gpl-2.0.html * Text Domain: recaptcha-registration */</code>
<code class="language-php">class reCAPTCHA_Registration { private $site_key; private $secret_key; public function __construct() { $this->site_key = '[YOUR_SITE_KEY]'; // Replace with your site key $this->secret_key = '[YOUR_SECRET_KEY]'; // Replace with your secret key add_action('register_form', array($this, 'display_recaptcha')); add_action('registration_errors', array($this, 'validate_recaptcha'), 10, 3); } public function display_recaptcha() { ?> <div class="g-recaptcha" data-sitekey="<?php echo $this->site_key; ?>" data-callback="recaptchaCallback"></div> <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response"> <?php } public function validate_recaptcha($errors, $sanitized_user_login, $user_email) { $response = isset($_POST['g-recaptcha-response']) ? $_POST['g-recaptcha-response'] : null; if (empty($response)) { $errors->add('empty_recaptcha', __('Please complete the reCAPTCHA.', 'recaptcha-registration')); } else { $verify_response = $this->verify_recaptcha($response); if (!$verify_response['success']) { $errors->add('invalid_recaptcha', __('Invalid reCAPTCHA response.', 'recaptcha-registration')); } } } private function verify_recaptcha($response) { $url = 'https://www.google.com/recaptcha/api/siteverify'; $data = array( 'secret' => $this->secret_key, 'response' => $response, 'remoteip' => $_SERVER['REMOTE_ADDR'] ); $response = wp_remote_post($url, array('body' => $data)); return json_decode(wp_remote_retrieve_body($response), true); } } new reCAPTCHA_Registration();</code>
recaptcha-registration.php
/wp-content/plugins/
以上がキャプチャをWordPress登録フォームと統合しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。