We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 2762
    • 1,198 Posts
    I have been looking for a snippet to cloak links, not finding him, I decided to create it smiley
    This snippet is based on this article:
    http://ctrtard.com/affiliate-marketing/better-affiliate-link-cloaking-for-seo/

    Adapted to work in the modx way
    Demo and istructions
    http://www.tattoocms.it/extras/snippets/cloaklinks.html

    Git
    https://github.com/Nicola1971/CloakLinks



    :D



      Free MODx Graphic resources and Templates www.tattoocms.it
      -----------------------------------------------------

      MODx IT  www.modx.it
      -----------------------------------------------------

      bubuna.com - Web & Multimedia Design
      • 13226
      • 953 Posts
      The "CloakLinksRedirect" snippet:

      Does the following suffice for the landing page
      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="[(modx_charset)]">
      <title>[*pagetitle*]</title>
      <meta name="robots" content="noindex, nofollow">
      </head>
      <body>
      [[CloakLinksRedirect]]
      </body>
      </html>

      Should the "CloakLinksRedirect" snippet be called cached ([[]]) or uncached ([!!]) ?

      What are the settings for the landing page, does the page need to be cached, uncached, searchable etc. ?

      The "CloakLinks" snippet:

      In the url there is name=\"$NameAttribute\" - this attribute isn't pre-defined in the snippet, is it a left-over ?

      I have slightly modified the copy I have:
      $textlink = isset ($textlink) ? $textlink : "Go to the website";
      $linkelements = isset ($linkelements) ? $linkelements : "";
      $targetlink = isset ($targetlink) ? $targetlink : "_blank";
      $landing = isset ($landing) ? $landing : "[*id*]";
      $landingurl = ($modx->makeUrl($landing, '', '', 'full'));
      $LinkWeb = isset($LinkWeb) ? $LinkWeb : '';
      $LinkTv = isset($LinkTv) ? $LinkTv : '';
      $ErrorEmptyLink = isset($ErrorEmptyLink) ? $ErrorEmptyLink : "Empty Link";
      $urlparam = isset($urlparam) ? $urlparam : "goto";
      
      if($LinkTv == ""){
      $url = $LinkWeb;
      } else {
      $Link = $modx->getTemplateVarOutput(''.$LinkTv.'',$id);
      $url = $Link[''.$LinkTv.''];
      }
      
      $url = base64_encode($url);
      $url = urlencode($url);
      
      $linkurl = "<a $linkelements target=\"$targetlink\" rel=\"nofollow\" href=\"$landingurl&$urlparam=$url\">$textlink</a>";
      
      if($LinkTv == "" && $LinkWeb == ""){
      echo $ErrorEmptyLink;
      } else {
      echo $linkurl;
      }

      You can see from my copy that I have added the "linkelements" parameter, this allows the addition of extras like class, title etc. to be added directly in the call e.g.
      [[CloakLinks? &urlparam=`` &landing=`` &LinkTv=`` &textlink=`` &linkelements=`class="" title="" id="" name=""`]]

      My Problem:

      I have various affiliate links in my site from a variety of different websites, so I use the main affiliate link plus a chunk with my personal affiliate link code per website e.g:
      domain.com/folder/page.html{{affiliatecode}}

      The chunk is called via a TV and then built together in the page, my call in the page looks like this:
      href="[*affiliateurl*][*affiliatecode*]"

      If I try to call this via CloakLinks the page crashes:
      [[CloakLinks? &urlparam=`read` &landing=`10` &LinkTv=`affiliateurl,affiliatecode` &textlink=`Read More` &linkelements=`class="external" title="External Link"`]]

      I am not a programmer and have no idea where I should / would have to start to get this sorted, so any help is much appreciated.
        • 2762
        • 1,198 Posts
        Quote from: iusemodx at Dec 22, 2014, 08:38 AM
        The "CloakLinksRedirect" snippet:

        Does the following suffice for the landing page
        
        
        <meta charset="[(modx_charset)]">
        <title>[*pagetitle*]</title>
        <meta name="robots" content="noindex, nofollow">
        
        
        [[CloakLinksRedirect]]
        
        

        Yes, should work in a blank template with your code.



        Should the "CloakLinksRedirect" snippet be called cached ([[]]) or uncached ([!!]) ?

        What are the settings for the landing page, does the page need to be cached, uncached, searchable etc. ?


        Uncached [!CloakLinksRedirect!] snippet with cached landing page works for me.



        The "CloakLinks" snippet:

        In the url there is name=\"$NameAttribute\" - this attribute isn't pre-defined in the snippet, is it a left-over ?

        Sorry, it's a left-over.
        Name attribute is no more supported in HTML5
        removed in 1.0.1


        I have slightly modified the copy I have:
        $textlink = isset ($textlink) ? $textlink : "Go to the website";
        $linkelements = isset ($linkelements) ? $linkelements : "";
        $targetlink = isset ($targetlink) ? $targetlink : "_blank";
        $landing = isset ($landing) ? $landing : "[*id*]";
        $landingurl = ($modx->makeUrl($landing, '', '', 'full'));
        $LinkWeb = isset($LinkWeb) ? $LinkWeb : '';
        $LinkTv = isset($LinkTv) ? $LinkTv : '';
        $ErrorEmptyLink = isset($ErrorEmptyLink) ? $ErrorEmptyLink : "Empty Link";
        $urlparam = isset($urlparam) ? $urlparam : "goto";
        
        if($LinkTv == ""){
        $url = $LinkWeb;
        } else {
        $Link = $modx->getTemplateVarOutput(''.$LinkTv.'',$id);
        $url = $Link[''.$LinkTv.''];
        }
        
        $url = base64_encode($url);
        $url = urlencode($url);
        
        $linkurl = "<a $linkelements="" target="\"$targetlink\"" rel="\"nofollow\"" href="\"$landingurl&$urlparam=$url\"">$textlink</a>";
        
        if($LinkTv == "" && $LinkWeb == ""){
        echo $ErrorEmptyLink;
        } else {
        echo $linkurl;
        }

        You can see from my copy that I have added the "linkelements" parameter, this allows the addition of extras like class, title etc. to be added directly in the call e.g.
        [[CloakLinks? &urlparam=`` &landing=`` &LinkTv=`` &textlink=`` &linkelements=`class="" title="" id="" name=""`]]


        Thank you - Good suggestion - added in 1.0.1

        My Problem:

        I have various affiliate links in my site from a variety of different websites, so I use the main affiliate link plus a chunk with my personal affiliate link code per website e.g:
        domain.com/folder/page.html{{affiliatecode}}

        The chunk is called via a TV and then built together in the page, my call in the page looks like this:
        href="[*affiliateurl*][*affiliatecode*]"

        If I try to call this via CloakLinks the page crashes:
        [[CloakLinks? &urlparam=`read` &landing=`10` &LinkTv=`affiliateurl,affiliatecode` &textlink=`Read More` &linkelements=`class="external" title="External Link"`]]

        I am not a programmer and have no idea where I should / would have to start to get this sorted, so any help is much appreciated.

        The snippet does not support two template variables, but we could try to add a new parameter like "LinkSuffixTv" smiley
          Free MODx Graphic resources and Templates www.tattoocms.it
          -----------------------------------------------------

          MODx IT  www.modx.it
          -----------------------------------------------------

          bubuna.com - Web & Multimedia Design
          • 13226
          • 953 Posts
          @Nicola - thanks for the feedback.

          The "CloakLinksRedirect" snippet:

          I have modified my template to suit the need for having more than one url parameter.

          I now create a cached page for each url parameter and the template uses the alias as the parameter. The snippet is called uncached as you suggested.

          Template code:
          <!DOCTYPE html>
          <html>
          <head>
          <meta charset="[(modx_charset)]">
          <title>[*pagetitle*]</title>
          <meta name="robots" content="noindex, nofollow">
          </head>
          <body>
          [!CloakLinksRedirect? &urlparam=`[*alias*]`!]
          </body>
          </html>

          So the link to the redirect can be anything you choose it to be, so long as a page per paramaeter is available e.g:
          [[CloakLinks? &urlparam=`test1` &landing=`10` &LinkTv=``]]
          [[CloakLinks? &urlparam=`test2` &landing=`11` &LinkTv=``]]
          [[CloakLinks? &urlparam=`test3` &landing=`12` &LinkTv=``]]


          The "CloakLinks" snippet:

          I have further modified the snippet after reading the post that inspired the snippet. Currently there is no attribute to add "Dummy" parameters, so I added one = $dummyparams

          $textlink = isset ($textlink) ? $textlink : "Go to the website";
          $linkelements = isset ($linkelements) ? $linkelements : "";
          $dummyparams = isset ($dummyparams) ? $dummyparams : "";
          $targetlink = isset ($targetlink) ? $targetlink : "_blank";
          $landing = isset ($landing) ? $landing : "[*id*]";
          $landingurl = ($modx->makeUrl($landing, '', '', 'full'));
          $LinkWeb = isset($LinkWeb) ? $LinkWeb : '';
          $LinkTv = isset($LinkTv) ? $LinkTv : '';
          $ErrorEmptyLink = isset($ErrorEmptyLink) ? $ErrorEmptyLink : "Empty Link";
          $urlparam = isset($urlparam) ? $urlparam : "goto";
           
          if($LinkTv == ""){
          $url = $LinkWeb;
          } else {
          $Link = $modx->getTemplateVarOutput(''.$LinkTv.'',$id);
          $url = $Link[''.$LinkTv.''];
          }
           
          $url = base64_encode($url);
          $url = urlencode($url);
           
          $linkurl = "<a $linkelements target=\"$targetlink\" rel=\"nofollow\" href=\"$landingurl$dummyparams&$urlparam=$url\">$textlink</a>";
           
          if($LinkTv == "" && $LinkWeb == ""){
          echo $ErrorEmptyLink;
          } else {
          echo $linkurl;
          }


          My call now look like:
          [[CloakLinks? &urlparam=`` &dummyparams=`&pid=[*id*]&alias=[*alias*]` &landing=`` &LinkTv=`` &textlink=`` &linkelements=`class="" title="" id="" name=""`]]



          Adding a further parameter (LinkSuffixTv) would be excellent, are you able to do that yourself ?
            • 2762
            • 1,198 Posts
            Added AffiliateSuffix parameter:

            
            // CloakLinks 1.0.2
            // author: by Nicola Lambathakis
            // credits: http://ctrtard.com/affiliate-marketing/better-affiliate-link-cloaking-for-seo/
            //
            // method 1: using a template variable [!CloakLinks? &landing=`49` &textlink=`site preview` &LinkTv=`AffiliateLink`!]
            // method 1 + custom url parameter [!CloakLinks? &landing=`49` &urlparam=`visit` &textlink=`site preview` &LinkTv=`AffiliateLink`!]
            // method 2 [!CloakLinks? &landing=`49` &textlink=`site preview` &LinkWeb=`http://modx.com/`!]
            //
            // Parameters:
            // &textlink = text placeholder | default value: "Go to the website"
            // &targetlink = href target attribute | default value: "_blank"
            // &landing = resource id of the landing page for redirect (must contain a [[CloakLinksRedirect]] snippet call)
            // &LinkTv = (method 1) name of the Text template variable containing the affiliate link
            // &LinkWeb = (method 2) full affiliate link
            // &AffiliateSuffix = link suffix for affiliate code (ie: 123456789 or [*affiliatecodetv*]) 
            // &urlparam = custom parameter url | default value: "goto"
            // &linkelements = allows the addition of extras like class, title (credits: iusemodx)
            // &dummyparams = "Dummy" parameters (credits: iusemodx)
            // &ErrorEmptyLink = text placeholder for empty tv and/or missing linkweb parameter | default value: "Empty Link"
            
            $textlink = isset ($textlink) ? $textlink : "Go to the website";
            $linkelements = isset ($linkelements) ? $linkelements : "";
            $dummyparams = isset ($dummyparams) ? $dummyparams : "";
            $targetlink = isset ($targetlink) ? $targetlink : "_blank";
            $landing = isset ($landing) ? $landing : "[*id*]";
            $landingurl = ($modx->makeUrl($landing, '', '', 'full'));
            $LinkWeb = isset($LinkWeb) ? $LinkWeb : '';
            $LinkTv = isset($LinkTv) ? $LinkTv : '';
            $AffiliateSuffix = isset($AffiliateSuffix) ? $AffiliateSuffix : '';
            $ErrorEmptyLink = isset($ErrorEmptyLink) ? $ErrorEmptyLink : "Empty Link";
            $urlparam = isset($urlparam) ? $urlparam : "goto";
            
              
            if($LinkTv == ""){
            $url = $LinkWeb.$AffiliateSuffix;
            } else {
            $Link = $modx->getTemplateVarOutput(''.$LinkTv.'',$id);
            $url = $Link[''.$LinkTv.''].$AffiliateSuffix;
            }
              
            $url = base64_encode($url);
            $url = urlencode($url);
              
            $linkurl = "<a $linkelements target=\"$targetlink\" rel=\"nofollow\" href=\"$landingurl$dummyparams&$urlparam=$url\">$textlink</a>";
              
            if($LinkTv == "" && $LinkWeb == ""){
            echo $ErrorEmptyLink;
            } else {
            echo $linkurl;
            }
            
            


            Example call:

            [[CloakLinks? &landing=`148` &dummyparams=`&pid=[*id*]&alias=[*alias*]` &textlink=`link to github` &AffiliateSuffix=`/Nicola1971` &LinkWeb=`https://github.com`]]



            Check the "GitHub" example at
            http://www.tattoocms.it/extras/snippets/cloaklinks.html
            (bottom of the page)

            I used "/Nicola1971" as affiliate suffix smiley [ed. note: banzai last edited this post 11 years, 9 months ago.]
              Free MODx Graphic resources and Templates www.tattoocms.it
              -----------------------------------------------------

              MODx IT  www.modx.it
              -----------------------------------------------------

              bubuna.com - Web & Multimedia Design
              • 13226
              • 953 Posts
              Great work.

              I have updated my copy of the snippet and have tested a variety of options to use in the "AffiliateSuffix":

              • TV with text input
              • DropDown TV with input option values e.g. Link1==?mylink1||Link2==?mylink2
              • DropDown TV with @Chunk binding for the values
              • Standard Chunk - {{mylink1}} chunk content = ?mylink1
              • Chunk containing a DropDown TV which has @Chunk binding

              All of the above solutions work without causing any problems.
                • 13226
                • 953 Posts
                UPDATE:

                After checking through a variety of the links I now have on the site, about 20% don't work, they give a 404 error page not found.

                The common denominator within the URL's that don't work is that they have == at the end of the URL, all other links have either one = or no = at the end.

                Can this have something to do with the encoding ?


                I have just checked this again and compared what is shown in the error page and what is in the browsers address bar.

                Error page has: NDg/dGVzdA==
                Address bar has: NDg%2FdGVzdA%3D%3D

                So the page won't show as the URL is different, the special characters have been changed

                What seems to be happening is when the page with the link is renderred in the browser the URLs special characters are being changed, so the link itself changes and no-longer works.

                This is a major problem
                  • 2762
                  • 1,198 Posts
                  Can't replicate this issue.
                  Special characters "URL encoding" should not affect the snippet.

                  I have 404 error only when url tv does not contain "http://"
                    Free MODx Graphic resources and Templates www.tattoocms.it
                    -----------------------------------------------------

                    MODx IT  www.modx.it
                    -----------------------------------------------------

                    bubuna.com - Web & Multimedia Design
                    • 13226
                    • 953 Posts
                    My Call:
                    [[CloakLinks? &urlparam=`view` &landing=`10` &LinkTv=`affiliate-url` &AffiliateSuffix=`[*affiliate-suffix*]` &linkelements=`class="external"` &textlink=`View Item`]]


                    Affiliate URL: http:// themeforest.net/item/colors-modx-creative-theme/7246895
                    Affiliate Suffix: ?test123

                    I have tried this with a text TV and a URL TV - no difference.

                    The rendered HREF HTML is:
                    &goto=aHR0cDovL3RoZW1lZm9yZXN0Lm5ldC9pdGVtL2NvbG9ycy1tb2R4LWNyZWF0aXZlLXRoZW1lLzcyNDY4OTU%2FdGVzdDEyMw%3D%3D

                    It should be:
                    &goto=aHR0cDovL3RoZW1lZm9yZXN0Lm5ldC9pdGVtL2NvbG9ycy1tb2R4LWNyZWF0aXZlLXRoZW1lLzcyNDY4OTU/dGVzdDEyMw==
                    [ed. note: iusemodx last edited this post 11 years, 8 months ago.]
                      • 2762
                      • 1,198 Posts
                      Ok tested your call.
                      Seems related to the question mark (?) in AffiliateSuffix


                      Try "/comments" as AffiliateSuffix - works for me and redirect to the comment page (http:// themeforest.net/item/colors-modx-creative-theme/7246895/comments) [ed. note: iusemodx last edited this post 11 years, 8 months ago.]
                        Free MODx Graphic resources and Templates www.tattoocms.it
                        -----------------------------------------------------

                        MODx IT  www.modx.it
                        -----------------------------------------------------

                        bubuna.com - Web & Multimedia Design