AI要約
説明
wpforms_process_validate_textarea アクションは、フォーム送信時に段落テキストフォームフィールドの検証を実行します。
パラメーター
- $field_id
- (int) フィールドID。
- $field_submit
- (array) フィールドに送信された元の未加工/未サニタイズのフィールド値。
- $form_data
- (配列) 後で使用するために処理および準備されたフォーム設定/データ。
ソース
wpforms/includes/fields/class-base.php
詳細情報
wpforms_process_validate_textarea アクションは、段落テキストフォームフィールドの配列に適用されます。この関数は、すべてのフォームフィールド do_action( wpforms_process_validate_{$field_type}, $field_id, $field_submit, $form_data ) で使用できます。
たとえば、これをメールフィールドに使用するには、do_action( wpforms_process_validate_email, $field_id, $field_submit, $form_data ) を使用します。
フィールド値は、後で wpforms_process_format_{$field_type} でサニタイズされることに注意することが重要です。the
例
このアクションを使用して、不適切な言葉遣いなどの、フォームの送信をブロックしたい単語のリストを段落テキストでスキャンしたり、フォームの非表示フィールドに値を設定するなどの別のトリガーとなる特定の単語を探すためにこのアクションを使用したりできます。これは、エントリからフィルタリングできます。
この例では、このフィールド内の不適切な言葉遣いを検索します。不適切な言葉遣いがある場合、フォームはエラーを表示し、送信されません。
/*
* Check the paragraph text field for profanity.
*
* @link https://wpforms.com/developers/wpforms_process_validate_textarea/
*
* @param int $field_id Field ID.
* @param array $field_submit Unsanitized field value submitted for the field.
* @param array $form_data Form data and settings.
*/
// Optional, you can limit to specific forms. Below, we restrict output to
// form ID #1423.
if ( absint( $form_data[ 'id' ] ) !== 1423 ) {
return;
}
function wpf_dev_profanity_filter_paragraph( $field_id, $field_submit, $form_data ) {
//Create your list of profanity words separated by commas
$blocked_words = array(
'badword1',
'badword2'
);
foreach( $blocked_words as $word ) {
if(strpos($field_submit, $word) !== FALSE ) {
wpforms()->process->errors[ $form_data[ 'id' ] ][ $field_id ] = esc_html__( 'No profanity allowed.', 'wpforms' );
return;
}
}
}
add_action( 'wpforms_process_validate_textarea', 'wpf_dev_profanity_filter_paragraph', 10, 3 );
関連
関連記事: