Last active
November 11, 2025 21:03
-
-
Save rickalday/a61bff5aa46b4eb73d7be033e65f388b to your computer and use it in GitHub Desktop.
Adds referral URL to donations made on Visual Builder forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Create a cookie to store the parent window referral url | |
| add_action( "givewp_donation_form_enqueue_scripts", function () { | |
| ?> | |
| <script> | |
| document.addEventListener("DOMContentLoaded", function () { | |
| createCookie("givewp_referrer", window.parent.frames.top.document.referrer, "1"); | |
| // Function to create the cookie | |
| function createCookie(name, value, days) { | |
| let expires; | |
| if (days) { | |
| let date = new Date(); | |
| date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); | |
| expires = "; expires=" + date.toGMTString(); | |
| } | |
| else { | |
| expires = ""; | |
| } | |
| document.cookie = escape(name) + "=" + | |
| escape(value) + expires + "; path=/"; | |
| } | |
| }); | |
| </script> | |
| <?php | |
| } ); | |
| // Create a hidden field in the form and assign the cookie value | |
| add_action( 'givewp_donation_form_schema', static function ( Give\Framework\FieldsAPI\DonationForm $form ) { | |
| $field = Give\Framework\FieldsAPI\Hidden::make( 'http_referrer' ) | |
| ->emailTag( 'http_referrer' ) | |
| ->defaultValue( $_COOKIE["givewp_referrer"] ); | |
| $form->insertAfter( 'email', $field ); | |
| } ); | |
| // Optional function: Add referral url value to donation details page | |
| add_filter( 'givewp_donation_details_custom_fields', function ( $customFields, $donation_id ) { | |
| // Bail early if no ID. | |
| if ( empty( $donation_id ) ) { | |
| return $customFields; | |
| } | |
| $payment_meta = []; | |
| // Prefer a helper function if available (safer than instantiating the legacy class). | |
| if ( function_exists( 'give_get_payment_meta' ) ) { | |
| $payment_meta = (array) give_get_payment_meta( $donation_id ); | |
| } elseif ( class_exists( 'Give_Payment' ) ) { | |
| // Only instantiate if the class exists and the ID is valid. | |
| $payment = new Give_Payment( $donation_id ); | |
| // Ensure we got an object and get_meta() exists. | |
| if ( is_object( $payment ) && method_exists( $payment, 'get_meta' ) ) { | |
| $payment_meta = (array) $payment->get_meta(); | |
| } | |
| } else { | |
| // Fallback to raw post meta if needed. | |
| $payment_meta = (array) get_post_meta( $donation_id, '_give_payment_meta', true ); | |
| } | |
| $referral = ''; | |
| if ( ! empty( $payment_meta['http_referrer'] ) ) { | |
| // Use esc_url_raw for storing/transferring; esc_url() is fine if you need display-safe output. | |
| $referral = esc_url_raw( $payment_meta['http_referrer'] ); | |
| }if ( $referral ) { | |
| $customFields[] = [ 'label' => __( 'Referral URL', 'givewp' ), 'value' => esc_url( $referral ), 'key' => 'http_referrer',]; | |
| } | |
| return $customFields; | |
| }, 10, 2 ); | |
| // Optional: The following three functions add an export option for the field in GiveWP > Tools > Export > Export Donaiton History | |
| 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' ); | |
| 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' ); | |
| function export_givewp_referral_url( $data, $payment, $columns ) { | |
| if ( ! empty( $columns['referralUrl'] ) ) { | |
| $data['referralUrl'] = ''; | |
| $refURL = $payment->get_meta( 'http_referrer' ); | |
| 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