Im Laufe der Jahre ist WordPress ein Ziel für Spammer geworden
Leider existiert automatisierte Software, deren Ziel es ist, das Web auf der Suche nach Websites zu kriechen, die mit einer beliebten Plattform wie WordPress erstellt wurden, und Hunderte, sogar Tausende von Spam -Kommentaren einzureichen. Spam -Kommentare sind sehr nervig, sie verbrauchen unsere kostbare Zeit, wenn es darum geht, sie zu moderieren und zu löschen.Ich weiß, dass Sie Spam -Kommentare genauso hassen wie ich und würde gerne wissen, wie man sie bekämpft. Eine Möglichkeit, Bots vom Senden von Spam -Kommentaren abzuschrecken, besteht darin, eine Captcha in das Kommentarformular zu integrieren.
In früheren Tutorials haben wir gelernt, wie man Captchas in das WordPress -Anmelde- und Registrierungsformular integriert.
In ähnlicher Weise werden wir nun durchlaufen, wie ein Captcha in das WordPress -Kommentarsystem integriert wird.
Es gibt viele Captcha-Plugins im WordPress-Plugin-Verzeichnis wie WP-Recaptcha und Securimage-wp-fixed.
Ziel dieses Tutorials ist es, kein weiteres Captcha -Plugin zu erstellen, sondern:
Plugin -Entwicklung
Fügen Sie den Plugin -Header hinzu.
<span><span><?php </span></span><span> </span><span><span>/* </span></span><span><span>Plugin Name: Add reCAPTCHA to comment form </span></span><span><span>Plugin URI: https://www.sitepoint.com </span></span><span><span>Description: Add Google's reCAPTCHA to WordPress comment form </span></span><span><span>Version: 1.0 </span></span><span><span>Author: Agbonghama Collins </span></span><span><span>Author URI: http://w3guy.com </span></span><span><span>License: GPL2 </span></span><span><span>*/</span></span>
<span>class Captcha_Comment_Form { </span> <span>/** <span>@type string private key|public key */</span> </span> <span>private $public_key, $private_key; </span> <span>/** <span>@type string captcha errors */</span> </span> <span>private static $captcha_error;</span>
<span>/** class constructor */ </span> <span>public function __construct() { </span> <span>$this->public_key = '6Le6d-USAAAAAFuYXiezgJh6rDaQFPKFEi84yfMc'; </span> <span>$this->private_key = '6Le6d-USAAAAAKvV-30YdZbdl4DVmg_geKyUxF6b'; </span> <span>// adds the captcha to the WordPress form </span> <span>add_action( 'comment_form', array( $this, 'captcha_display' ) ); </span> <span>// delete comment that fail the captcha challenge </span> <span>add_action( 'wp_head', array( $this, 'delete_failed_captcha_comment' ) ); </span> <span>// authenticate the captcha answer </span> <span>add_filter( 'preprocess_comment', array( $this, 'validate_captcha_field' ) ); </span> <span>// redirect location for comment </span> <span>add_filter( 'comment_post_redirect', array( $this, 'redirect_fail_captcha_comment' ), 10, 2 ); </span> <span>}</span>
Die Methode captcha_display (), die die Recaptcha -Herausforderung ausgibt, wird dem Kommentarformular durch die Aktion comment_form hinzugefügt.
Die Aktion wp_head enthält die Rückruffunktion delete_failed_captcha_comment (), mit der alle Kommentare gelöscht werden, die über die Captcha -Herausforderung versagen.
Die Filterpräfikation_Commention ruft die Methode validate_captcha_field () auf, um sicherzustellen, dass das Feld captcha nicht leer bleibt und dass die Antwort korrekt ist.
Der Filter comment_post_redirect rufen Sie redirect_fail_captcha_comment () zum Hinzufügen von Abfrageparametern zur Umleitung von Kommentaren.
Hier ist der Code für captcha_display (), der die Captcha -Herausforderung ausgibt.
Zusätzlich prüft es, ob an der aktuellen Seiten -URL eine Abfragezeichenfolge angehängt ist, und zeigt die entsprechende Fehlermeldung an, abhängig vom Wert von $ _get ['captcha'] durch reverect_fail_captcha_comment ()
festgelegt.<span><span><?php </span></span><span> </span><span><span>/* </span></span><span><span>Plugin Name: Add reCAPTCHA to comment form </span></span><span><span>Plugin URI: https://www.sitepoint.com </span></span><span><span>Description: Add Google's reCAPTCHA to WordPress comment form </span></span><span><span>Version: 1.0 </span></span><span><span>Author: Agbonghama Collins </span></span><span><span>Author URI: http://w3guy.com </span></span><span><span>License: GPL2 </span></span><span><span>*/</span></span>
<span>class Captcha_Comment_Form { </span> <span>/** <span>@type string private key|public key */</span> </span> <span>private $public_key, $private_key; </span> <span>/** <span>@type string captcha errors */</span> </span> <span>private static $captcha_error;</span>
Die Methode validate_captcha_field (), wie der Name impliziert, die Captcha -Antwort zu validieren, indem sichergestellt wird, dass das Feld Captcha nicht leer bleibt und die gelieferte Antwort korrekt ist.
<span>/** class constructor */ </span> <span>public function __construct() { </span> <span>$this->public_key = '6Le6d-USAAAAAFuYXiezgJh6rDaQFPKFEi84yfMc'; </span> <span>$this->private_key = '6Le6d-USAAAAAKvV-30YdZbdl4DVmg_geKyUxF6b'; </span> <span>// adds the captcha to the WordPress form </span> <span>add_action( 'comment_form', array( $this, 'captcha_display' ) ); </span> <span>// delete comment that fail the captcha challenge </span> <span>add_action( 'wp_head', array( $this, 'delete_failed_captcha_comment' ) ); </span> <span>// authenticate the captcha answer </span> <span>add_filter( 'preprocess_comment', array( $this, 'validate_captcha_field' ) ); </span> <span>// redirect location for comment </span> <span>add_filter( 'comment_post_redirect', array( $this, 'redirect_fail_captcha_comment' ), 10, 2 ); </span> <span>}</span>
Schauen wir uns valate_captcha_field () genauer an, insbesondere die sonst -bedingte Anweisung, ein Aufruf an Recaptcha_Response (), um zu überprüfen, ob die Captcha -Antwort korrekt ist.
unten finden Sie den Code für die Recaptcha_Response ().
/** Output the reCAPTCHA form field. */ public function captcha_display() { if ( isset( $_GET['captcha'] ) && $_GET['captcha'] == 'empty' ) { echo '<span><span><span><strong</span>></span>ERROR<span><span></strong</span>></span>: CAPTCHA should not be empty'; </span> } elseif ( isset( $_GET['captcha'] ) && $_GET['captcha'] == 'failed' ) { echo '<span><span><span><strong</span>></span>ERROR<span><span></strong</span>></span>: CAPTCHA response was incorrect'; </span> } echo <<<span><span><span><CAPTCHA_FORM</span> </span></span><span> <span><style type<span>='text/css'</span>></span><span><span><span>#submit</span> { </span></span></span><span><span> <span>display: none; </span></span></span><span><span> <span>}</span></span><span><span></style</span>></span> </span> <span><span><span><script</span> type<span>="text/javascript"</span> </span></span><span> <span>src<span>="http://www.google.com/recaptcha/api/challenge?k=<span><?= $this->public_key; ?></span>"</span>></span><span> </span></span><span><span> </span><span><span></script</span>></span> </span> <span><span><span><noscript</span>></span> </span> <span><span><span><iframe</span> src<span>="http://www.google.com/recaptcha/api/noscript?k=<span><?= $this->public_key; ?></span>"</span> </span></span><span> <span>height<span>="300"</span> width<span>="300"</span> frameborder<span>="0"</span>></span><span><span></iframe</span>></span> </span> <span><span><span><br</span>></span> </span> <span><span><span><textarea</span> name<span>="recaptcha_challenge_field"</span> rows<span>="3"</span> cols<span>="40"</span>></span> </span> <span><span><span></textarea</span>></span> </span> <span><span><span><input</span> type<span>="hidden"</span> name<span>="recaptcha_response_field"</span> </span></span><span> <span>value<span>="manual_challenge"</span>></span> </span> <span><span><span></noscript</span>></span> </span> <span><span><span><input</span> name<span>="submit"</span> type<span>="submit"</span> id<span>="submit-alt"</span> tabindex<span>="6"</span> value<span>="Post Comment"</span>/></span> </span>CAPTCHA_FORM; }
Erlauben Sie mir zu erklären, wie die recaptcha_response () funktioniert.
Eine Postanforderung wird an den Endpunkt http://www.google.com/recaptcha/api/verify mit den folgenden Parametern gesendet.
Die vom Formular gesendete Herausforderung und Antwortdaten werden auf $ Challenge bzw. $ response gespeichert. $ _Server ["remote_addr"] Erfassen Sie die IP -Adresse des Benutzers und es an $ remote_ip.
<span>/** </span><span> * Add query string to the comment redirect location </span><span> * </span><span> * <span>@param $location string location to redirect to after comment </span></span><span> * <span>@param $comment object comment object </span></span><span> * </span><span> * <span>@return <span>string</span> </span></span><span> */ </span> <span>function redirect_fail_captcha_comment( $location, $comment ) { </span> <span>if ( ! empty( <span>self::</span>$captcha_error ) ) { </span> <span>$args = array( 'comment-id' => $comment->comment_ID ); </span> <span>if ( <span>self::</span>$captcha_error == 'captcha_empty' ) { </span> <span>$args['captcha'] = 'empty'; </span> <span>} elseif ( <span>self::</span>$captcha_error == 'challenge_failed' ) { </span> <span>$args['captcha'] = 'failed'; </span> <span>} </span> <span>$location = add_query_arg( $args, $location ); </span> <span>} </span> <span>return $location; </span> <span>}</span>
<span>/** </span><span> * Verify the captcha answer </span><span> * </span><span> * <span>@param $commentdata object comment object </span></span><span> * </span><span> * <span>@return <span>object</span> </span></span><span> */ </span> <span>public function validate_captcha_field( $commentdata ) { </span> <span>// if captcha is left empty, set the self::$captcha_error property to indicate so. </span> <span>if ( empty( $_POST['recaptcha_response_field'] ) ) { </span> <span><span>self::</span>$captcha_error = 'captcha_empty'; </span> <span>} </span> <span>// if captcha verification fail, set self::$captcha_error to indicate so </span> <span>elseif ( $this->recaptcha_response() == 'false' ) { </span> <span><span>self::</span>$captcha_error = 'challenge_failed'; </span> <span>} </span> <span>return $commentdata; </span> <span>}</span>
Jeder Kommentar, der von einem Benutzer abgegeben wurde, der die Captcha -Herausforderung nicht bestanden hat oder das Feld leer gelassen hat
<span>/** </span><span> * Get the reCAPTCHA API response. </span><span> * </span><span> * <span>@return <span>string</span> </span></span><span> */ </span> <span>public function recaptcha_response() { </span> <span>// reCAPTCHA challenge post data </span> <span>$challenge = isset( $_POST['recaptcha_challenge_field'] ) ? esc_attr( $_POST['recaptcha_challenge_field'] ) : ''; </span> <span>// reCAPTCHA response post data </span> <span>$response = isset( $_POST['recaptcha_response_field'] ) ? esc_attr( $_POST['recaptcha_response_field'] ) : ''; </span> <span>$remote_ip = $_SERVER["REMOTE_ADDR"]; </span> <span>$post_body = array( </span> <span>'privatekey' => $this->private_key, </span> <span>'remoteip' => $remote_ip, </span> <span>'challenge' => $challenge, </span> <span>'response' => $response </span> <span>); </span> <span>return $this->recaptcha_post_request( $post_body ); </span> <span>}</span>
Schließlich schließen wir die Plugin -Klasse.
<span>$post_body = array( </span> <span>'privatekey' => $this->private_key, </span> <span>'remoteip' => $remote_ip, </span> <span>'challenge' => $challenge, </span> <span>'response' => $response </span> <span>); </span> <span>return $this->recaptcha_post_request( $post_body );</span>
Wir sind fertig mit der Codierung der Pluginklasse. Um die Klasse auf die Arbeit zu bringen, müssen wir sie so instanziieren:
<span>/** </span><span> * Send HTTP POST request and return the response. </span><span> * </span><span> * <span>@param $post_body array HTTP POST body </span></span><span> * </span><span> * <span>@return <span>bool</span> </span></span><span> */ </span> <span>public function recaptcha_post_request( $post_body ) { </span> <span>$args = array( 'body' => $post_body ); </span> <span>// make a POST request to the Google reCaptcha Server </span> <span>$request = wp_remote_post( 'https://www.google.com/recaptcha/api/verify', $args ); </span> <span>// get the request response body </span> <span>$response_body = wp_remote_retrieve_body( $request ); </span> <span>/** </span><span> * explode the response body and use the request_status </span><span> * <span>@see https://developers.google.com/recaptcha/docs/verify </span></span><span> */ </span> <span>$answers = explode( "\n", $response_body ); </span> <span>$request_status = trim( $answers[0] ); </span> <span>return $request_status; </span> <span>}</span>
Bei Aktivierung des Plugins wird ein Captcha zum WordPress -Kommentarformular hinzugefügt, wie unten angezeigt.
Am Ende dieses Tutorials sollten Sie dem Kommentarformular zusätzliche Formularfelder hinzufügen und nahezu jede Funktion implementieren, die Sie im Kommentarsystem dank der genannten Filter und Aktionen erhalten möchten.
Wenn Sie das Plugin auf Ihrer WordPress-Site verwenden oder den Code ausführlich untersuchen möchten, laden Sie das Plugin von Github herunter.
bis ich wieder auf dich zukomme, glücklich codieren!
Wie oft sollte ich mein Captcha -Plugin aktualisieren? Dies stellt sicher, dass Sie über die neuesten Sicherheitsfunktionen verfügen und das Plugin mit der neuesten Version von WordPress kompatibel bleibt.
Das obige ist der detaillierte Inhalt vonIntegration eines Captcha in die WordPress -Kommentarform. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!