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

Reply to this note

Please Login to reply.

Discussion

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.

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

25k sats + a lifetime membership upgrade at nostr:nprofile1qyd8wumn8ghj7cn0wd68ytnvv43hgatjd9n8jtnwv46z7qg4waehxw309a5xjum59ehx7um5wghxcctwvsqzqr5g4trndr2lykpyx7pxqs4nlvazdgfx70v9wcvvddn99204h7s27tcduj still up for grabs to whoever can find me a working solution:

The issue seems to be that 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.

What I've tried to do is - 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.

#asknostr #wordpress #woocommerce #devs

nostr:nevent1qvzqqqqqqypzqqn3uxu6m9lr790dws3j03ww5dynh4nzsps3vw9h5rmw9zkcelusqyt8wumn8ghj7etyv4hzumn0wd68ytnvv9hxgtcpzemhxue69uhks6tnwshxummnw3ezumrpdejz7qgswaehxw309ahx7tnnw3ezucmj9uqzptp5jayaqmpfrf98yxnlfqew5hxe2f0dpt0xm4yrqveslpenregda7xxuf

FYI in case anybody is following this / trying to figure out the solution. The issue is on the server level, 100%. I've duplicated the Staging version of Satosh.ee to a free host and the referral links worked there on the first try. I've escalated the issue with the hosting provider, hopefully it will be solved soon 🙏

SOLVED, TL;DR:

Rocket.net's infrastructure strips tilde characters (~) from query parameters at the server level before they reach WordPress/PHP.

This broke WooRewards (AKA MyRewards - the free version of the plugin) referral links. After months of debugging and working with their engineering team, they confirmed this is a platform limitation they cannot change. Solution: Switched hosts to Cloudways - referral links now work immediately.

What I tried (all failed on Rocket.net):

Disabled all caching (WordPress, Cloudflare, server-level)

Modified .htaccess with cache bypass rules

Created MU-plugins for early session initialization

Tested with multiple referral plugins (same issue across all)

Cookie whitelisting (only partially helped)

Definitive proof it was server-level:

Created debug logging that showed when visiting ?~=abc123&foo=bar, the URL arrived at the server but $_SERVER['QUERY_STRING'] was empty in PHP - the tilde parameter was stripped before reaching the application layer.

Lesson learned: If your referral/affiliate plugins use non-standard query parameter formats (especially tilde), verify your host doesn't filter them. Standard parameters like ?ref=CODE worked fine, but WooRewards' referral link format was incompatible with Rocket.net's infrastructure.

Working solution: Migrated to Cloudways] - both WooRewards tilde links and standard referral code plugins now function correctly on first visit.