Created
September 10, 2025 12:21
-
-
Save techjewel/2010da7a6d8e8321881b40748a2f4c02 to your computer and use it in GitHub Desktop.
Code Snippet to support old affiliateWP Pretty links to FluentAffiliate
This file contains hidden or 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 | |
| /** | |
| * Code Snippet to support old affiliateWP Pretty links to FluentAffiliate | |
| * This will check if the URL contains /partner/{referral_code} and will convert it to ?ref={referral_code} | |
| * where 'ref' (or new variable) is the query variable that you had set in FluentAffiliate settings. | |
| */ | |
| add_action('wp', function () { | |
| if (!defined('FLUENT_AFFILIATE_DIR')) { | |
| return; | |
| } | |
| global $wp; | |
| $request = (string)$wp->request; | |
| if (!$request) { | |
| return; | |
| } | |
| // Check the next line, change 'partner' to your Referral variable that you had with AffiliateWP | |
| $refVariable = 'partner'; | |
| if (strpos($request, $refVariable . '/') === false) { | |
| return; | |
| } | |
| $refArr = explode('/', $request); | |
| $refArr = array_filter($refArr); | |
| if (count($refArr) < 2) { | |
| return; | |
| } | |
| // last element is the referral code | |
| $referralCode = array_pop($refArr); | |
| $refVar = array_pop($refArr); | |
| if (!$referralCode || $refVar != $refVariable) { | |
| return; | |
| } | |
| $newQueryVar = \FluentAffiliate\App\Helper\Utility::getQueryVarName(); | |
| $newRequest = str_replace("$refVariable/$referralCode", '/', $request); | |
| // remove double slashes | |
| $newRequest = str_replace('//', '/', $newRequest); | |
| // remove trailing slash | |
| $newRequest = rtrim($newRequest, '/'); | |
| $url = site_url($newRequest); | |
| $givenQueryArgs = $_GET; | |
| $givenQueryArgs[$newQueryVar] = $referralCode; | |
| $url = add_query_arg($givenQueryArgs, $url); | |
| wp_redirect($url); | |
| exit; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment