最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

permalinks - Automatically append ID to at the end of specific URLs

matteradmin5PV0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

This 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

Post a comment

comment list (0)

  1. No comments so far