Contact Form 7 バージョン 5.8.6でカスタム化した場所へエラーメッセージを表示したい

実現したいこと

Contact Form 7 バージョン 5.8.6でエラーメッセージの位置をカスタム化した場所に表示したい

発生している問題・分からないこと

Contact Form 7 バージョン 5.5.6.1で以下のコードをfunctions.phpに書いて、エラーメッセージをカスタム化した場所に表示するようにしていましたが、最新版のバージョン 5.8.6では機能せずどう修正したらいいのか分からず困っています。

生年月日の入力フィールドの初期状態
生年月日の入力フィールドの初期状態

生年月日の各SELECTを1セットにして、未入力があった場合生年月日1セットの下に1つだけエラーメッセージを出す。
Contact Form 7 バージョン 5.5.6.1での状態
エラーを1つにして、カスタムな位置に出力した状態

最新版のバージョン 5.8.6での状態
カスタムポジションが機能してない状態

該当のソースコード

HTML

1<tr> 2<th class="hissu">生年月日</th> 3<td><p>[select* birth-year class:text-short4 first_as_label "---" "1950" "1951" "1952" "1953" "1954" "1955" "1956" "1957" "1958" "1959" "1960" "1961" "1962" "1963" "1964" "1965" "1966" "1967" "1968" "1969" "1970" "1971" "1972" "1973" "1974" "1975" "1976" "1977" "1978" "1979" "1980" "1981" "1982" "1983" "1984" "1985" "1986" "1987" "1988" "1989" "1990" "1991" "1992" "1993" "1994" "1995" "1996" "1997" "1998" "1999" "2000" "2001" "2002" "2003" "2004" "2005"]年[select* birth-month first_as_label "---" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12"]月[select* birth-date first_as_label "---" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31"]日</p><span class="wpcf7-custom-item-error birth-year birth-month birth-date"><span class="wpcf7-form-control"></span></span></td> 4</tr>

PHP

1// エラーの出力位置をカスタマイズする2/* 参考リンク: https://do-jo.jp/plugins/1307/ 3 * https://qiita.com/kinshist/items/7a16a1fe4bae27fe11b2 4 * https://nldot.info/i-want-to-change-the-validation-message-display-position-in-copipable-contact-form-7/ 5 */6function wpcf7_custom_item_error_position( $items, $result ) {7 $class = 'wpcf7-custom-item-error';8 $names = array( 'birth-year', 'birth-month', 'birth-date'); // name属性を指定9 $processed_day_messeage = false;10 11 if ( isset( $items['invalid_fields'] ) ) {12 foreach ( $items['invalid_fields'] as $k => $v ) {13 $orig = $v['into'];14 $name = substr($orig, 0, strcspn($orig,':')); //Conditional Fields for Contact Form 7 バージョン 2.1.6では、invalid_fieldsの後ろに:のあとにConditional Fields for Contact Form 7用の情報が入るので取り除く15 $name = substr( $name, strrpos($name, ".") + 1 );16 if ( in_array( $name, $names ) ) {17 if ($name == 'birth-year' || $name == 'birth-month' || $name == 'birth-date') { //生年月日の内1つでも未入力があった場合、フラグをONにして、エラーを1つだけにするため残りのエラーメッセージを除去する18 if ($processed_day_messeage == true) {19 unset($items['invalid_fields'][$k]);20 continue;21 } else {22 $processed_day_messeage = true;23 }24 }25 $items['invalid_fields'][$k]['into'] = ".{$class}.{$name}"; //エラーメッセージをカスタムポジションに表示するように設定26 }27 }28 $items['invalid_fields'] = array_values($items['invalid_fields']);29 }30 return $items;31}32add_filter( 'wpcf7_feedback_response', 'wpcf7_custom_item_error_position', 10, 2 );33

試したこと・調べたこと

上記の詳細・結果

デバッグログを吐かせて、invalid_fieldsの中身を確認した所、最新版ではintoの項目が別の場所に移動しており、しかも、内容がエラーが起こったフォーム名になっているようでした。
試しに、".{$class}.{$name}"を入れてみましたが、当然のごとく、うまくいきませんでした。
最新版では各項目の項目名がクラスからNAMEに変更になったということで、
wpcf7-custom-item-errorのクラス名に指定しているのを、NAMEに指定したりしてみたのですがダメでした。

補足

このフックはRestAPIへの応答のカスタマイズ用ということまでは調査できたのですが、当方RestAPIの知識がなく、どうすればいいのか分からず困っています。

諸先輩方で、最新版のバージョン 5.8.6でエラーメッセージをカスタムポジションに出力させることに成功された方、その方法をご教授いただければ幸いです。

コメントを投稿

0 コメント