Workers + Cron Trigger FOR wp-cron.php

Good morning,
(I’m Italian and I use google translator to write in English forgive me for mistakes)
(I am not a developer)

I need help figuring out if I’ve entered the correct code in Workers to replace Wordpress’s wp-cron.php function and bypass Cloudflare’s cache:

In the script code are just these lines and the Cron Trigger I set it to 5 minutes:

addEventListener(“scheduled”, event => {
event.waitUntil(handleScheduled(event))
})

async function handleScheduled(event) {
await fetch(“https://inviaggiotraluoghiesapori.it/wp-cron.php?doing_wp_cron”)
}

thank you so much for the support

i also disabled the native function on wordpress in wp-config.php adding:

define (‘DISABLE_WP_CRON’, true);

Well, upon a scheduled event you call handleScheduled which will then fetch https://inviaggiotraluoghiesapori.it/wp-cron.php?doing_wp_cron.

And if you have scheduled this for every five minutes (I presume */5 * * * *) that should work just fine. Does it not? What does the Worker’s execution history say?

Though I am still not a fan of HTTP based schedules, if you can schedule that natively on your machine with a script it would be a safer bet than relying on an HTTP call.

yes I can create a server side cron job from cpanel,
but I should disable the cloudflare cache on the specific page https://inviaggiotraluoghiesapori.it/wp-cron.php via dns right?

Unless you have set up a page rule to cache that URL, Cloudflare won’t cache .php so there’s no cache to disable.

But if you have an option to call this via a proper cron job, that would be definitely preferable over the HTTP stuff :slight_smile:.

1 Like

And no, it’s not cached

$ curl -I https://inviaggiotraluoghiesapori.it/wp-cron.php | grep status
cf-cache-status: DYNAMIC

sandro, then I create cron jobs from cplanel and I’m fine, right?

If you know which command to execute, sure. You probably need to call wp-cron.php in one way or another. The point is, a web request is not exactly ideal or very stable for such a task and Wordpress offers that probably rather as workaround for when people don’t know how to configure a proper cron job.

As for your original question, the Worker setup should be all right and if the executions are listed in the history you should be good to go.

Thanks super nice sandro !!!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.