I create mousemade pixel perfection stuffs for the interwebs.

Published: 7 years ago

Drupal Affiliate Link cloaking/redirection module

The below module provides an easy way to redirect/cloak your affiliate links through Drupal.

cloaker.info

[php]
name = Drupal Link Cloaker/Redirector
description = Provides affiliate link redirection.
core = 6.x
version = 1.0
[/php]

cloaker.module

[php]
<?php
/*
* Implementation of hook_perm()
*/
function tdt_helper_perm() {
return array( ‘administer affiliate link cloaking’);
}
/*
* Implementation of hook_menu
*/
function tdt_helper_menu() {
$items[‘goto/%’] = array(
‘title’ => ‘Redirecting…’,
‘page callback’ => ‘tdt_helper_aff_redirect’,
‘page arguments’ => array(1),
‘access arguments’ => array(‘access content’),
‘type’ => MENU_CALLBACK,
);
$items[‘admin/settings/cloak’] = array(
‘title’ => ‘Link cloaking settings’,
‘description’ => t(‘You can set affiliate links and associated keywords here.’),
‘page callback’ => ‘tdt_helper_aff_settings’,
‘access arguments’ => array(‘administer affiliate link cloaking’),
);
return $items;
}

/*
* Admin affiliate link settings page
*/
function tdt_helper_aff_settings() {
$output = t(‘You can set affiliate links and associated keywords here.’);
$output .= drupal_get_form(‘tdt_helper_aff_settings_form’);
return $output;
}

/*
* Affiliate link settings form
*/
function tdt_helper_aff_settings_form() {
$form[‘links_csv’] = array(
‘#title’ => t(‘Affiliate Links’),
‘#description’ => t(‘Enter affiliate program names &amp; affiliate links, one pair per line. Name &amp; Link should be seperated by coma.’),
‘#type’ => ‘textarea’,
‘#default_value’ => variable_get(‘aff_data’,”),
);
$form[‘submit’] = array(
‘#type’ => ‘submit’,
‘#value’ => ‘Save’,
);
return $form;
}
/*
* Parse Input
*/
function tdt_helper_aff_settings_parse($str) {
$delimiter = ‘,’;
$lines = array_values(array_filter(explode("\r\n", $str)));
foreach ($lines as $line) {
if(strlen(trim($line))>; 0) {
list ($k,$v) = explode (‘,’,$line);
$pairs[$k] = $v;
}
}
return $pairs;
}
function tdt_helper_aff_settings_form_submit($form_id, $form_state) {
variable_set(‘aff_data’, $form_state[‘values’][‘links_csv’]);
drupal_set_message(t(‘Your Settings Saved.’));
}
/*
* Redirect to affiliate links;
*/
function tdt_helper_aff_redirect($aff) {
$r = tdt_helper_aff_settings_parse(variable_get(‘aff_data’,”));
if(array_key_exists($aff,$r)) {
header("Location: $r[$aff]");
} else {
drupal_set_header("HTTP/1.0 404 Not Found");
}
}

?>
[/php]

To add affiliate links, go to admin/settings/cloak, and enter pairs of link keyword & links. To link to your affiliate program, use something like “goto/yourkeyword”, which will redirect to the corresponding link.

Ex.

Values to be entered on settings page;

[sourcecode language=”plain”]
google, http://google.com
youtube, http://youtube.com
[/sourcecode]

Ex. link from an article;

[php light=”true”]<a href="goto/google">Go to google</a>[/php]

.

Hope it helps.

Thank you!

Leave a Reply

Some HTML is OK