0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MW WP form に スパムメールがたくさん来るので追記したコード

Last updated at Posted at 2024-11-29

MW WP form に スパムメールがたくさん来るので追記したコードです。
「https://」「http://」を禁止文字に設定しました。
よかったらいいねください🙇‍♂️🙇‍♂️🙇‍♂️

<追記>
functions.php に追記するだけです。
ショートコードの mw-wp-form-〇〇 の部分が 今は、「47」です。
お気をつけください。

PHP themes/functions.php
/**
 * 禁止文字バリデーションルールを設定
 */
function coop_applicate_validation_rule($Validation, $data) {
    $Validation->set_rule('textarea', 'prohibited_words'); // チェック対象フィールド名を変更
    return $Validation;
}
// ここでフォームIDを変更してください(例: mw-wp-form-47)
add_filter('mwform_validation_mw-wp-form-47', 'coop_applicate_validation_rule', 10, 2);

/**
 * 禁止文字バリデーションルールを登録
 */
function my_validation_rule( $validation_rules, $key ) {
    // 禁止文字チェックルールを追加
    $validation_rules['prohibited_words'] = new MW_WP_Form_Validation_Rule_Prohibited_Words($key);
    return $validation_rules;
}
add_filter('mwform_validation_rules', 'my_validation_rule', 10, 2);

/**
 * 禁止文字チェックのバリデーションルールクラス
 */
class MW_WP_Form_Validation_Rule_Prohibited_Words extends MW_WP_Form_Abstract_Validation_Rule
{
    protected $name = 'prohibited_words';
 
    public function rule( $key, array $options = []) {
        $value = $this->Data->get( $key );
        if ( !MWF_Functions::is_empty( $value ) ) {
            // 禁止文字のリスト
            $prohibited_words = ['https://', 'http://']; // ここに禁止したい文字列を追加

            // 禁止文字が含まれているかをチェック
            foreach ($prohibited_words as $word) {
                if (strpos($value, $word) !== false) {
					$message_str = "お問い合わせが混み合っているため送信できません。しばらくたってからお願いします。";
                    return $message_str; // エラーメッセージを返す
                }
            }
        }
        return null; // 問題がなければ null を返す
    }

    public function admin( $key, $value ) {}
}


0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?