I cleaned that up a bit. This worked as intended for me:
https://gist.github.com/mathetos/9247769bd31caef15655a225994e24cf
Thanks!
Thanks so much…that worked!
Hi Matt,
The fields shows “*” as if it’s required and validation is getting called, but the email notices don’t show this new field.
I’ve tried {give-referral} and {give_referral} in the email template, but neither show the sponsored person’s name…just the literal variable value.
/**
* Add Custom Donation Form Fields
*
* @param $form_id
*/
function give_myprefix_custom_form_fields( $form_id ) {
if ( $form_id == 7290) {
//$forms = array( 7290 );
?>
<div id=”give-referral-wrap”>
<label class=”give-label” for=”give-referral”><?php _e( ‘Triath-a-Toddlers Name?:’, ‘give’ ); ?><span class=”give-required-indicator”>*</span> <span class=”give-tooltip icon icon-question” data-tooltip=”<?php _e( ‘Please tell us whom you are sponsoring.’, ‘give’ ) ?>”></span>
</label>
<textarea class=”give-textarea” name=”give_referral” id=”give-referral”></textarea>
</div>
<?php
}
}
add_action( ‘give_after_donation_levels’, ‘give_myprefix_custom_form_fields’, 10, 1 );
/*
* Validate Custom Field
*
* Check for errors without custom fields
*
* @param $valid_data
* @param $data
*/
function give_myprefix_validate_custom_fields( $required_fields, $form_id ) {
//$forms = array( 7290 );
if ( $form_id == 7290 ) {
$required_fields[‘give_referral’] = array(
‘error_id’ => ‘invalid_give_referral’,
‘error_message’ => __( ‘Please tell us whom you are sponsoring.’, ‘give’ ),
);
}
return $required_fields;
}
add_filter( ‘give_donation_form_required_fields’, ‘give_myprefix_validate_custom_fields’, 10, 2 );
/**
* Add Field to Payment Meta
*
* Store the custom field data custom post meta attached to the give_payment CPT.
*
* @param $payment_id
* @param $payment_data
*
* @return mixed
*/
function myprefix123_give_donations_save_custom_fields( $payment_id, $payment_data ) {
if ( isset( $_POST[‘give_referral’] ) ) {
$message = implode( “\n”, array_map( ‘sanitize_text_field’, explode( “\n”, $_POST[‘give_referral’] ) ) );
add_post_meta( $payment_id, ‘give_referral’, $message );
}
}
add_action( ‘give_insert_payment’, ‘myprefix123_give_donations_save_custom_fields’, 10, 2 );
/**
* Show Data in Payment Details
*
* Show the custom field(s) on the payment details page in wp-admin
*
* @param $payment_meta
* @param $user_info
*/
function give_myprefix_purchase_details( $payment_meta, $user_info ) {
// Bounce out if no data for this transaction
$give_referral = get_post_meta( $payment_id, ‘give_referral’, true );
if ( $give_referral ) : ?>
<div class=”referral-data”>
<label><?php _e( ‘Referral:’, ‘give’ ); ?></label>
<?php echo wpautop( $give_referral ); ?>
</div>
<?php endif;
}
Are you seeing the data reflected on the Donation detail page? If so, that means your function for adding the information as “Donation meta” is working correctly. In that case then, I would recommend just using our Meta Email tag instead of a custom function.
See our docs on that here:
https://givewp.com/documentation/core/settings/emails/meta-email-tags/
Essentially, as-is without a custom email tag, you should be able to use a meta email tag like this: {meta_donation_give_referral}
Test that with the Preview Email feature on a donation where you KNOW the give_referral information is showing correctly in the Donation Details.
Thanks!
Thanks so much…that worked using this in the email forms:
Triath-a-Toddlers Name: {meta_donation_give_referral}
Glad to hear it.
If you’re enjoying Give and appreciate our support, we’d love a kind review from you here:
https://wordpress.org/support/plugin/give/reviews/
Thanks!