25k sats + lifetime Gold membership on SATOSH.EE and my eternal gratitude to whoever helps me solve the issue (did not set up a bounty here since I'll be posting this also on nostr and X).
I’m running into an issue with referral links on a WooCommerce site (WordPress + WooRewards paid version + Referral Codes (WooRewards and Referral Codes are LongWatchStudio plugins); also tested with the Affiliates plugin - the issue also occurred with that one). The goal is to establish referral bonds between users for a loyalty/affiliate program.
The problem:
- Referral logic only triggers on the second visit to a referral URL.
- On first visit, cookies for tracking (e.g., lwsadmsession…) either don’t appear or the plugin code never runs.
- Reloading the same link triggers the referral logic as expected.
- This behavior occurs with multiple referral plugins, not just WooRewards.
What I’ve tried:
- Disabled all other plugins, switched to default theme
- Cloudflare + Rocket.net: disabled query string caching
- .htaccess rules to bypass server caching
- Early hook code to force session/logic execution (relied on ChatGPT to come up with those codes, would not be surprised if they were faulty in and of themselves)
- Verified no Varnish or server-level caching interference
Developer insight from WooRewards:
“Our code is never called. Something else takes charge before WordPress, and (even if unintentional) fakes our redirection.”
Looking for ideas why referral plugin code isn’t executing on first visit, even when cookies or sessions should be available.
Happy to provide more details, just drop a comment or email me kontext@satosh.ee
Thanks!
The second visit usually returns a MISS or BYPASS because the cookie/session already exists, and then WordPress responds dynamically.
*satoshi.com/ref
Cache Level: Bypass
satoshi.com/?ref=*
Cache Level: Bypass
apache# Bypass cache for referral codes RewriteCond %{REQUEST_URI} ^/ref_code [NC,OR] RewriteCond %{QUERY_STRING} (^|&)lrc_code= [NC] RewriteRule .* - [E=Cache-Control:no-cache]
The key insight is that redirect caching was the hidden issue - not just page caching, but the redirect itself being cached before WordPress could process the referral logic.add_action( 'init', function() { if ( class_exists( 'WooCommerce' ) ) { if ( null === WC()->session ) { WC()->session = new WC_Session_Handler(); WC()->session->init(); } } }, 1 );
Step by Step solution
Phase 1: Quick Wins (Start Here - 70% Success Rate)
yoursite.com/*ref*
→ Cache Level: BypassPhase 2: Server-Level Issues (25% Success Rate)
.htaccess
to.htaccess-backup
Phase 3: WordPress Code Solutions (30% Success Rate)
functions.php
file:.htaccess
:Note Hosting-Specific Quick Fixes
Advanced Solutions (If above Steps Fail)
/wp-content/mu-plugins/
force-referral-early.php
:To make sure this never happens again
Lax
' row here.