ホームページ >CMS チュートリアル >&#&プレス >キャプチャをWordPressログインフォームと統合します
このチュートリアルでは、Google RecaptchaをWordPressログインシステムに統合するWordPressプラグインの構築を示しています。 プラグインはHTTP APIを使用してRecaptChaにPOSTリクエストを送信し、ユーザーCaptCha Responseを検証します。
プラグインの開発には、Recaptchaキーのプロパティを備えたPHPクラスの作成、ログインフォームにCaptchaを追加し、応答の検証が含まれます。コードは、必要なパラメーターを使用して
にPOSTリクエストを送信する方法を示しています。 これにより、人間をボットと区別し、不正なアクセスの試みを防ぐことにより、ウェブサイトのセキュリティが強化されます。以前のチュートリアルでは、WordPress HTTP APIを調査しました。 このチュートリアルは、その上に構築され、WordPressプラグイン内でAPI消費を紹介します。 以前は、HTTP APIを使用してWhois and Social Data WordPressウィジェットを構築しました。
以下は、統合されたcaptchaを備えたWordPressログインフォームのスクリーンショットです:https://www.google.com/recaptcha/api/verify
1。プラグインヘッダー:
2。 PHPクラス:
PHPクラスを作成して、Recaptchaキーを保存します:<code class="language-php"><?php /* Plugin Name: WP Login Form with reCAPTCHA Plugin URI: https://www.sitepoint.com Description: Adds Google's reCAPTCHA to WordPress Login Version: 1.0 Author: Agbonghama Collins Author URI: http://w3guy.com License: GPL2 */</code>
3。プラグインのインスタンス化:
最後に、クラスをインスタンス化します:
<code class="language-php">class reCAPTCHA_Login_Form { private $public_key, $private_key; public function __construct() { $this->public_key = '6Le6d-USAAAAAFuYXiezgJh6rDaQFPKFEi84yfMc'; $this->private_key = '6Le6d-USAAAAAKvV-30YdZbdl4DVmg_geKyUxF6b'; add_action( 'login_form', array( $this, 'captcha_display' ) ); add_action( 'wp_authenticate_user', array( $this, 'validate_captcha_field' ), 10, 2 ); } public function captcha_display() { ?> <noscript> <iframe src="https://www.google.com/recaptcha/api/noscript?k=<?=%24this->public_key?>" height="300" width="300" frameborder="0"></iframe><br><br> <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea> <input type="hidden" name="recaptcha_response_field" value="manual_challenge"> </noscript> <?php } public function validate_captcha_field($user, $password) { if ( ! isset( $_POST['recaptcha_response_field'] ) || empty( $_POST['recaptcha_response_field'] ) ) { return new WP_Error( 'empty_captcha', 'CAPTCHA cannot be empty' ); } if( isset( $_POST['recaptcha_response_field'] ) && $this->recaptcha_response() === 'false' ) { return new WP_Error( 'invalid_captcha', 'Incorrect CAPTCHA response' ); } return $user; } public function recaptcha_response() { $challenge = isset($_POST['recaptcha_challenge_field']) ? esc_attr($_POST['recaptcha_challenge_field']) : ''; $response = isset($_POST['recaptcha_response_field']) ? esc_attr($_POST['recaptcha_response_field']) : ''; $remote_ip = $_SERVER["REMOTE_ADDR"]; $post_body = array( 'privatekey' => $this->private_key, 'remoteip' => $remote_ip, 'challenge' => $challenge, 'response' => $response ); return $this->recaptcha_post_request( $post_body ); } public function recaptcha_post_request( $post_body ) { $args = array( 'body' => $post_body ); $request = wp_remote_post( 'https://www.google.com/recaptcha/api/verify', $args ); $response_body = wp_remote_retrieve_body( $request ); $answers = explode( "\n", $response_body ); $request_status = trim( $answers[0] ); return $request_status; } } new reCAPTCHA_Login_Form();</code>
これにより、プラグインコードが完成します。 使用するために完全なプラグインをダウンロードしてください。 これは、プラグインでのWordPress HTTP API使用量を示すシリーズの一部です。
(擬似オリジナリ性のために書き直しを必要としないため、簡潔にするためにFAQSセクションが削除されました。コンテンツは事実であり、変更は必要ありません。以上がキャプチャをWordPressログインフォームと統合しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。