Created
January 10, 2025 09:37
-
-
Save Audiovoyeur/6b2a31c95721065ab68c7791beac43d0 to your computer and use it in GitHub Desktop.
Custom JetFormBuilder Filter for Formatting Repeater Field Output.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_filter( 'jet-form-builder/content-filters', function( $filters ) { | |
| // Define a custom filter class for formatting repeater field data | |
| class Separate_With_Spaces_JFB_Filter extends \Jet_Form_Builder\Classes\Filters\Base_Filter { | |
| // Unique ID for this filter | |
| public function get_id(): string { | |
| return 'separate_with_spaces'; | |
| } | |
| // Define callback arguments (in this case, the delimiter) | |
| public function callback_args(): array { | |
| return array( '' ); | |
| } | |
| // Main function to apply formatting logic | |
| public function apply_macros( $value, ...$args ): string { | |
| // Check if the input value is an array (repeater field data) | |
| if ( is_array( $value ) ) { | |
| $formatted_output = ''; // Initialize an empty string to store formatted output | |
| // Loop through each repeater item (e.g., shareholder) | |
| foreach ( $value as $index => $shareholder ) { | |
| if ( is_array( $shareholder ) ) { | |
| // Add a header for each shareholder | |
| $formatted_output .= "Shareholder " . ($index + 1) . ":\n"; | |
| // Loop through each key-value pair within the shareholder data | |
| foreach ( $shareholder as $field => $field_value ) { | |
| // Format the key as "Fieldname: Value" (replace underscores with spaces, capitalize first letters) | |
| $formatted_output .= ucfirst( str_replace( '_', ' ', $field ) ) . ": " . $field_value . "\n"; | |
| } | |
| // Add a blank line after each shareholder for readability | |
| $formatted_output .= "\n"; | |
| } | |
| } | |
| // Convert newlines to <br> tags for HTML email formatting and sanitize output | |
| return nl2br( wp_kses_post( $formatted_output ) ); | |
| } | |
| // If the value is not an array but a string, format using the chosen delimiter | |
| if ( ! is_string( $value ) ) { | |
| return ''; // Return empty string for unsupported types | |
| } | |
| // Get the delimiter argument (default to ', ' if not provided) | |
| list( $delimiter ) = $args; | |
| if ( empty( $delimiter ) ) { | |
| $delimiter = ', '; | |
| } | |
| // Convert comma-separated values into a string using the chosen delimiter | |
| $output = implode( $delimiter, explode( ',', $value ) ); | |
| // Convert newlines to <br> tags and sanitize output for email display | |
| return nl2br( wp_kses_post( $output ) ); | |
| } | |
| } | |
| // Add the custom filter to JetFormBuilder filters | |
| $filters[] = new Separate_With_Spaces_JFB_Filter(); | |
| return $filters; | |
| } ); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This PHP code snippet adds a custom JetFormBuilder filter to format repeater field data in emails. The filter outputs each entry as Fieldname: Value pairs, making the data easy to read. Each repeater group (e.g., "Shareholder 1") is separated by a blank line for clarity.
Features:
tags for HTML email-friendly output.
Use Case:
This filter is useful for generating human-readable, structured content in JetFormBuilder form notification emails, particularly when handling complex repeater fields.