Replying to Avatar Kontext

Web developers / Wordpress experts / Code wizards! I need help with the following issue: WooCommerce referral links only trigger on 2nd visit.

25k sats + lifetime Gold membership on SATOSH.EE and my eternal gratitude to whoever helps me solve the problem.

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!

#asknostr #devs #wordpress #woocommerce #bounty

Well according to Chat (again) this is a known issue: plugins like WooRewards don’t run on the first visit via a referral URL — the plugin’s code doesn’t execute until a second visit, which suggests something is bypassing WordPress early on.

From reddit: “It usually comes down to caching and when WooCommerce sessions actually start. What helped me was hooking into WordPress really early, like on init or wp_loaded, to catch the referral info before any caching or redirects happen."

Immediate Fixes to Try

1. Ensure Cloudflare Excludes Referral URLs from Cache

Cloudflare’s default behavior often serves cached pages for URLs with query parameters unless explicitly configured otherwise. You can address this by:

• Using Cache Rules to bypass caching when the query string contains your referral parameter. Example rule: (If http.request.uri.query contains "ref", then bypass cache.)

2. Use Cloudflare Transform Rules for Query Ignoring

If you’re using “Cache Everything,” query strings may still get cached. Transform Rules can help you ignore specific query parameters — like ref — to ensure each visit is treated as unique

3. Launch Referral Logic at the Earliest Hooks

Move your referral tracking code to the earliest stages of WordPress execution — ideally in an mu‑plugin — using hooks like:

add_action('init', 'set_referral_cookie', 1);

function set_referral_cookie(){

if (isset($_GET['ref'])) {

setcookie('custom_ref', sanitize_text_field($_GET['ref']), time() + DAY_IN_SECONDS * 30, '/');

error_log('Referral cookie set: ' . $_GET['ref']);

}

}

If you don’t see this log on first visit — but do on reload — it’s strong evidence that the code isn’t being run due to caching interference.

4. Add Debug Logs for Plugin Calls

Add logging at the very start of your referral plugin’s main file or in a mu-plugin:

error_log('Referral plugin reached init at ' . current_time('mysql'));

If you don’t see this log on first visit — but do on reload — it’s strong evidence that the code isn’t being run due to caching interference.

Summary Diagnostic Flow

Step

Description

1. Add early logging

Confirm if plugin or cookie logic executes at first visit.

2. Test referral link

Use curl -I https://satosh.ee/?ref=test — check headers like cf-cache-status: HIT vs MISS.

3. Adjust cache rules

Use Cloudflare Cache or Transform rules to bypass referrals.

4. Validate in Incognito

Remove preload/prefetch interference from browser.

5. Re-test every change

Confirm if the plugin logic actually runs on first visit post-changes.

Reply to this note

Please Login to reply.

Discussion

Hey, just wanted to say thanks for the input, I've tried most of the things on your list beforehand, and as much as I've continued testing the culprit seems to be: referral cookie attempts to set too late, after WooCommerce sessions have started or headers have been sent. On first visit, no session exists → cookie cannot be set → referral only works on second visit.

I've tried to do step 3: Launch Referral Logic at the Earliest Hooks many times (even before posting this problem here) with different code snippets from ChatGPT, Claude, now also Lumo and Duck.ai (which, admittedly, also uses ChatGPT) but none of the solutions work. The end result is still the same (or sometimes worse).

Bummer. Maybe completely disabling Cloudflare?

Rocket.net support said they can't even completely disable it 🤦‍♂️ Only specific URL paths