How To Automatically Add Affiliate Links To WordPress or bbPress Posts

I’ve been working on a WordPress site with bbPress installed (well, BuddyBoss to be precise) & one of the requests was for all of the links posted in the bbPress forum to automatically turn to affiliate links.

Is it possible?

You betcha… In fact, I’ve actually done it before on another site in the past (though that was just a phpBB forum).

All you need to do is simply add a filter to your theme’s functions.php file, like so:


// Function to force links through affiliate linker
add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );

function auto_affiliate_url($a){
$supplied_url = $a['2'];
$encoded_supplied_url = urlencode($supplied_url);
// Determine if URL already contains querystring for appending affiliate IDs
if (strpos($supplied_url, '?') !== false) {
$appender = "&";
} else {
$appender = "?";
}
// Add HTTP protocol if missing so that we can parse the URL
$detecthttp = (parse_url($supplied_url));
if ($detecthttp['scheme'] == "") {
$url_with_protocol = "https://".$supplied_url."";
} else {
$url_with_protocol = $supplied_url;
}

$full_domain = parse_url($url_with_protocol, PHP_URL_HOST);
$domain_no_www = str_replace('www.', '', $full_domain);

if ($domain_no_www == "google.com") {
$final_url = "affiliate link";
} else if ($domain_no_www == "amazon.com") {
$final_url = affiliate link";
} else {
$final_url = $supplied_url;
}

return '<a'.$a['1'].'href="'.$final_url.'"'.$a['3'].'>';
}

function filter_the_content_in_the_main_loop( $content ) {

$content = preg_replace_callback('/<a(.*?)href="(.*?)"(.*?)>/', 'auto_affiliate_url', $content);

    return $content;
}

That particular bit of code will scan through all of the links in a WordPress post & automatically switch them to be affiliate links if they match the URLs that you’ve defined in the function (which are set to google.com & amazon.com for the example above).

All you need to do is simply replace “affiliate link” with your actual affiliate ID appended to $supplied_url.

Example:


$final_url = "".$supplied_url."".$appender."id=1234";

And if you want to use it in bbPress or BuddyBoss content, just need to change the hooks like so:


// Function to force BuddyBoss links through affiliate linker
add_filter( 'bbp_get_topic_content', 'filter_the_content_in_the_main_loop' );
add_filter( 'bbp_get_reply_content', 'filter_the_content_in_the_main_loop' );

And it’s all handled server side which means that there’s no need for additional JavaScript on the page. Sure, it may slow the DB down on the initial request but just make sure that you have caching enabled & you should be OK.

I tested it on a site with 400,000+ posts and no caching and it worked just fine.

Oh, and since there’s no (easy) “perfect” way to determine base domains due to the sheer number of variances, as a failsafe if it can’t figure out which link to use it’ll just leave the normal link in place in the post.

Hope it helps you! 🙂

Leave a Comment