[Drupal] How to display Affiliate Links on page load for the contents using Category as reference id?

| | 4 min read

In this article I will be explaining What is Affiliate Links and what all configurations should be done to generate affiliate links on page load. Also will explain how to generate titles of required patterns using preg_match(). In a related article we have explained how to use hook_form_alter() to make the Multiple select field a single select.

Before we start let me give a brief introduction on Affiliate Links. Affiliate Links are external links that leads to Services, Merchants or even Vendors. In these URLs, it contains a specific ID(like an Unique Identifier) to identify from where the link comes. This can be generated mainly two ways like

  • If we have just one affiliate link say Google Ads we can add the id to the URL.
  • If our site is a product site and we have multiple merchants, in this case its difficult to get id for each affiliate link. In this case there are a few site like Skim Links which provide a unique id for all those merchants we needed. Once we activate an account there, they will create js file for us using this Unique ID. Just add this in our pag.tpl.php file and when ever an affiliate link is created it will pass our id to the site

  <script type="text/javascript" src="source/filename.js"></script>

Now lets come back to our issue. In our site at first we were manually entering Affiliate links when we create each node. Now the problem here is that if a user has to change the URL format for an affiliate link say Amazon, then he has to manually enter all the nodes that has Affiliate Link : Amazon and change it. Instead of this we decided to create a URL pattern for each Affiliate content and call them on page load i.e, when we load the page, first we will identify the corresponding Affliate Category of this page and using category as reference will call the corresponding affiliate link created for the category and replace the title in the URL with the generated patterns. Will give you an example for this, consider the case of Amazon, URL Pattern we generated is "http://www.amazon.co.uk/s?url=search-alias%3Daps&field-keywords=[title-…]", now when we load the page we replace "[title-for-url-search]" with title in requested format which will be explained below.

The replacement patterns we generated for each node title is as follows

  • [title] - Touch of Power - Maria Snyder
  • [title-for-url-search] - touch+of+power+maria+snyder
  • [truncated-title] - touch of power

There are a few configuration changes to be done before we start,

  • Create two content type say "Event" and "Affiliate Links" path "admin/content/types/add".
  • Add the following CCK fields for the content type "Event" : Title, Body, Image etc.
  • Add the following CCK fields For the content type "Affiliate Links" :- Affiliate Name, Affiliate Button Image : field_affiliate_button : File and URL Template, URL Template : field_affiliate_url_template : Text.
  • Add a new taxonomy term "Affiliate Category" path "admin/content/taxonomy/add/vocabulary" and select Content types: "Event" and "Affiliate Links". Once we created this add the required terms to this.

Identify the required taxonomy terms and add all the terms in "Affiliate Category" say "Book", "Stage", etc. Now create contents for the content type "Affiliate Links" say "Amazon", "Ebay", etc you want to display in your site.

Consider a situation where for an Affliate Link different URL is required. For example, in the case of Ebay.co.uk we need different URLs for the Category "Book" and "Stage", in this case we add this as different contents.

Once we added the contents for the content type "Affiliate Links", add contents for the next content type "Events", when we add the content don't forget to give the correct Affiliate Category.

Now lets see how to convert the title to the required patterns as mentioned below

  • [title] - Touch of Power - Maria Snyder
  • [title-for-url-search] - touch+of+power+maria+snyder
  • [truncated-title] - touch of power


  /*Remove empty space after title*/
  $node->title=trim($node->title, "  ");
  /*Remove contents from title after comma*/
  $nodetitle = explode(', ', $node->title);
  $patterns='( - | )';
  $replacements='+';
  $string=$nodetitle[0];
  /*Replace '-' and ' ' with + using preg_replace*/
  $nodetitleset = preg_replace($patterns, $replacements, $string);
  /*will get the [title-for-url-search] pattern here*/
  $titleforurlsearch = drupal_strtolower($nodetitleset);
  $node->title=trim($node->title, "  ");
  /*Remove contents from title after '-'*/
  $truncatedtitle = explode('-', $node->title);
  $truncatedtitle=trim($truncatedtitle[0], "  ");
  /*will get the [truncated-title] pattern here*/
  $truncatedtitle = drupal_strtolower($truncatedtitle);

Hope you are clear in what we did here. The remaining part will be continued in the second part of this article.