Last active
November 6, 2022 11:05
-
-
Save EricBusch/93d5108a5aaecd54d3cc7327342ba845 to your computer and use it in GitHub Desktop.
Replace affiliate links with links to VigLink.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Replace affiliate links with links to VigLink. | |
* | |
* There are limitations to this method. Not all products contain the necessary data | |
* to redirect to VigLink. If the necessary data is not available, the affiliate network | |
* affiliate link will be returned instead. Therefore it's a good idea to enter your | |
* affiliate ID for all networks just in case here WordPress Admin Area >Datafeedr API > Networks | |
* | |
* This works best with most (but not all) products from the following affiliate networks: | |
* | |
* AdRecord, Adtraction, Affiliate4You, AffiliateWindow, APD, Belboon, BettyMills, ClixGalore, | |
* Commission Junction, Double.net, Impact Radius, LinkConnector, LinkShare (Rakuten), Optimise, | |
* Partner-ads, PepperJam, TradeDoubler, TradeTracker, Webgains and Zanox. | |
* | |
* To use this code you must add your VigLink API key at the top of this function. | |
* | |
* Replace 88ac2a40e081e283ac504d1789d398ac with your VigLink API key. | |
* | |
* Your VigLink API key can be found on your account page: https://publishers.viglink.com/account | |
* | |
* @param string $url Description. | |
* @param array $product An array of the current Datafeedr product's information. | |
* @param string $affiliate_id Affiliate ID for current product. | |
* | |
* @return string URL. | |
* | |
*/ | |
add_filter( 'dfrapi_before_affiliate_id_insertion', 'mycode_generate_viglink_link', 20, 3 ); | |
function mycode_generate_viglink_link( $url, $product, $affiliate_id ) { | |
/** | |
* Add your VigLink API key here. | |
* Your VigLink API key can be found on your account page: | |
* @link https://publishers.viglink.com/account | |
* Replace 88ac2a40e081e283ac504d1789d398ac with your unique VigLink API key. | |
*/ | |
$viglink_key = '88ac2a40e081e283ac504d1789d398ac'; | |
/* Stop Editing */ | |
$viglink_url = 'http://redirect.viglink.com/'; | |
$key = 'direct_url'; | |
if ( ! isset( $product[ $key ] ) ) { | |
return $url; | |
} | |
$direct_url = $product[ $key ]; | |
if ( substr( $direct_url, 0, 4 ) !== "http" ) { | |
$direct_url = 'http%3A%2F%2F' . $direct_url; | |
} | |
$url = add_query_arg( array( | |
'u' => $direct_url, | |
'key' => $viglink_key, | |
), $viglink_url ); | |
return $url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment