I am preparing a page on my wordpress site where I will post coupons and product urls for a website I am an affiliate of.
Instead of creating affiliate links by hand on every URL, I would like to automatically add the affiliate query parameter on every URL of this particular domain.
The urls might be:
example.com
example.com/
example.com/cell-phones-c_11293/
example.com/cell-phones/pp_617010.html?wid=4
The query parameter I want to add is ?lkid=12345678 (the same for every URL).
example.com?lkid=12345678
example.com/?lkid=12345678
example.com/cell-phones-c_11293/?lkid=12345678
example.com/cell-phones/pp_617010.html?wid=4&lkid=10823628
I want this functionality only for this particular page, not for every post on Wordpress, so I wouldn't like to add the code to the functions.php, or install a plugin just for this single page.
Is it possible to add it as a header or body script to the page through the "Scripts" screen option at the editor?
I have found the following code from this answer,
<script>
var $links = $('a'); // get all anchor tags
// loop through each anchor tag
$.each($links, function(index, item){
var url = $(this).attr('href'); // var for value of href attribute
// use regex to match your domain
var pattern = new RegExp(/(example.com\/)(.*)/i);
if(pattern.test(url))
$(this).attr('href', url + '?lkid=12345678');
// append ?lkid=12345678 if url contains example
});
</script>
And the following code from this answer
<script>
function updateQueryStringParameter(uri, key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
}
else {
return uri + separator + key + "=" + value;
}
}
</script>
...but I unfortunately I don't know enough programming to make them work for my case.