Skip to content

Instantly share code, notes, and snippets.

@presswizards
Last active July 11, 2024 08:09
Show Gist options
  • Save presswizards/4736b46943668d0f1e62e41abac8298e to your computer and use it in GitHub Desktop.
Save presswizards/4736b46943668d0f1e62e41abac8298e to your computer and use it in GitHub Desktop.
Add Blesta Affiliate ID to all links on page
add_action( 'wp_head', function () { ?>
<script>
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
window.onload = function() {
var anchors = document.getElementsByTagName("a");
let affiliate_id = getParameterByName('a');
if (affiliate_id !== null ) {
let blestadomain = '.presswizards.com';
let daystoexpire = 60;
const d = new Date();
d.setTime(d.getTime() + (daystoexpire*24*60*60*1000));
document.cookie = "affiliate_code=" + affiliate_id + "; expires=" + d.toUTCString() + "; ;domain=" + blestadomain + ";path=/";
console.log('Set cookie - affiliate ID: ' + affiliate_id + '; expires: ' + d.toUTCString() + '; domain: ' + blestadomain);
for (var i = 0; i < anchors.length; i++) {
anchors[i].href = anchors[i].href.indexOf('#') != -1 ? anchors[i].href : anchors[i].href += anchors[i].href.indexOf('?') != -1 ? "&a=" + affiliate_id : "?a=" + affiliate_id;
}
fetch('https://billing.presswizards.com/order/forms/a/' + affiliate_id, { mode: "no-cors" });
}
}
</script>
<?php }, 21 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment