【问题标题】:Automatically prepend affiliate identifier to URLs自动将附属标识符添加到 URL
【发布时间】:2021-09-21 12:49:12
【问题描述】:

我想自动为我网站上的 URL 添加会员标识符。 例如,我有外部 URL,例如:

https://www.adairs.com.au/

https://bedthreads.com.au/products/caitlin-robson-footed-bowl

我的会员标识符因零售商而异,例如:

adairs.com.au: https://t.example.com/12345/t/54321?Url=

bedthreads.com.au: https://t.example.com/12121/t/21212?Url=

我需要在点击形成时将这些自动添加到外部 URL 的开头:

https://t.example.com/12345/t/54321?Url=https://www.adairs.com.au/

https://t.example.com/12121/t/21212?Url=https://bedthreads.com.au/products/caitlin-robson-footed-bowl

我找到的最接近的解决方案是here,但是我在 java 方面不是很有经验并且无法使其工作。理想情况下,我还想在未来为其他零售店添加其他自动会员标识符。

另外,是否可以在 PHP 中添加它? 任何帮助将不胜感激。谢谢。

    // attach a click even to all <a> elements
    $("a").click(function() {
        addAffiliate(this);
    });
    
    // 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
    function addAffiliate(link) {
        // make sure this link is not to the current site and does not contain the affiliateURL
        if ((link.href).indexOf(adairs.com.au) < 0 && (link.href).indexOf(adairsaffURL) < 0){
            // update the link with the affiliateURL, the url encoded link, and the additional query string
            link.href = affiliateURL + escape(link.href);
        
}else if((link.href).indexOf(bedthreads.com.au) < 0 && (link.href).indexOf(bedthreadsaffURL) < 0){
                link.href = bedthreadsaffURL + escape(link.href);  
        }
        alert(link.href);
        // return true to follow the link
        return true;
    }

【问题讨论】:

    标签: javascript php prepend affiliate


    【解决方案1】:

    按照您的想法,我更新了您的代码以使其正常工作。请Run code snippet观看演示。

    您只需要使用.indexOf('adairs.com.au') 来检查链接是否属于联盟计划,因此您将其附加到affiliate URL

    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) {
        
        if (link.hash.substr(1).length > 0) return true;
        var redirectUrl = link.href;
        if (!redirectUrl || !isValidURL(redirectUrl)) return true;
        
        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);
      });
    
      function isValidURL(string) {
        var res = string.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
        return (res !== null)
      };
    
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <a href="https://www.adairs.com.au/">test adairs</a>
    <br/><br/>
    <a href="https://bedthreads.com.au/products/caitlin-robson-footed-bowl">test bed threads</a>
    <br/><br/>
    <a href="http://www.google.com">test not match adairs or bedthreads</a>
    <br/><br/>
    <a href="#dsdsd">test no link</a>

    更新: 功能添加到 Wordpress。将以下内容放入主题中的functions.php中。

    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) {
            
            if (link.hash.substr(1).length > 0) return true;
            var redirectUrl = link.href;
            if (!redirectUrl || !isValidURL(redirectUrl)) return true;
    
            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);
          });
    
          function isValidURL(string) {
            var res = string.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
            return (res !== null)
      };
    
    
        });
    </script>
    
    <?php
    }
    

    【讨论】:

    • 谢谢,该代码看起来应该可以工作。我已经尝试将它实施到我的网站中,但不幸的是没有运气。
    • 我尝试使用 将它添加到 footer.php 以及: add_action('wp_footer', 'auto_affiliate_url'); function auto_affiliate_url() { ?> // 此处为 javscript 代码
    • @KaiH 我更新了添加到wordpress的功能,请把它放到你主题文件夹中的functions.php中。
    • 现在运行良好。感谢您的帮助,非常感谢!
    • 很抱歉打扰您,但我遇到了一个问题,即在执行此代码后我的弹出窗口不再打开。我正在使用 WordPress/Elementor。知道如何避免这个问题吗?
    猜你喜欢
    • 2010-10-17
    • 1970-01-01
    • 2018-11-02
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多