Overwrite the Affiliate Cookie in Ultimate Affiliate Pro
In Ultimate Affiliate Pro, when a user clicks on an affiliate link, a cookie (such as uap_referral
) is set to track the affiliate. If the same user clicks on a different affiliate link later, you might want to overwrite the existing cookie to credit the new affiliate.
Contents
show
Here’s how you can handle this scenario:
Steps to Overwrite the Affiliate Cookie
- Check if the Cookie Exists:
- Before setting a new cookie, check if the current
uap_referral
cookie exists.
- Before setting a new cookie, check if the current
- Overwrite the Cookie if a New Affiliate Link is Used:
- If the cookie exists and a different affiliate link is used, overwrite the current cookie with the new affiliate ID.
- Code to Overwrite the Cookie:
You can hook into WordPress’s init
action to check for the affiliate link, and then overwrite the existing affiliate cookie if necessary.
function overwrite_affiliate_cookie() {
// Check if an affiliate ID is present in the URL (e.g., ?ref=affiliate_id)
if (isset($_GET['ref'])) {
$new_affiliate_id = sanitize_text_field($_GET['ref']);
// Check if the current cookie exists
if (isset($_COOKIE['uap_referral'])) {
// Get the current cookie value
$current_affiliate_data = unserialize(stripslashes($_COOKIE['uap_referral']));
// Compare the current affiliate ID with the new one
if ($current_affiliate_data['affiliate_id'] != $new_affiliate_id) {
// Overwrite the cookie with the new affiliate ID
$new_affiliate_data = array(
'affiliate_id' => $new_affiliate_id,
'campaign' => '', // Add campaign if needed
'referral_hash' => md5(uniqid($new_affiliate_id, true)), // Generate a new referral hash
'visit_id' => $current_affiliate_data['visit_id'] + 1, // Increment visit ID (optional)
'timestamp' => time(),
'site_referer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
);
// Serialize the new data
$new_affiliate_data_serialized = serialize($new_affiliate_data);
// Set the cookie with the new affiliate data
setcookie('uap_referral', $new_affiliate_data_serialized, time() + (86400 * 30), '/'); // 30 days expiry
// Optionally, log or debug
// error_log('Affiliate cookie overwritten: ' . print_r($new_affiliate_data, true));
}
} else {
// Set a new cookie if it doesn't exist
$affiliate_data = array(
'affiliate_id' => $new_affiliate_id,
'campaign' => '', // Add campaign if needed
'referral_hash' => md5(uniqid($new_affiliate_id, true)),
'visit_id' => 1, // Start with visit ID 1
'timestamp' => time(),
'site_referer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
);
// Serialize and set the cookie
$affiliate_data_serialized = serialize($affiliate_data);
setcookie('uap_referral', $affiliate_data_serialized, time() + (86400 * 30), '/'); // 30 days expiry
}
}
}
add_action('init', 'overwrite_affiliate_cookie', 2);
Explanation of Code:
- Check for
ref
Query Parameter:- The code looks for the affiliate ID in the URL parameter (e.g.,
?ref=123
).
- The code looks for the affiliate ID in the URL parameter (e.g.,
- Retrieve Current Cookie Value:
- If the
uap_referral
cookie is already set, it is retrieved and unserialized into an array.
- If the
- Compare Affiliate IDs:
- If the current affiliate ID stored in the cookie is different from the new one (from the
ref
query parameter), the cookie is overwritten with the new affiliate data.
- If the current affiliate ID stored in the cookie is different from the new one (from the
- Set a New Cookie if Not Present:
- If the cookie does not exist, it is created with the new affiliate ID.
- Serialize and Set the Cookie:
- The cookie data is serialized and saved, with an expiration time of 30 days (
86400 * 30
).
- The cookie data is serialized and saved, with an expiration time of 30 days (
- Unique Referral Hash:
- A new
referral_hash
is generated each time an affiliate ID is stored.
- A new
- Hook it early in the init, in my example it is on level 2 priority.
Important Notes:
- Sanitization: Always sanitize the input from the URL using
sanitize_text_field()
to avoid security vulnerabilities. - Cookie Expiration: The cookie is set to expire after 30 days (
time() + (86400 * 30)
), but you can adjust this based on your requirements. - Referrals: If your campaign involves additional tracking parameters (like
campaign
), you can add that to the cookie as well. - Testing: Ensure that you test this code thoroughly in a development environment to confirm that the cookie is properly overwritten when a different affiliate link is clicked.
This method ensures that the uap_referral
cookie is updated with the latest affiliate link when the user clicks on a new one, while respecting the rules of your affiliate program.
Comments
Leave a Comment