Custom field using PHP with GiveWP plugin

I am trying my luck at PHP (which I have very limited experience with). I have a custom field set up using PHP for a plugin called GiveWP. The field is to specify where the person wants their donation to go to support. I then added PHP to add that to an email tag so that admin and the donor can see what was entered in that field. Problem is, the codes aren’t matching up to show in the notification email what was entered. It just says “No support data found”.

Here is the page: http://dev1.igniswebdesign.com/donate/

Here is the code – can you help?

Code For Custom Field

/**
* Add Custom Donation Form Fields
*
* @param $form_id
*/
function give_myprefix_custom_form_fields( $form_id ) {
// Only display for forms with the ID “415”;
// Remove “If” statement to display on all forms
// For a single form, use this instead:
// if ( $form_id == 415) {
$forms = array( 415 ); ?>
<div id=”give-support-wrap”>
<label class=”give-label” for=”give-support”><?php _e( ‘What would you like to suppport?’, ‘give’ ); ?> <span class=”give-tooltip icon icon-question” data-tooltip=”<?php _e( ‘What would you like your donation to support?’, ‘give’ ) ?>”></span>
</label>
<select name=”give-support”>
<option value=””>Select…</option>
<option value=”T-Shirts/Sweatshirts/Hats”>T-Shirts/Sweatshirts/Hats</option>
<option value=”Africa”>Africa</option>
<option value=”Armenia”>Armenia</option>
<option value=”Bolivia”>Bolivia</option>
<option value=”Columbia”>Columbia</option>
<option value=”Dominican Republic”>Dominica Republic</option>
<option value=”Peru”>Peru</option>
<option value=”No Preference”>No Preference</option>
</select>
</div>
<?php
// endif;
}

add_action( ‘give_after_donation_levels’, ‘give_myprefix_custom_form_fields’, 10, 1 );

/**
* Add Field to Payment Meta
*
* Store the custom field data custom post meta attached to the <code>give_payment</code> CPT.
*
* @param $payment_id
* @param $payment_data
*
* @return mixed
*/
function myprefix123_give_donations_save_custom_fields( $payment_id, $payment_data ) {
if ( isset( $_POST[‘give_support’] ) ) {
$message = implode( “n”, array_map( ‘sanitize_text_field’, explode( “n”, $_POST[‘give_support’] ) ) );
add_post_meta( $payment_id, ‘give_support’, $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_support = get_post_meta( $payment_id, ‘give_support’, true );
if ( $give_support ) : ?>
<div class=”support-data”>
<label><?php _e( ‘support:’, ‘give’ ); ?></label>
<?php echo wpautop( $give_support ); ?>
</div>
<?php endif;
}

add_action( ‘give_payment_personal_details_list’, ‘give_myprefix_purchase_details’, 10, 2 );

Code For Email Tag

/**
* Adds a Custom “Support” Tag
* @description: This function creates a custom Give email template tag
*
* @param $payment_id
*/
function my_custom_prefix_add_sample_support_tag( $payment_id ) {
give_add_email_tag( ‘support’, ‘This tag can be used to output the custom support field’, ‘my_custom_prefix_get_donation_support_data’ );
}

add_action( ‘give_add_email_tags’, ‘my_custom_prefix_add_sample_support_tag’ );

/**
* Get Donation Support 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_support_data( $payment_id ) {

$payment_meta = give_get_payment_meta( $payment_id );
$output = __( ‘No support data found.’, ‘give’ );
if ( isset( $payment_meta[‘support’] ) && ! empty( $payment_meta[‘support’] ) ) {
$output = $payment_meta[‘support’];
}

return $output;
}

Read more here:: Custom field using PHP with GiveWP plugin

Leave a Reply

Your email address will not be published. Required fields are marked *