0

I have a few custom tables in the database and I need to clean expired data from time to time.

If I was using WP-Cron, it would be easy, but WP-Cron sucks and is a no-go (especially for what I am doing). Having that in mind, I need to launch some code using crontab. How should I do it in a clean, effective manner that would not require nasty hacks?

I was thinking of creating a REST endpoint, but how to protect it so it could only be opened by the crontab?

The solution should be inside the plugin folder so it could be stored in GIT easily (i.e. nasty workarounds storing files in the main folder (a.k.a public_html) are not welcomed).

Was searching for a good solution for hours with no success... Thanks in advance!

2
  • 2
    Have you tried launching WP Cron via crontab instead? WP Cron normally relies on a nonblocking request when a user visits because WP can't modify crontab, but if you have access to crontab you can trigger it at regular intervals so it always runs even if nobody visits, and at specific intervals. On my own site I trigger it every 5 minutes via a cron job, and I recently changed it to use a job manager such as Cavalcade to run cron events. Even WP CLI can trigger it
    – Tom J Nowell
    Aug 13, 2020 at 11:53
  • @TomJNowell genius! Haha, sometimes the answer is so obvious that it's hard to see :D Thanks a lot!! Aug 14, 2020 at 12:03

1 Answer 1

1

The solution is as @Tom J Nowell mentioned to launch WP Cron via crontab (and disable it on user visits).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.