Skip to content

Instantly share code, notes, and snippets.

@MaximeCulea
Last active January 14, 2023 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaximeCulea/df0766f9e17105f03717f206a22a2d39 to your computer and use it in GitHub Desktop.
Save MaximeCulea/df0766f9e17105f03717f206a22a2d39 to your computer and use it in GitHub Desktop.
Cron for WP with WP-CLI.

Add a crontab

crontab -e

Crontab

WordPress Crons running with WP-CLI each 15 minutes with :

Var Type Description
{path_to_wp} string path to the WordPress root folder
{path_to_php} string path to php
{path_to_wp_cli_phar} string path to the wp-cli.phar package. Using our project Skafold, it's generally `{wp_content_folder}/tools/wp-cli.phar`. If you have wp cli installed, just use `wp`.
{wp_content_folder} string depending on if project is running with composer or not : content or wp-content

Single site

*/15 * * * * cd {path_to_wp} && {path_to_php} {path_to_wp_cli_phar} cron event run --due-now

Exemples:

*/15 * * * * cd /home/public_html/ && php71 content/tools/wp-cli.phar cron event run --due-now

For WooCommerce Action Scheduler (https://actionscheduler.org/wp-cli/):
*/5 * * * * php tools/wp-cli.phar action-scheduler run

Multisite

*/15 * * * * cd {path_to_wp} && {path_to_php} {path_to_wp_cli_phar} site list --field=url | xargs -i -n1 {path_to_php} {path_to_wp_cli_phar} cron event run --due-now --url="{}"

Exemple:

*/15 * * * * cd /home/public_html/ && php71 wp-cli.phar site list --field=url | xargs -i -n1 php71 wp-cli.phar cron event run --due-now --url="{}"

Disable WP Cron

// Ensure WP Cron are disabled into wp-config
define( 'DISABLE_WP_CRON', true );
@MaximeCulea
Copy link
Author

Other writing :
for url in $(wp site list --field=url --url=); do wp option get siteurl --url=$url; php wp cron event run --due-now --url=$url; done;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment