Hi Eric, sorry to bother you again. The above code works perfectly on the front end, but unfortunately doesn’t save my custom value to the database.
If I amend my code with the addition of a “! is_admin” condition, my custom value is successfully saved to the database, but I’m back to receiving a validation error on all other forms where the ‘receivers_email’ field has been excluded / is not required.
My code that saves to the database, but causes a validation error on forms that do not feature the ‘receivers_email’ field:
function wg_customize_donation_form( $fields, Charitable_Donation_Form $form ) {
if ( ! is_admin() && ! has_term( 'groups', 'campaign_category', get_the_ID() ) ) {
unset( $fields['receivers_email'] );
}
return $fields;
}
add_filter( 'charitable_donation_form_user_fields', 'wg_customize_donation_form', 10, 2 );
My code that produces no validation errors, but doesn’t save my custom value to the donation post_meta donor array:
function wg_customize_donation_form( $fields, Charitable_Donation_Form $form ) {
if ( ! has_term( 'groups', 'campaign_category', get_the_ID() ) ) {
unset( $fields['receivers_email'] );
}
return $fields;
}
add_filter( 'charitable_donation_form_user_fields', 'wg_customize_donation_form', 10, 2 );
The code I am using to generate my custom field is:
function wg_add_recipient_email_field_for_groups_only() {
$field = new Charitable_Donation_Field( 'receivers_email', array(
'label' => __( 'Receiver\'s email', 'wg' ),
'data_type' => 'user',
'donation_form' => array(
'type' => 'email',
'show_before' => 'email',
'required' => true,
),
'admin_form' => array(
'type' => 'email',
'show_after' => 'email',
'required' => false,
),
'show_in_meta' => true,
'show_in_export' => true,
'email_tag' => array(
'description' => __( 'The receiver\'s email address' , 'wg' ),
),
) );
charitable()->donation_fields()->register_field( $field );
}
add_action( 'init', 'wg_add_recipient_email_field_for_groups_only' );
Any thoughts very much appreciated and apologies if I’m just doing something really silly/careless here!