1

I've been reading alot about WP Cron and I have decided that I don't want to use WP Cron. I'd like to setup a linux cron at my webhost (Media Temple DV Server).

I've made a wordpress plugin that checks some posts for variables and if everything checks out it sends out an email to the user who submitted it. I want this to run every couple of minutes because i am expecting a large volume in a short span of time and also I only want to process my submissions in small batches of 10 or 20 at a time.

What I have right now is a custom scheduled event that uses wp cron to fire every 15 minutes, BUT what i would rather is a Linux cron that runs, hits the plugin url, and does its thing every 2 minutes.

Right now, the linux cron is running every 2 minutes so that's great (i set it to send me an email when the cron runs in the cron itself), BUT because i have my main code wrapped tied to the wp-cron on a 15 minute interval, the code is only running every 15 minutes.

So, I'd like to totally remove the wpcron portion from the plugin and just have the code trigger when the linux cron runs.

Does anyone have thoughts on how i can set the linux cron to run a wordpress plugin? A very basic example like Hello World plugin would be fine. I just need some help figuring out how to connect the linux cron to the plugin. Thanks.

4
  • Have you tried using WP CLI? WP Cron can be triggered via a real cron job rather than from the frontend, and custom commands can be ran
    – Tom J Nowell
    Commented Jun 29, 2017 at 18:25
  • I will look at that, I'll see what i can find. I may have tried that. From how i understand it when i set DISABLE_WP_CRON to TRUE in wp-config that only disables the "load per every page load on wp cron", but the wp cron is still working. I setup a cron to trigger wp cron every 2 minutes. But my wp-cron scheduled event still only runs every 15 minutes. So, I'm looking for a way to get rid of the scheduled event or tell it to run right now. I want it to run when Linux Cron runs.
    – Robbiegod
    Commented Jun 29, 2017 at 18:30
  • I already have a plugin written. Is there a way with WP CLI to just trigger that plugin to run? Which command would i use? The plugin is already activacted and i don't see a command that would make sense to execute the plugin. Do i need to rewrite the plugin and create custom wp commands? Any thoughts?
    – Robbiegod
    Commented Jun 29, 2017 at 19:53
  • wpcron jobs only run when somebody is around to run them, assuming your actual cron job runs every 2 and not every 15 minutes. You can write a custom WP CLI command though
    – Tom J Nowell
    Commented Jun 29, 2017 at 22:54

1 Answer 1

2

The solution I came up with here goes as follows:

  1. Installed WP-CLI and made use of the eval-file command.
  2. Converted my wordpress plugin to a standalone script.
  3. In the standalone script, I initiated wordpress so i didn't have to modify most of it. I just removed my the activation hooks and other wordpress plugin specific code.
  4. My final cron line looked like this:

    /usr/local/bin/wp --path="/var/www/vhosts/path/to/site/" eval-file /var/www/vhosts/path/to/site/and/file/location/standalone-cron-send-email.php

  5. For security purposes, I've added this to the top of the script so outside intruders can not run the script. Only WP-CLI can run the script otherwise the meant to be run from command line message is returned to the browser.

    if( php_sapi_name() !== 'cli' ) { die("Meant to be run from command line"); }

2
  • I would note that this file might be runnable from the frontend and so there is a security risk
    – Tom J Nowell
    Commented Jun 29, 2017 at 22:55
  • @TomJNowell - I updated my answer to show what I've added to the standalone script to protect it from outsiders trying to run the script. Basically, only WP-CLI can run the script.
    – Robbiegod
    Commented Jun 30, 2017 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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