-
Notifications
You must be signed in to change notification settings - Fork 0
Conditional Fields
Wapic Fields allows you to show or hide fields dynamically based on the value of another field.
This feature is useful for adding conditional logic in metaboxes, option pages, or term meta fields.
Conditional fields use the condition parameter to define when a field should be visible. The field will only appear when the controlling field matches the specified value.
In this example, the URL field will only appear if the Toggle field is set to yes.
<?php
// 1. Toggle field (controlling field)
Wapic_Fields\Field::add_control(
array(
'id' => '_sample_toggle_conditional',
'type' => 'toggle',
'label' => esc_html__( 'Enable Conditional Fields', 'wapic-field' ),
'value' => get_option( '_sample_toggle_conditional' ),
)
);
// 2. URL field (conditional field)
// This field only appears when the toggle is set to 'yes'
Wapic_Fields\Field::add_control(
array(
'id' => '_sample_url_conditional',
'type' => 'url',
'label' => esc_html__( 'Conditional URL', 'wapic-field' ),
'description' => esc_html__( 'This field appears only when the toggle is enabled.', 'wapic-field' ),
'value' => get_option( '_sample_url_conditional' ),
'condition' => array(
'field' => '_sample_toggle_conditional', // The controlling field ID
'value' => 'yes', // Show when toggle = 'yes'
),
)
);The condition parameter accepts an array with the following keys:
| Parameter | Type | Description | Required |
|---|---|---|---|
field |
string | The ID of the controlling field | Yes |
value |
mixed | The value that triggers visibility | Yes |
Show a text field only when a specific option is selected:
<?php
// Select field
Wapic_Fields\Field::add_control(
array(
'id' => '_payment_method',
'type' => 'select',
'label' => esc_html__( 'Payment Method', 'wapic-field' ),
'options' => array(
'credit_card' => 'Credit Card',
'paypal' => 'PayPal',
'bank' => 'Bank Transfer',
),
'value' => get_option( '_payment_method' ),
)
);
// Conditional field - only shows when 'bank' is selected
Wapic_Fields\Field::add_control(
array(
'id' => '_bank_account',
'type' => 'text',
'label' => esc_html__( 'Bank Account Number', 'wapic-field' ),
'value' => get_option( '_bank_account' ),
'condition' => array(
'field' => '_payment_method',
'value' => 'bank',
),
)
);Show different fields based on radio button selection:
<?php
// Radio field
Wapic_Fields\Field::add_control(
array(
'id' => '_display_mode',
'type' => 'radio',
'label' => esc_html__( 'Display Mode', 'wapic-field' ),
'options' => array(
'image' => 'Image',
'video' => 'Video',
),
'value' => get_option( '_display_mode' ),
)
);
// Show image field when 'image' is selected
Wapic_Fields\Field::add_control(
array(
'id' => '_featured_image',
'type' => 'image',
'label' => esc_html__( 'Featured Image', 'wapic-field' ),
'value' => get_option( '_featured_image' ),
'condition' => array(
'field' => '_display_mode',
'value' => 'image',
),
)
);
// Show URL field when 'video' is selected
Wapic_Fields\Field::add_control(
array(
'id' => '_video_url',
'type' => 'url',
'label' => esc_html__( 'Video URL', 'wapic-field' ),
'value' => get_option( '_video_url' ),
'condition' => array(
'field' => '_display_mode',
'value' => 'video',
),
)
);Show a field when a checkbox is checked:
<?php
// Checkbox field
Wapic_Fields\Field::add_control(
array(
'id' => '_enable_custom_css',
'type' => 'checkbox',
'label' => esc_html__( 'Enable Custom CSS', 'wapic-field' ),
'options' => array(
'yes' => 'Enable',
),
'value' => get_option( '_enable_custom_css' ),
)
);
// Conditional textarea - only shows when checkbox is checked
Wapic_Fields\Field::add_control(
array(
'id' => '_custom_css',
'type' => 'textarea',
'label' => esc_html__( 'Custom CSS', 'wapic-field' ),
'value' => get_option( '_custom_css' ),
'condition' => array(
'field' => '_enable_custom_css',
'value' => 'yes',
),
)
);You can have multiple fields that depend on the same controlling field:
<?php
// Controlling field
Wapic_Fields\Field::add_control(
array(
'id' => '_enable_features',
'type' => 'toggle',
'label' => esc_html__( 'Enable Advanced Features', 'wapic-field' ),
'value' => get_option( '_enable_features' ),
)
);
// First conditional field
Wapic_Fields\Field::add_control(
array(
'id' => '_feature_color',
'type' => 'color',
'label' => esc_html__( 'Feature Color', 'wapic-field' ),
'value' => get_option( '_feature_color' ),
'condition' => array(
'field' => '_enable_features',
'value' => 'yes',
),
)
);
// Second conditional field
Wapic_Fields\Field::add_control(
array(
'id' => '_feature_text',
'type' => 'text',
'label' => esc_html__( 'Feature Text', 'wapic-field' ),
'value' => get_option( '_feature_text' ),
'condition' => array(
'field' => '_enable_features',
'value' => 'yes',
),
)
);Conditional fields work the same way in metaboxes:
<?php
// In your metabox render method
Wapic_Fields\Field::add_control(
array(
'id' => '_show_author',
'type' => 'toggle',
'label' => esc_html__( 'Show Author Bio', 'wapic-field' ),
'value' => get_post_meta( $post->ID, '_show_author', true ),
)
);
Wapic_Fields\Field::add_control(
array(
'id' => '_author_bio',
'type' => 'wp_editor',
'label' => esc_html__( 'Author Bio', 'wapic-field' ),
'value' => get_post_meta( $post->ID, '_author_bio', true ),
'condition' => array(
'field' => '_show_author',
'value' => 'yes',
),
)
);Conditional fields also work in taxonomy term meta:
<?php
// In your render_fields method
Wapic_Fields\Field::add_control(
array(
'id' => '_has_icon',
'type' => 'toggle',
'label' => esc_html__( 'Has Custom Icon', 'wapic-field' ),
'value' => $term_id ? get_term_meta( $term_id, '_has_icon', true ) : '',
)
);
Wapic_Fields\Field::add_control(
array(
'id' => '_icon_image',
'type' => 'image',
'label' => esc_html__( 'Icon Image', 'wapic-field' ),
'value' => $term_id ? get_term_meta( $term_id, '_icon_image', true ) : '',
'condition' => array(
'field' => '_has_icon',
'value' => 'yes',
),
)
);- Client-Side Only: Conditional logic is handled via JavaScript on the client side
- Field IDs: The controlling field must be defined before the conditional field
- Validation: Hidden fields are automatically excluded from validation
- Saving: Hidden field values are still saved if they have data
- Learn about Field Types
- Learn about Using Tabs
- Learn about Use in Metabox
- Learn about Use in Options