• Resolved linzD

    (@linzd)


    I want to run a short script every time we get a new visitor to the site, which saves a cookie with their landing page and their source.

     I’ve written this code and it works as expected and I have hooked it to the “send_headers” hook. I chose this hook because I believe it’s the earliest hook where global $post is set, which I need.

    However… when we have caching on our Siteground site the script often doesn’t run on the first page load, presumably when the visitor is being served a cached page.

    For example, often if a visit starts on the Homepage the script doesn’t run. It then runs later when they visit an uncached page.

    This means our code doesn’t work as expected.

    I have added our cookie “bnfo_settings” to the “sgo_bypass_cookies” filter but this hasn’t worked.

    Can you provide any guidance of how I can make sure this script is always run please? Obviously I don’t want to turn caching off.

    Thank you!

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Georgi Ganchev

    (@georgiganchev)

    Hello @linzd,

    The hook approach is not recommended in your scenario as the full-page caching loads your page serving the static HTML copies of your page to your visitors. PHP code including hooks like “send headers” will not execute for cached pages because the server is bypassing the normal WordPress loading process to deliver the cached content quickly.

    You should use a different approach such as JavaScript tracking logic or AJAX request which can trigger a PHP script that is not cached.

    Best regards,
    Georgi Ganchev

    Thread Starter linzD

    (@linzd)

    Thank you. That’s a good shout. I am bit out of practice as a coder, but re-wrote in JS rather than PHP.

    Hopefully that’ll solve the issue.

    Plugin Support Gergana Petrova

    (@gpetrova)

    Hello @linzd,

    Since we haven’t heard back from you for a while, we will mark the topic as Resolved. You will still be able to post new updates if needed.

    Best Regards,
    Gergana Petrova

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Dynamic script not running on cached page’ is closed to new replies.