pull down to refresh

The problem occurs when referral plugin code doesn't execute on first page load due to caching, server-level redirects, or WordPress initialization timing issues.

Step by Step solution

Phase 1: Quick Wins (Start Here - 70% Success Rate)

Step 1: Test in Incognito Mode
  1. Open private/incognito browser window
  2. Test your referral link
  3. If it works → Clear all browser cache and cookies
  4. If still broken → Continue to Step 2
Step 2: Disable All Caching For Cloudflare users:
  • Log into Cloudflare dashboard
  • Create page rule: yoursite.com/*ref* → Cache Level: Bypass
  • Go to Caching → Configuration → Set "Caching Level" to "No Query String"[3][4]
For WordPress caching plugins:
  • Deactivate WP Rocket, W3 Total Cache, etc.
  • Test referral links
  • If working, configure exclusions for referral URLs[5][6]

Phase 2: Server-Level Issues (25% Success Rate)

Step 3: Check .htaccess File
  1. Backup your .htaccess file first
  2. Via FTP/cPanel, rename .htaccess to .htaccess-backup
  3. Test referral links
  4. If working, the issue is redirect order in .htaccess[7][8]
Fix .htaccess redirect order:
# Custom redirects MUST come BEFORE WordPress section
RewriteCond %{QUERY_STRING} ^(.*)ref=(.*)$ [NC]
RewriteRule ^(.*)$ /$1?%1ref=%2 [L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
# WordPress rules here
</IfModule>
# END WordPress

Phase 3: WordPress Code Solutions (30% Success Rate)

Step 4: Force Early Plugin Execution Add this code to your theme's functions.php file:
// Force early referral tracking
function force_early_referral_tracking() {
    if (isset($_GET['ref']) || isset($_GET['referral']) || isset($_GET['affiliate'])) {
        // Force WooCommerce session start
        if (class_exists('WooCommerce')) {
            WC()->session->init();
        }
        // Force session start
        if (!session_id()) {
            session_start();
        }
    }
}
add_action('init', 'force_early_referral_tracking', 1);
Step 5: Server-Level Cache Bypass Add before WordPress rules in .htaccess:
# Bypass caching for referral URLs
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} (ref=|referral=|affiliate=) [NC]
RewriteRule .* - [E=no-cache:1]
Header set Cache-Control "no-cache, no-store, must-revalidate" env=no-cache
</IfModule>

Note Hosting-Specific Quick Fixes

The success rate varies significantly by hosting environment. Cloudflare users should start with CDN configuration, while shared hosting users often need to address server-level restrictions.
Why This Happens??
This issue occurs because
  • server level caching serves cached pages before WordPress loads
  • CDN/Cloudflare caches URLs with query parameters
  • .htaccess redirects interfere with referral parameters
  • WordPress hook timing - plugins load after other systems intercept requests
  • Session management issues on shared hosting with limited PHP session timeouts

Advanced Solutions (If above Steps Fail)

Create Must-Use Plugin:
  1. Create folder /wp-content/mu-plugins/
  2. Add file force-referral-early.php:
<?php
function force_referral_plugins_early() {
    if (isset($_GET['ref']) || isset($_GET['referral'])) {
        while (ob_get_level()) {
            ob_end_clean();
        }
        if (!headers_sent()) {
            header('Cache-Control: no-cache');
        }
    }
}
add_action('muplugins_loaded', 'force_referral_plugins_early', 1);

To make sure this never happens again

  1. Always backup files before making changes
  2. Test on staging site first if possible
  3. Document changes so you can reverse them
  4. Contact plugin support with diagnostic data if nothing works
The key insight is that "something else takes charge before WordPress" meaning the issue occurs at the server/CDN level before your WordPress plugins can execute. Start with the highest success rate solutions first, and work systematically through the troubleshooting matrix.
Thank you for the detailed & step-by-step response! Phase 1 I have tried, although I will make a note to the plugin devs that it would be good to have an option to have specific referral links i.e. site.com/ref/code. Currently the Referral Codes plugin creates them simply as site.com/code which is the same format as any other page. However, I did have Rocket.net support turn off Cloudflare for the specific URL's and it didn't work, so that's probably not the issue anyway. Phase 2 & 3 I have also tried in different formats (and solutions via different LLM's - I'm not a dev myself, unfortunately), but I'll definitely give your specific instructions a go.
One thing that I didn't mention - ChatGPT said it shouldn't make a difference in how the links act - was that there was also a PHP error that came up in debugging:
PHP Deprecated: Creation of dynamic property WC_Coupon::$lws_referralcode
Chat: Not critical for functionality yet, but in PHP 8.2+ dynamically adding properties to objects is deprecated. The plugin is trying to attach $lws_referralcode directly to the WC_Coupon object. This should ideally be stored in meta instead:
php Copy code $coupon->update_meta_data('lws_referralcode', $code);
Chat: This doesn’t directly break the cookie behavior, but it’s good to fix for future PHP compatibility.
In any case, I'll follow your instructions and see where it takes me. Can't guarantee I'll get to it tonight (has been a long day), but tomorrow at the latest!
reply