Trigger a custom wordpress plugin with a linux cron

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”);
    }

Leave a Comment