概要
wpforms_field_properties フィルタは、WPForms プラグインにおいてフォームフィールドのプロパティを変更するために使用されるフックです。このフィルタを使うことで、フォームのフィールドに対するさまざまな変更が可能になります。例えば、フィールドのラベル、クラス、必須設定、プレースホルダーなどをカスタマイズすることができます。
よく使われる機能
- フィールドラベルの変更
- フィールドの必須設定を変更
- プレースホルダーの追加または変更
- CSS クラスの追加
- フィールドの説明テキストの変更
- 特定条件に基づくフィールドの表示制御
フィルタの構文
apply_filters( 'wpforms_field_properties', $properties, $field, $form_data );
パラメータ
$properties: フィールドのプロパティを含む配列$field: 対象のフィールドのデータ$form_data: フォーム全体のデータ
戻り値
- 修正されたフィールドプロパティの配列
使用可能な WPForms バージョン
- WPForms バージョン 1.0 以上
使用可能な WordPress バージョン
- WordPress バージョン 4.9 以上
この関数のアクションでの使用可能性
| アクション | 使用可能性 |
|---|---|
| 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: フィールドラベルの変更
このコードは、特定のフィールドのラベルをカスタマイズします。
add_filter( 'wpforms_field_properties', function( $properties, $field, $form_data ) {
if ( 'your_field_key' === $field['key'] ) {
$properties['label'] = '新しいラベル名';
}
return $properties;
}, 10, 3 );
引用元: https://wpforms.com/docs/
サンプル 2: プレースホルダーの追加
このサンプルは、入力フィールドにプレースホルダーを設定します。
add_filter( 'wpforms_field_properties', function( $properties, $field, $form_data ) {
if ( 'your_field_key' === $field['key'] ) {
$properties['placeholder'] = 'ここに入力してください';
}
return $properties;
}, 10, 3 );
引用元: https://wpforms.com/docs/
サンプル 3: 必須フィールドの設定
このコードは、特定のフィールドを必須に設定します。
add_filter( 'wpforms_field_properties', function( $properties, $field, $form_data ) {
if ( 'your_field_key' === $field['key'] ) {
$properties['required'] = true;
}
return $properties;
}, 10, 3 );
引用元: https://wpforms.com/docs/
サンプル 4: CSS クラスの追加
このサンプルは、フィールドに特定の CSS クラスを追加します。
add_filter( 'wpforms_field_properties', function( $properties, $field, $form_data ) {
if ( 'your_field_key' === $field['key'] ) {
$properties['class'] .= ' custom-class';
}
return $properties;
}, 10, 3 );
引用元: https://wpforms.com/docs/
サンプル 5: 説明テキストの変更
このコードは、フィールドの説明テキストを変更します。
add_filter( 'wpforms_field_properties', function( $properties, $field, $form_data ) {
if ( 'your_field_key' === $field['key'] ) {
$properties['description'] = 'このフィールドについての説明';
}
return $properties;
}, 10, 3 );
引用元: https://wpforms.com/docs/