Conditionally Send Notification with ACF Advanced Forms

<?php
/**
 * conditionally send notification emails based on permit type
 *
 * @link https://advancedforms.github.io/filters/af-form-email-recipient/
 * @link https://advancedforms.github.io/filters/af-form-email-content/
 *
 */
function filter_email_content( $content, $email, $form, $fields ) {

     // get permit type from application and check name of email
     $permit_type = af_get_field( 'wd_permit_application_type' );
     $email_name = $email['name'];
     $email_value = print_r( $email );

     // check if permit type and email name match
     if( $email_name == 'User Notice' ) {

          // emtpy out content
          $content = '';

          // loop over permit types user has set in "Site Options"
          if ( have_rows( 'wd_options_permits', 'option' ) ) :
          	while ( have_rows( 'wd_options_permits', 'option' ) ) : the_row();

                    // get permit name set for each options
                    $permit_name = get_sub_field( 'wd_permit_name' );

                    // compare permit name in "Site Options" with permit name from application and attach content if they match
                    if( $permit_type['label'] == $permit_name ) {
                         $content .= get_sub_field( 'email_notification_content' );
                    }

          	endwhile;
          endif;
     }

     return $content;

}
add_filter( 'af/form/email/content/key=form_5f1af8ee58176', 'filter_email_content', 10, 4 );

About Matt Whiteley

Matt has been building bespoke WordPress websites for over 10 years specializing in the Genesis Framework. He integrates Advanced Custom Fields heavily in all builds, especially with the new Gutenberg Block Editor, allowing clients to easily modify all aspects of the their website with ease.

He works with a wide variety of small businesses and agencies across the country providing development services as well as hosting and maintenance. When he isn't coding he enjoys spending time with his wife and two children, golfing and playing poker.