最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论