How to Customize JetFormBuilder Repeater Email Template Output

JetFormBuilder is a powerful and flexible form builder plugin for WordPress that allows you to create complex and customized forms with ease. One of the most useful features of JetFormBuilder is the ability to send emails to users with the form data they submitted. However, sometimes you may want to include repeater fields in the email, which can be tricky to set up. In this article, we will explain step-by-step how to create an email repeater template in JetFormBuilder using the JetEngine plugin, so you can easily include repeater fields in your email templates and send your users fully customized and informative emails.

The following snippet provides a filter to modify the content of an email template that includes repeater fields in JetFormBuilder.

When a user submits a form with a repeater field, JetFormBuilder generates an email with the repeater data included. However, the default format of the repeater data may not be very readable or user-friendly, especially if there are multiple items in the repeater.

This code modifies the repeater data format in the email template to make it more readable and structured. It does this by creating a label for the repeater field, then iterating through each item in the repeater and formatting the data with labels and separators.

The code first checks the content type of the email (whether it is HTML or plain text) and sets the separator and tab accordingly. Then it iterates through each item in the repeater and generates a row of data for each item. The row includes the item number, labels, and values, separated by tabs or line breaks.

Finally, the code assembles all the rows into a single string with the repeater label at the beginning, and returns the modified content to be used in the email template.

This filter can be added to the functions.php file of a WordPress theme, and will work with JetFormBuilder version 2.1.0 or higher. It is an example of how JetFormBuilder’s filters and hooks can be used to modify the behavior and output of the plugin to meet specific needs.

Edit these variables

The three variables that you can edit to change the output of the repeater field in the email template are:

  1. $separator: This variable sets the separator between each item in the repeater field. By default, the separator is set to “<br>” for HTML emails and “\n\r” for plain text emails. You can change the separator to any other string or character that you prefer.

  2. $tab: This variable sets the tab or indent used in the repeater field for each item. By default, the tab is set to “ ” for HTML emails and “\t” for plain text emails. You can change the tab to any other string or character that you prefer.

  3. $repeater_label: This variable sets the label for the repeater field in the email template. By default, the label is set to “Repeater Heading”. You can change the label to any other string or text that you prefer.

You can modify these variables according to your needs to customize the output of the repeater field in the email template. Just make sure to test the modified output to ensure that it works as expected.

				
					<?php

/**
 * Works in JetFormBuilder >= 2.1.0
 */
 
add_filter(
	'jet-form-builder/send-email/template-repeater',
	function ( string $content, array $items ) {

		/** @var \Jet_Form_Builder\Actions\Types\Send_Email $action */
		$action  = jet_fb_action_handler()->get_current_action();
		$is_html = 'text/html' === $action->get_content_type();

		$index          = 0;
		$separator      = $is_html ? "<br>" : "\n\r";
		$tab            = $is_html ? "&emsp;" : "\t";
		$repeater_label = 'Repeater Heading';

		$rows = array();

		$if_array = function ( $value ) {
			return is_array( $value ) ? implode( ', ', $value ) : $value;
		};

		foreach ( $items as $item ) {
			$item_data = array();

			foreach ( $item as $key => $value ) {
				$label = jet_fb_request_handler()->get_attr( $key, 'label', $key );

				$item_data[] = sprintf( '%1$s: %2$s', $label, call_user_func( $if_array, $value ) );
			}
			$row = "Repeater Item " . ++ $index . $separator . $tab;
			$row .= implode( $separator . $tab, $item_data );

			$rows[] .= $row;
		}

		return ( $separator . $repeater_label . $separator . implode( $separator, $rows ) );
	},
	10, 3
);
				
			

Need help figuring it out? We can help.  Contact us today.