A simple affiliate system for your own website

With WP-Wartung24 I offer various technical services for WordPress. It happens again and again that other WordPress service providers and web designers want to recommend my services. To make this easier, I wanted to have my own small affiliate system. In this post I explain how I did it.

preliminary considerations

Although I offer some flat-rate services, it is not always possible to order my services online. In many cases it makes sense to contact us before placing an order. For this reason, classic affiliate programs that rely on certain order volumes or the like cannot be used. So an independent solution had to be found, with which I would be informed when a prospective customer contacted me that he came to me on a recommendation. It should also be easy for the recommender to recommend my services.

desired system

In principle, the desired system is quite simple:

  • the recommender should be able to link any page of my website
  • If a contact is made, I would like to know where the recommendation came from

It seems to make sense that the recommender adds a URL parameter to his links to my offer. Let’s assume that the fictitious service provider WP agency is involved, then the links could look like this:

www.wp-wartung24.de/?ref=wp-agentur

The URL parameter can be appended to any URL on the page.

First step: Read URL parameters

Since a visitor can stay on different subpages of a website, the information of the URL parameter must be retained for a longer period of time, which is why I decided to use a cookie.

I use a plugin to read the URL parameter and save it in a cookie:

The plugin is very simple. Nevertheless, you could of course achieve the whole thing yourself with a few lines of code in the child theme or custom plugin. For the sake of convenience, I decided to use the plugin.

The URL parameters to be checked and the cookie lifetime are defined in the plugin settings. The cookie has the same name as the URL parameter. In my example, the cookie has a lifetime of 3 days.

Thus, based on the URL parameter, we have now saved a cookie with the desired information. Now the info just has to get to me.

Second step: contact and order forms

There are various forms on my website. On the one hand simple contact forms, on the other hand “real” order processes, which are also realized via forms. In all such cases, the affiliate information shall be transmitted.

I use TorroForms for my forms, a fairly new FormBuilder plugin from the German community. A special feature of the plugin is that you can intervene in many places and make adjustments with a few lines of your own code. The associated API is well documented.

It doesn’t really matter how the forms are structured at the end. That’s why I’ll spare myself the details here… 😉

Third step: How does the cookie info get into the form?

This is actually the crucial step. And also the step on which I tinkered the longest. The CookieMonster plugin creates a separate shortcode for each saved URL parameter, which you can then use on your own pages. So in my case that would be a

[[ref]]

I would have to include this code in the notification email of the TorroForms form. So the post could end here. But it isn’t, because unfortunately TorroForms shortcodes are not evaluated when generating the notification emails. There are also many hooks in TorroForms to retrofit your own functionality. Not exactly at this point, however.

After consulting Sven Wagener, the author of the plugin, he recommended that I define my own template tag for TorroForms. These template tags are used to customize the notification emails and to integrate the available field data.

Since I’m working with a child theme, I was able to integrate the template tag extension directly into my theme. Alternatively, the code belongs in a custom plugin. The code then looks like this:


<?php
/**
 * Core: Torro_TemplateTags_WPW24 class
 */

if ( ! defined( 'ABSPATH' ) ) {
 exit;
}

/**
 * Torro Forms template tags for WPW24 handler class
 *
 * Handles template tags for WPW24.
 *
 */
final class Torro_Templatetags_WPW24 extends Torro_TemplateTags {
 /**
 * Instance
 *
 * @var null|Torro_Templatetags_WPW24
 */
 private static $instance = null;

public static function instance() {
 if ( null === self::$instance ) {
 self::$instance = new self();
 }
 return self::$instance;
 }

protected function init() {
 $this->title = __( 'WPW24', 'torro-forms' );
 $this->name="WPW24";
 $this->description = __( 'WPW24 Templatetags', 'torro-forms' );
 }

/**
 * Adding all tags of class
 */
 public function tags() {
 $this->add_tag( 'referral', __( 'Referral', 'torro-forms' ), __( 'Adds the referral cookie content.', 'torro-forms' ), array( $this, 'referral' ) );
 }

/**
 * %sitename%
 */
 public function referral() {
 if ( !isset($_COOKIE["ref"])) {
 return '-';
 }
 return $_COOKIE["ref"];
 }
}

torro()->templatetags()->register( 'Torro_Templatetags_WPW24' );


Essentially, a new class is defined for template tags, which then creates just a single tag called referral. The associated function then defines what is to be output, namely the cookie content.

I packed the definition in a torroforms.php and then simply added it to my functions.php.


/*
 * Add special TorroForms template tags
 */
require_once('torroforms.php');

As a result of these efforts, the template tags list now shows another set of WPW24-specific tags.

Conclusion

That was it already. If a referral parameter was set, this is now transmitted in all forms in which I have integrated the template tag shown above. So I always know who recommended the interested party.

Of course, the system has disadvantages: the affiliates do not have their own overview of possible leads and orders. However, the system is only intended for colleagues and partners with whom I am in constant contact anyway. So the relationship of trust is there anyway. And outsiders can send me their ID as often as they like, this does not automatically result in an affiliate claim. 😉

I am very satisfied to have realized the desired function with very simple means.

How would you have solved that? I welcome suggestions in the comments.