Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

《Wordpress》Contact Form 7でメールアドレスの再入力チェック

More than 3 years have passed since last update.

入力フォームプラグイン「Contact Form 7」で、メールの再入力チェックを行いたいときのコードです。
UI的にはイマイチですが、この実装は要件によく含まれますね。

フォームの編集

WordPressの管理画面のコンタクトフォームの編集画面から以下のコードを入力します。

[email* inquiry_mail]
[email* inquiry_mail_confirm]

確認用のコードのname属性の末尾に_confirmを記述します。

function.phpの編集

「function.php」に以下のコードを記述します。

function wpcf7_main_validation_filter( $result, $tag ) {
  $type = $tag['type'];
  $name = $tag['name'];
  $_POST[$name] = trim( strtr( (string) $_POST[$name], "\n", " " ) );
  if ( 'email' == $type || 'email*' == $type ) {
    if (preg_match('/(.*)_confirm$/', $name, $matches)){
      $target_name = $matches[1];
      if ($_POST[$name] != $_POST[$target_name]) {
        if (method_exists($result, 'invalidate')) {
          $result->invalidate( $tag,"確認用のメールアドレスが一致していません");
      } else {
          $result['valid'] = false;
          $result['reason'][$name] = '確認用のメールアドレスが一致していません';
        }
      }
    }
  }
  return $result;
}

add_filter( 'wpcf7_validate_email', 'wpcf7_main_validation_filter', 11, 2 );
add_filter( 'wpcf7_validate_email*', 'wpcf7_main_validation_filter', 11, 2 );

おわります。

uto-usui
Branding Engineer @ Condé Nast 🇯🇵 for VOUGE GQ WIRED/Songwriter/Fav. 🐈 🌸 🎸 📸 📚 🛠🍎☕️🍷👩🏻‍💻
https://uto-usui.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away