This feature is available in Pods 3.0+
The newest feature that we’ve added to Pods with the release of Pods 3.0 is Conditional Logic for Fields. This allows you to show or hide a field based on the value of another field. Each field type has contextually aware conditional checks which handle numeric or text-based comparisons.
Contents
Supported Field Types
- Text
- Plain Text
- Website
- Phone
- Password
- Paragraph
- Plain Paragraph Text
- Code (Syntax Highlighting)
- Date / Time
- Date / Time
- Date
- Time
- Number
- Plain Number
- Currency
- Relationships / Media
- File / Image / Video
- Limited conditions:
- =
- !=
- blank
- not blank
- Conditional value to use: ID
- Limited conditions:
- oEmbed
- Relationship
- Limited conditions:
- =
- !=
- blank
- not blank
- Conditional value to use: The value (simple custom defined) or ID (object
relationships)
- Limited conditions:
- File / Image / Video
- Other
- Yes / No
- Limited conditions:
- =
- !=
- Conditional value to use: 0 or 1
- Limited conditions:
- Color Picker
- Limited conditions:
- =
- !=
- blank
- not blank
- Conditional value to use: The value (simple custom defined) or ID (object
relationships)
- Limited conditions:
- Yes / No
Unsupported Field Types
- Paragraph
- WYSIWYG (Visual Editor) — There’s an issue with the visual editor syncing with the conditional
value being checked, this will be worked on
- WYSIWYG (Visual Editor) — There’s an issue with the visual editor syncing with the conditional
- Layout Elements
- Heading
- HTML Content
Setting up Conditional Logic for a Field
When adding or editing your field, go to the “Conditional Logic” tab and you will find an option to “Enable Conditional Logic”.
Turning Conditional Logic on will now allow you to specify conditional rules to use.
Turning Conditional Logic off again will result in the conditional rules previously set being ignored and the field will become visible in all cases.
Available Logic
Visibility action
- Show field
- Hide field
Rule Requirements
- Any – Any rules met will trigger the visibility action
- All – All rules met will trigger the visibility action
Conditional Rules
- is (=) – Whether the field value exactly equals the text value provided
- is not (!=) – Whether the field value does not exactly equal the text value provided
- is blank – Whether the field value is empty (Yes / No fields are excluded)
- is not blank – Whether the field value is not empty (Yes / No fields are excluded)
- Numeric rules
- greater than (>) – Whether the field value is greater than the text value provided
- greater than or equal (>=) – Whether the field value is greater than or equal to the text value provided
- less than (<) – Whether the field value is less than the text value provided
- less than or equal (<=) – Whether the field value is less than or equal to the text value provided
- Text rules (excluding File / Image / Video, Relationship, Yes / No, Color Picker)
- contains – Whether the field value contains the text value provided
- does not contain – Whether the field value does not contain the text value provided
- starts with – Whether the field value starts with the text value provided
- does not start with – Whether the field value does not start with the text value provided
- ends with – Whether the field value ends with the text value provided
- does not end with – Whether the field value does not end with the text value provided
- matches pattern – Whether the field value matches the Regular Expression pattern from the text value provided
- Patterns must exclude the wrapper character, so
/\d/would just be\d
- Patterns must exclude the wrapper character, so
- does not match pattern – Whether the field value does not match the Regular Expression pattern from the text value provided
- Patterns must exclude the wrapper character, so
/\d/would just be\d
- Patterns must exclude the wrapper character, so
Conditional values used by field type
- Text-based fields are simple, they are what they are for input.
- Date fields always use values that are in ISO 8601 format for value comparisons.
- For date/time fields, the format for
March 1st, 2023 at 2pmwould be2023-03-01 14:00:00 - For date fields, the format for
March 1st, 2023would be2023-03-01 - For time fields, the format for
2pmwould be14:00:00
- For date/time fields, the format for
- Relationships have different values referenced depending on what kind of object is related to.
- For objects like Posts or Users that have IDs, the ID value will be what you want to reference in your conditional.
- For simple objects like custom defined lists or predefined lists, the value will be what you want to reference.
- If no values are set in a format like
value|Labelfor custom defined lists, then the label automatically is referenced as the value.
- If no values are set in a format like
Currently, fields that are conditionally hidden and submitted through forms will not be saved. If a field was hidden by Conditional Logic, it will be excluded from the saving process itself when submitting forms.
If you are saving values manually with PHP, conditional logic will not be checked.
You can enable saving of all fields with conditional logic submitted through forms by bypassing that logic using the below filter.
// Enable saving conditionally hidden fields. add_filter( 'pods_api_save_pod_item_exclude_conditionally_hidden_fields', '__return_false' );
Displaying values with Conditional Logic
If you get the value of a field or display it, nothing will change even if the conditional logic has hidden that field in forms. You will still need to use your own logic to handle whether to display that field’s value.
Extending Conditional Logic (JS)
There is not currently a way to hook into the JS side of the conditional logic handling. This will be added in a future release.
Extending Conditional Logic (PHP)
If you want to support your own custom rules, there is a PHP hook to handle this (only runs on form submit saves on the PHP side). This filter looks like this and you can take that Conditional_Logic object and adjust rules with $logic->set_rules( $rules_array ) and other available methods from that object.
/** * Allow filtering the conditional logic object used for a Whatsit object. * * @since 3.0 * * @param Conditional_Logic $conditional_logic The conditional logic object. * @param Whatsit $object The object data. * @param string $action The action to take (show/hide). * @param string $logic The logic to use (any/all). * @param array $rules The conditional rules. */ $conditional_logic = apply_filters( 'pods_data_conditional_logic_for_object', $conditional_logic, $object, $action, $logic, $rules );