I need to be able to automatically change normal links to affiliate links on my Wordpress website.
For example: when I add a GOG link, it should automatically append my partner ID at the end of this URL so it would look like that - GOG/my-affiliate-id.
Does anyone know how can I do that? I have hundreds of links on my website so I would like to make it as light as possible.
Thank you, Kacper
I need to be able to automatically change normal links to affiliate links on my Wordpress website.
For example: when I add a GOG link, it should automatically append my partner ID at the end of this URL so it would look like that - GOG/my-affiliate-id.
Does anyone know how can I do that? I have hundreds of links on my website so I would like to make it as light as possible.
Thank you, Kacper
Share Improve this question asked Mar 12, 2019 at 14:32 kacper3355kacper3355 772 silver badges9 bronze badges 3- Add the link to where? How would we know if the link is actually an affiliate link? You don't want to append the ID to just any links, do you? – Sally CJ Commented Mar 12, 2019 at 16:14
- Add the link in normal WordPress post. I'd like to specify some sites in the code and add specific ID to each. – kacper3355 Commented Mar 12, 2019 at 16:23
- For example - when I add post containing example, my partner ID should be automatically added to the very end of this url – kacper3355 Commented Mar 12, 2019 at 16:24
1 Answer
Reset to default 1This is very simple to achieve with JavaScript. Place this at the bottom of your page, just before </body>
tag.
This will append ?pp=my-affiliate-id
at the end of your links. Just change the variable aid
value below:
<script>
// Change "my-affiliate-id" below to your actual affiliate id
const aid = 'my-affiliate-id';
// Append slash with affiliate id, only if an affiliate ID is not found in the link yet
const goglinks = document.querySelectorAll('a[href*="gog"]');
goglinks.forEach(function(el) {
if(!el.href.includes('pp=')) {
el.href = el.href.replace(/\?.*$/, '') + '?pp=' + aid
}
})
</script>
Demo: https://jsfiddle/samliew/ks3y8059