Last active
May 9, 2024 09:52
-
-
Save goranefbl/09ba1f657d89fe9aaa87ffd41ce2e695 to your computer and use it in GitHub Desktop.
Track referrals with WPForms
This file contains 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
function wpgens_get_user_email_by_meta_or_email($meta_value) { | |
// Check if the meta_value is a valid email | |
if (is_email($meta_value)) { | |
return $meta_value; // If it's a valid email, return it directly | |
} | |
$args = array( | |
'meta_key' => 'gens_referral_id', | |
'meta_value' => $meta_value, | |
'number' => 1, | |
'fields' => 'ID' // Only get user ID to optimize the query | |
); | |
// Get user ID based on meta key and value | |
$users = get_users($args); | |
// Check if a user was found | |
if (!empty($users)) { | |
// Since we are expecting only one user, take the first one | |
$user_id = $users[0]; | |
$user_info = get_userdata($user_id); | |
// Check if user data was successfully retrieved and email exists | |
if ($user_info && !empty($user_info->user_email)) { | |
return $user_info->user_email; | |
} | |
} | |
return ''; // Return empty string if no user is found | |
} | |
function wpf_dev_process_filter( $fields, $entry, $form_data ) { | |
$form_id = 263; | |
// Bail early if form ID does not match | |
if ( $form_data[ 'id' ] != $form_id ) { | |
return $fields; | |
} | |
$referral_code = sanitize_text_field( $_COOKIE['gens_raf'] ); | |
foreach ( $fields as $field ) { | |
// Find ID of field, add text field or hidden field | |
if ( $field[ 'id' ] == 3 ) { | |
$fields[$field[ 'id' ]][ 'value' ] = wpgens_get_user_email_by_meta_or_email($referral_code); | |
} | |
} | |
return $fields; | |
} | |
add_filter( 'wpforms_process_filter', 'wpf_dev_process_filter', 10, 3 ); | |
// Divi Forms | |
function wpgens_get_user_email_by_meta_or_email($meta_value) { | |
// Check if the meta_value is a valid email | |
if (is_email($meta_value)) { | |
return $meta_value; // If it's a valid email, return it directly | |
} | |
$args = array( | |
'meta_key' => 'gens_referral_id', | |
'meta_value' => $meta_value, | |
'number' => 1, | |
'fields' => 'ID' // Only get user ID to optimize the query | |
); | |
// Get user ID based on meta key and value | |
$users = get_users($args); | |
// Check if a user was found | |
if (!empty($users)) { | |
// Since we are expecting only one user, take the first one | |
$user_id = $users[0]; | |
$user_info = get_userdata($user_id); | |
// Check if user data was successfully retrieved and email exists | |
if ($user_info && !empty($user_info->user_email)) { | |
return $user_info->user_email; | |
} | |
} | |
return ''; // Return empty string if no user is found | |
} | |
function wpgens_add_email_data_to_body() { | |
if (is_page('webcare-signup')) { | |
$referral_code = isset($_COOKIE['gens_raf']) ? sanitize_text_field($_COOKIE['gens_raf']) : ''; | |
$email = wpgens_get_user_email_by_meta_or_email($referral_code); | |
echo "<script type='text/javascript'>var referralEmail = '{$email}';</script>"; | |
} | |
} | |
add_action('wp_head', 'wpgens_add_email_data_to_body'); | |
// Javascript | |
<script> | |
document.addEventListener("DOMContentLoaded", function() { | |
if (typeof referralEmail !== 'undefined' && referralEmail !== '') { | |
var inputField = document.getElementById('et_pb_contact_referral_0'); | |
if (inputField) { | |
inputField.value = referralEmail; | |
} | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment