Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpmudev-sls/4124c18d1bcbd4252562ec87271b995a to your computer and use it in GitHub Desktop.
Save wpmudev-sls/4124c18d1bcbd4252562ec87271b995a to your computer and use it in GitHub Desktop.
[Forminator] Omit field on email notifications.
<?php
/**
* Plugin Name: [Forminator] Omit "none" on email notifications.
* Plugin URI: https://premium.wpmudev.org/
* Description: Omit the "select" field type in email notifications if the selected value is "none".
* Author: Glauber Silva @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* Task: 0/11289012348292/1168248790047528
* License: GPLv2 or later
*
* @package Forminator_omit_none_on_email_notifications
*/
defined( 'ABSPATH' ) || exit;
/**
* Message data filter - This filter is present in: plugins/forminator/library/modules/custom-forms/front/front-mail.php
*
* @since 1.0.4
*
* @param array $data - the post data.
* @param Forminator_Custom_Form_Model $custom_form - the form.
* @param Forminator_Form_Entry_Model $entry - saved entry @since 1.0.3.
*
* @return array $data
*/
function wpmudev_omit_none_on_email_notifications( $data, $custom_form, $entry ) {
$fields_list = array( 'select', 'checkbox', 'radio' );
foreach ( $custom_form->fields as $key_field => $value_field ) {
if ( array_key_exists( $value_field->slug, $data ) && in_array( $value_field->raw['type'], $fields_list, true ) ) {
if ( is_array( $data[ $value_field->slug ] ) && 1 === count( $data[ $value_field->slug ] ) && strpos( strtolower( $data[ $value_field->slug ][0] ), 'none' ) !== false ) {
unset( $custom_form->fields[ $key_field ] );
} elseif ( is_array( $data[ $value_field->slug ] ) ) {
foreach ( $data[ $value_field->slug ] as $key_array => $value_array ) {
if ( strpos( strtolower( $value_array ), 'none' ) !== false ) {
unset( $entry->meta_data[ $value_field->slug ]['value'][ $key_array ] );
}
}
} elseif ( strpos( strtolower( $data[ $value_field->slug ] ), 'none' ) !== false ) {
unset( $custom_form->fields[ $key_field ] );
}
} elseif ( in_array( $value_field->raw['type'], $fields_list, true ) ) {
unset( $custom_form->fields[ $key_field ] );
}
}
return $data;
}
add_filter( 'forminator_custom_form_mail_data', 'wpmudev_omit_none_on_email_notifications', 999, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment