Skip to content

Instantly share code, notes, and snippets.

@rickalday
Last active October 30, 2024 14:59
Show Gist options
  • Save rickalday/a61bff5aa46b4eb73d7be033e65f388b to your computer and use it in GitHub Desktop.
Save rickalday/a61bff5aa46b4eb73d7be033e65f388b to your computer and use it in GitHub Desktop.
Adds referral URL to donations made on Visual Builder forms
<?php
// Adds a hidden field to the form that stores the URL
add_action('givewp_donation_form_schema', static function (Give\Framework\FieldsAPI\DonationForm $form) {
$field = Give\Framework\FieldsAPI\Hidden::make('referralUrl')
->defaultValue($_SERVER['HTTP_REFERER'])
->emailTag('referralUrl');
$form->insertAfter('email', $field);
});
// (Optional) Use this email tag in the email templates {meta_donation_referralUrl}
function referralurl_donation_details( $payment_id ) {
$payment = new Give_Payment( $payment_id );
$payment_meta = $payment->get_meta();
$referral_url = esc_url($payment_meta['referralUrl']);
if ( $referral_url ) : ?>
<div id="give-referral_url" class="give-admin-box-inside">
<p><strong><?php esc_html_e( 'Referral URL:', 'give' ); ?></strong><br />
<a href="<?php echo $referral_url; ?>" target="_blank" rel="noopener noreferrer"><?php echo $referral_url; ?></a>
</div>
<?php endif;
}
add_action( 'give_view_donation_details_payment_meta_after', 'referralurl_donation_details', 10, 1 );
function export_option_givewp_referral_url() {
?>
<tr class="give-export-option-fields give-export-option-custom-field">
<td scope="row" class="row-title">
<label><?php esc_html_e( 'Referral URL:', 'give' ); ?></label>
</td>
<td class="give-field-wrap">
<div class="give-clearfix">
<ul class="give-export-option-custom-fields-ul">
<!-- Custom field checkbox -->
<li class="give-export-option-start">
<label for="give-custom-field">
<input type="checkbox" checked
name="give_give_donations_export_option[referralUrl]"
id="referral-url"><?php _e( 'Referral URL', 'give' ); ?>
</label>
</li>
</ul>
</div>
</td>
</tr>
<?php
}
add_action( 'give_export_donation_fields', 'export_option_givewp_referral_url' );
/**
* This function sets the column header in the generated CSV file for the custom field. Add an if statement for each aditional field.
*/
function column_header_givewp_referral_url( $cols ) {
if ( isset( $cols['referralUrl'] ) ) {
$cols['referralUrl'] = __( 'Referral URL', 'give' );
}
return $cols;
}
add_filter( 'give_export_donation_get_columns_name', 'column_header_givewp_referral_url' );
/**
* This function populates the CSV with the custom field data. Add an if statement for each aditional field.
*/
function export_givewp_referral_url( $data, $payment, $columns ) {
if ( ! empty( $columns['referralUrl'] ) ) {
$data['referralUrl'] = '';
$refURL = $payment->get_meta( 'referralUrl' );
if ($refURL ) {
$data['referralUrl'] = $refURL;
}
}
return $data;
}
add_filter( 'give_export_donation_data', 'export_givewp_referral_url', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment