ホームページ >CMS チュートリアル >&#&プレス >キャプチャをWordPress登録フォームと統合します

キャプチャをWordPress登録フォームと統合します

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌オリジナル
2025-02-18 11:42:09270ブラウズ

このチュートリアルでは、SPAM登録と戦うためにGoogleのRecaptchaをWordPress登録フォームに統合する方法を示しています。 WordPress HTTP APIを活用して、ユーザーの応答を確認します。

重要な概念:

    recaptcha:
  • 人間とボットを区別するGoogleサービスが自動化されたスパム登録を妨げます。 wordpress http api:
  • recaptcha apiとの通信のために使用されています。
  • プラグインの開発:Recaptchaの統合を処理するためのカスタムプラグインを構築します。
  • なぜrecaptchaを使用しますか?
WordPressの人気により、多数のスパムアカウントを作成するボットの主要なターゲットになります。 Recaptchaは、この問題に対する堅牢なソリューションを提供します

プラグインの開発手順:

recaptchaキーを取得します:recaptchaのウェブサイト(recaptcha v2をお勧めします)にドメインを登録し、サイトキーとシークレットキーを取得します。

  1. プラグインヘッダー:標準のプラグインヘッダーでプラグインファイル()を開始します:

  2. recaptcha-registration.phpPHPクラス:

    recaptcha機能を管理するクラスを作成:
<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>
  1. プラグインをアクティブにします:
  2. ディレクトリにアップロードし、WordPress管理パネルでアクティブ化します。 プレースホルダーキーを実際のキーに置き換えることを忘れないでください。
<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>
    保護された登録フォームのスクリーンショット:
  1. recaptcha-registration.php/wp-content/plugins/
  2. この改善されたバージョンでは、パフォーマンスとユーザーエクスペリエンスを向上させるために、新しいRecaptcha V2と非同期負荷を使用します。 また、エラー処理と国際化も含まれます。 ブラケットのプレースホルダーを実際のRecaptchaキーに置き換えることを忘れないでください。 このコードは、元の例よりも簡潔で効率的です。

以上がキャプチャをWordPress登録フォームと統合しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。