Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Need a better affiliate url function WordPress

  • SOLVED

I need to make a function that edits a URL

I have affiliates which I add a prefix (mostly) and sometimes suffix to the URL for certain domain names

domain names (could be http or https and with or without www)
then if that domain name prefix a certain string (default prefix but be able to specify to suffix)

This is the way I am doing it now but I think I could structure it so to allow me to default as prefix to url but allow to suffix url and also allow for http and https and www and non www



function affiliate($url) {

//Coggles
if (stripos($url,'http://www.coggles.com') !== false) {
$url = str_replace('http://www.coggles.com', 'http://www.awin1.com/cread.php?awinmid=4318&awinaffid=174073&clickref=&p=http://www.coggles.com', $url);
}
//Coastal
if (stripos($url,'http://www.coastal.com') !== false) {
$url = str_replace('http://www.coastal.com', 'http://click.linksynergy.com/deeplink?id=Z77QPydcorE&mid=24395&murl=http://www.coastal.com', $url);
}

return $url;


}


Answers (2)

2014-08-22

Martin Pham answers:

Hi Katie,
Please try this function

function affiliate($url) {

$domain = parse_url($url, PHP_URL_HOST);

if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $match)) {
$domain = $match['domain'];
}

if( $domain === 'coggles.com' ) {
$affiliate_link = 'http://www.awin1.com/cread.php?awinmid=4318&awinaffid=174073&clickref=&p=';
} elseif($domain === 'coastal.com') {
$affiliate_link = 'http://click.linksynergy.com/deeplink?id=Z77QPydcorE&mid=24395&murl=';
} else {
$affiliate_link = '';
}

return $affiliate_link.$url;
}


Katie comments:

Thanks martin im reading this on my phone but will try it out as soon as I get up this is the kind of function I was looking for


Katie comments:

yes it works thank you martin

2014-08-22

timDesain Nanang answers:

do you want the function to
- replaced $url in the content automatically or
- as shortcode or
- use affiliate() manually
?


have you tried these plugins:
https://wordpress.org/plugins/simple-link-cloaker/
https://wordpress.org/plugins/wp-auto-affiliate-links/
https://wordpress.org/plugins/thirstyaffiliates/
https://wordpress.org/plugins/pretty-link/
https://wordpress.org/plugins/affilinker/
or
https://wordpress.org/plugins/tags/affiliate


Katie comments:

im gonna use affiliate manually


Katie comments:

affiliate() I mean

I think I can do what I need without a plugin but I will check them out


timDesain Nanang comments:

would you like to provide each sample for prefix and suffix aff link?


Katie comments:

Hi tim im not sure I understand the question, there are a bunch of diffferent links but they all follow the pattern of either attaching either a prefix to the url or a suffix (like an amazon link)

What i am using right now is "working" but i thought there might be a little better way to tweak what i was doing