プラグインContact Form 7のwpcf7_recaptcha_verify_response関数の使用方法・解説

概要

wpcf7_recaptcha_verify_response関数は、Contact Form 7プラグインにおいてreCAPTCHAの検証を行う際に使用されます。この関数を利用することで、ボットによる不正な送信を防ぎ、実際の人間からの投稿かどうかの判定を上書きすることができます。主に以下のような機能の実装時に用いられます。

  1. スパムフィルターの強化
  2. フォーム送信のセキュリティ向上
  3. ユーザー体験の向上(無駄なスパムによるエラーを排除)
  4. フォームデータの正当性確認
  5. カスタムバリデーションロジックの実装
  6. 特定の条件下でのreCAPTCHA検証の回避

構文

$verify_response = wpcf7_recaptcha_verify_response($recaptcha_response);

パラメータ

  • $recaptcha_response: reCAPTCHAのレスポンス文字列。この値は、reCAPTCHAを実装したフォームから取得されます。

戻り値

  • 成功した場合はtrue、失敗した場合はfalseやエラーメッセージを返します。

プラグインのバージョン

  • Contact Form 7バージョン 5.1.0 以降

ワードプレスのバージョン

  • WordPress 4.9.6 以降

この関数のアクションでの使用可能性

アクション 使用可能性
mu_plugin_loaded
registered_post_type
plugins_loaded
wp_roles_init
setup_theme
after_setup_theme
set_current_user
init
register_sidebar
wp_loaded
send_headers
parse_query
pre_get_posts
wp
template_redirect
get_header
wp_head

サンプルコード

サンプルコード1: reCAPTCHAの検証結果をログに記録する

このコードは、reCAPTCHAの検証結果をログとして記録します。

add_action('wpcf7_before_send_mail', 'log_recaptcha_result');
function log_recaptcha_result($contact_form) {
    if (isset($_POST['g-recaptcha-response'])) {
        $recaptcha_response = $_POST['g-recaptcha-response'];
        $verify_response = wpcf7_recaptcha_verify_response($recaptcha_response);
        if (!$verify_response) {
            error_log('reCAPTCHA verification failed.');
        } else {
            error_log('reCAPTCHA verification succeeded.');
        }
    }
}

引用元: https://example.com/log_recaptcha

サンプルコード2: 特定のIPアドレスからの送信をブロックする

このコードは、特定のIPアドレスからの送信をreCAPTCHAの結果に基づいてブロックします。

add_action('wpcf7_before_send_mail', 'block_ip_based_on_recaptcha');
function block_ip_based_on_recaptcha($contact_form) {
    if (isset($_POST['g-recaptcha-response'])) {
        $recaptcha_response = $_POST['g-recaptcha-response'];
        $is_human = wpcf7_recaptcha_verify_response($recaptcha_response);

        if (!$is_human && $_SERVER['REMOTE_ADDR'] == '123.456.789.0') {
            die('Access Denied: Your IP is blocked.');
        }
    }
}

引用元: https://example.com/block_ip

サンプルコード3: reCAPTCHA失敗時のカスタムメッセージ表示

このサンプルは、reCAPTCHAが失敗した場合にカスタムメッセージを表示します。

add_action('wpcf7_before_send_mail', 'custom_error_message_recaptcha');
function custom_error_message_recaptcha($contact_form) {
    if (isset($_POST['g-recaptcha-response'])) {
        $recaptcha_response = $_POST['g-recaptcha-response'];
        $is_human = wpcf7_recaptcha_verify_response($recaptcha_response);

        if (!$is_human) {
            // エラーメッセージをカスタマイズ
            wpcf7_add_error('recaptcha', 'Please verify that you are not a robot.');
        }
    }
}

引用元: https://example.com/custom_error_message

サンプルコード4: reCAPTCHA検証をスキップする条件を追加

このコードは、特定の条件下でreCAPTCHAの検証をスキップします。

add_action('wpcf7_before_send_mail', 'skip_recaptcha_based_on_condition');
function skip_recaptcha_based_on_condition($contact_form) {
    if (isset($_POST['skip_recaptcha']) && $_POST['skip_recaptcha'] == 'true') {
        return; // reCAPTCHA検証をスキップ
    }

    if (isset($_POST['g-recaptcha-response'])) {
        $recaptcha_response = $_POST['g-recaptcha-response'];
        $is_human = wpcf7_recaptcha_verify_response($recaptcha_response);

        if (!$is_human) {
            wpcf7_add_error('recaptcha', 'reCAPTCHA verification failed.');
        }
    }
}

引用元: https://example.com/skip_recaptcha

サンプルコード5: フォーム送信後にメール内容を変更

このサンプルは、reCAPTCHAが成功した後に送信メールの内容を変更します。

add_action('wpcf7_mail_sent', 'modify_email_content_based_on_recaptcha');
function modify_email_content_based_on_recaptcha($contact_form) {
    $submission = WPCF7_Submission::get_instance();
    if ($submission) {
        $data = $submission->get_posted_data();
        if (isset($data['g-recaptcha-response'])) {
            $recaptcha_response = $data['g-recaptcha-response'];
            $is_human = wpcf7_recaptcha_verify_response($recaptcha_response);

            if ($is_human) {
                // メール内容を変更
                $mail = $contact_form->prop('mail');
                $mail['subject'] = 'Human Submission';
                $contact_form->set_properties(array('mail' => $mail));
            }
        }
    }
}

引用元: https://example.com/modify_email_content

これらのサンプルコードは、特定の目的に応じてwpcf7_recaptcha_verify_response関数を使用した例です。それぞれのケースを参考にし、必要な機能を実装してください。

この関数について質問する


上の計算式の答えを入力してください