I am currently using the following code in order to automatically prepend affiliate URLs with my affiliate ID:
`add_action('wp_footer', 'auto_affiliate_url');
function auto_affiliate_url()
{
?>
<script>
jQuery(document).ready(function ($) {
// adairs affiliate URL for redirect
var adairsaffURL = "https://t.example.com/12345/t/54321?Url=";
// bedthreads affiliate URL for redirect
var bedthreadsaffURL = "https://t.example.com/12121/t/21212?Url=";
// function called when link is clicked
addAffiliate = function(link) {
var redirectUrl = link.href;
if (redirectUrl.indexOf('adairs.com.au') > 0) {
redirectUrl = adairsaffURL + escape(redirectUrl);
} else if (redirectUrl.indexOf('bedthreads.com.au') > 0) {
redirectUrl = bedthreadsaffURL + escape(redirectUrl);
}
link.href = redirectUrl;
return true;
}
$("a").click(function(event) {
addAffiliate(this);
});
});
</script>
<?php
}`
This works well, however it creates an issue when attempting to open pop ups in Elementor, as it appends something like this to the URL without opening the pop-up:
#elementor-action%3Aaction%3Dpopup%3Aopen%26settings%3DeyJpZCI6IjM3NjgiLCJ0b2dnbGUiOmZhbHNlfQ%3D%3D
Elementor includes these event triggers, but I am not sure if they are useful in this case.
I would like this code to run only if the click is not for a pop up or internal link. I have tried using if ((link.href).indexOf(location.host) < 0 before the function with no luck. Any help would be much appreciated.