Custom Field No Longer Working
-
Hi – I created a custom field to collect the name/organization that a visitor is donating to some time ago using code for you documentation. I added it to the site using the My Custom Function plugin. We recently noticed that it is no longer being sent in the email notification when a donation is made. We are receiving this instead of the data – “No referral data found.”
I’m wondering if a plugin update changed something that we need to adjust the code we originally added.
Any idea’s?
Thanks!!
The page I need help with: [log in to see the link]
-
HI @willnev — yes Give 2.0 saves and retrieves the donation meta in a different way now. We can help best if you provide your whole code snippet, then we can point you to where it needs to be updated.
@willnev Are you using direct SQL queries to get metadata or Give/WP core functions like get_post_meta etc.
Can you share code sample because we added backward compatibility in 2.0?
Hey guys –
Thanks for resposnding!
Here’s the code that I’m using. It’s being added by using the PHP Inserter plugin.
/**
* Custom Form Fields
*
* @param $form_id
*/function give_myprefix_custom_form_fields( $form_id ) {
//Only display for a specific form;
//Remove “If” statement to display on all forms?>
<div id=”give-referral-wrap”>
<label class=”give-label” for=”give-referral”><?php _e( ‘Who would you liketo donate to?:’, ‘give’ ); ?></label>
<textarea class=”give-textarea” name=”give_referral” id=”give-
referral”></textarea>
</div>
<?php}
add_action( ‘give_purchase_form_after_personal_info’, ‘give_myprefix_custom_form_fields’, 10, 1 );
/**
* Validate Custom Field
*
* @description check for errors without custom fields
*
* @param $valid_data
* @param $data
*/
function give_myprefix_validate_custom_fields( $valid_data, $data ) {//Check that we’re validating the proper form
//Remove if this is a global field
if ( $data[‘give-form-id’] !== ‘754’ ) {
return;
}//Check for a referral data
if ( empty( $data[‘give_referral’] ) ) {
give_set_error( ‘give_referral’, __( ‘Who would you like to donate to.’, ‘give’ ));
}
}add_action( ‘give_checkout_error_checks’, ‘give_myprefix_validate_custom_fields’, 10, 2 );
/**
* Add Field to Payment Meta
*
* @description store the custom field data in the payment meta
*
* @param $payment_meta
*
* @return mixed
*/
function give_myprefix_store_custom_fields( $payment_meta ) {
$payment_meta[‘referral’] = isset( $_POST[‘give_referral’] ) ? implode( “n”, array_map(‘sanitize_text_field’, explode( “n”, $_POST[‘give_referral’] ) ) ) : ”;
return $payment_meta;
}add_filter( ‘give_payment_meta’, ‘give_myprefix_store_custom_fields’ );
/**
* Show Data in Transaction Details
*
* @description show the custom field(s) on the transaction page
*
* @param $payment_meta
* @param $user_info
*/
function give_myprefix_purchase_details( $payment_meta, $user_info ) {//uncomment below to see payment_meta array
// echo “"; // var_dump($payment_meta); // echo "
“;
//Bounce out if no data for this transaction
if ( ! isset( $payment_meta[‘referral’] ) ) {
return;
}?>
<div class=”referral-data”>
<label><?php echo __( ‘Donation Goes To:’, ‘give’ ); ?></label>
<?php echo wpautop( $payment_meta[‘referral’] ); ?>
</div><?php
}add_action( ‘give_payment_personal_details_list’, ‘give_myprefix_purchase_details’, 10, 2 );/* Enter Your Custom Functions Here */
/**
* Adds a Custom “Referral” Tag
* @description: This function creates a custom Give email template tag
*
* @param $payment_id
*/
function my_custom_prefix_add_sample_referral_tag( $payment_id ) {
give_add_email_tag( ‘referral’, ‘This tag can be used to output the custom referral field’, ‘my_custom_prefix_get_donation_referral_data’ );
}add_action( ‘give_add_email_tags’, ‘my_custom_prefix_add_sample_referral_tag’ );
/**
* Get Donation Referral Data
*
* @description Example function that returns Custom field data if present in payment_meta; the example used here is in conjunction with the Give documentation tutorials
* @param $payment_id
*
* @return string|void
*/
function my_custom_prefix_get_donation_referral_data( $payment_id ) {$payment_meta = give_get_payment_meta( $payment_id );
$output = __( ‘No referral data found.’, ‘give’ );
if ( isset( $payment_meta[‘referral’] ) && ! empty( $payment_meta[‘referral’] ) ) {
$output = $payment_meta[‘referral’];
}return $output;
}@willnev sorry for your inconvenience
After 2.0 email tags render callback start accepting array instead of payment ID.
Read here: https://givewp.com/documentation/developers/how-to-add-custom-email-tags/Use below function as email tag render callback.
/** * Get Donation Referral Data * * @description Example function that returns Custom field data if present in payment_meta; the example used here is in conjunction with the Give documentation tutorials * * @param array $tag_args * * @return string */ function my_custom_prefix_get_donation_referral_data( $tag_args ) { $payment_meta = give_get_payment_meta( $tag_args['payment_id'] ); $output = __( 'No referral data found.', 'give' ); if ( isset( $payment_meta['referral'] ) && ! empty( $payment_meta['referral'] ) ) { $output = $payment_meta['referral']; } return $output; }
Hey guys – I made a mistake as to which form I’m having an issue with. It’s actually, the form on the home page not the one 1 posted above. I added the code you suggested but still receive {referral} in the email confirmation rather than the name of the person that is being donated to.
The code now looks like this:
/**
* Custom Form Fields
*
* @param $form_id
*/function give_myprefix_custom_form_fields( $form_id ) {
//Only display for a specific form;
//Remove “If” statement to display on all forms?>
<div id=”give-referral-wrap”>
<label class=”give-label” for=”give-referral”><?php _e( ‘Who would you liketo donate to?:’, ‘give’ ); ?></label>
<textarea class=”give-textarea” name=”give_referral” id=”give-
referral”></textarea>
</div>
<?php}
add_action( ‘give_purchase_form_after_personal_info’, ‘give_myprefix_custom_form_fields’, 10, 1 );
/**
* Validate Custom Field
*
* @description check for errors without custom fields
*
* @param $valid_data
* @param $data
*/
function give_myprefix_validate_custom_fields( $valid_data, $data ) {//Check that we’re validating the proper form
//Remove if this is a global field
if ( $data[‘give-form-id’] !== ‘754’ ) {
return;
}//Check for a referral data
if ( empty( $data[‘give_referral’] ) ) {
give_set_error( ‘give_referral’, __( ‘Who would you like to donate to.’, ‘give’ ));
}
}add_action( ‘give_checkout_error_checks’, ‘give_myprefix_validate_custom_fields’, 10, 2 );
/**
* Add Field to Payment Meta
*
* @description store the custom field data in the payment meta
*
* @param $payment_meta
*
* @return mixed
*/
function give_myprefix_store_custom_fields( $payment_meta ) {
$payment_meta[‘referral’] = isset( $_POST[‘give_referral’] ) ? implode( “n”, array_map(‘sanitize_text_field’, explode( “n”, $_POST[‘give_referral’] ) ) ) : ”;
return $payment_meta;
}add_filter( ‘give_payment_meta’, ‘give_myprefix_store_custom_fields’ );
/**
* Show Data in Transaction Details
*
* @description show the custom field(s) on the transaction page
*
* @param $payment_meta
* @param $user_info
*/
function give_myprefix_purchase_details( $payment_meta, $user_info ) {//uncomment below to see payment_meta array
// echo “"; // var_dump($payment_meta); // echo "
“;
//Bounce out if no data for this transaction
if ( ! isset( $payment_meta[‘referral’] ) ) {
return;
}?>
<div class=”referral-data”>
<label><?php echo __( ‘Donation Goes To:’, ‘give’ ); ?></label>
<?php echo wpautop( $payment_meta[‘referral’] ); ?>
</div><?php
}add_action( ‘give_payment_personal_details_list’, ‘give_myprefix_purchase_details’, 10, 2 );/* Enter Your Custom Functions Here */
/**
* Get Donation Referral Data
*
* @description Example function that returns Custom field data if present in payment_meta; the example used here is in conjunction with the Give documentation tutorials
*
* @param array $tag_args
*
* @return string
*/
function my_custom_prefix_get_donation_referral_data( $tag_args ) {$payment_meta = give_get_payment_meta( $tag_args[‘payment_id’] );
$output = __( ‘No referral data found.’, ‘give’ );if ( isset( $payment_meta[‘referral’] ) && ! empty( $payment_meta[‘referral’] ) ) {
$output = $payment_meta[‘referral’];
}return $output;
}It’s easier to work on this stuff with a GIST instead of here in the forum. Most likely you just need to remove that IF statement in the “Validate Custom Field” function. I’ve done that here in this GIST:
https://gist.github.com/mathetos/7c54fedef7eed37c4350e047a548cf7b
Feel free to fork that, tweak it locally, and if you keep struggling post your code as a new GIST for us.
Thanks so much for your quick response!
I updated the code with what you have there but the plugin My Custom Functions says there a “fatal error” (sound dire -lol).
Hey there… I really just took your code from above to show how to use GIST. But I’ve cleaned up that code a bit now. Try again, let me know how it goes.
- The topic ‘Custom Field No Longer Working’ is closed to new replies.