Skip to content

Instantly share code, notes, and snippets.

@unapersona
Created March 15, 2022 10:41
Show Gist options
  • Save unapersona/5dab6d7671d21d884d36eaf0a2b1964a to your computer and use it in GitHub Desktop.
Save unapersona/5dab6d7671d21d884d36eaf0a2b1964a to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: WordPress Multisite Cron CLI
Description: WPCLI command utilities for WPCron on Multisite
Author: Jorge González
Version: 1.0.0
Author URI: http://unapersona.com/
*/
/**
* WordPress Multisite Cron utilities
*/
class WPMU_Cron_CLI {
/**
* Executes WP-Cron on every site
*
* ## EXAMPLES
*
* wp mu-cron execute
*
* @when after_wp_load
*/
function execute( $args, $assoc_args ) {
if ( ! is_multisite() ) {
WP_CLI::error( 'This is not a multisite instalation' );
}
$sites = get_sites(
array(
'archived' => 0,
'deleted' => 0,
'limit' => -1,
)
);
foreach ( $sites as $site ) {
switch_to_blog( $site->blog_id );
$url = home_url();
$blogname = get_bloginfo( 'name' );
WP_CLI::line();
WP_CLI::line( "Executing CRON on {$blogname} ({$url})" );
WP_CLI::runcommand(
'cron event run --due-now',
array(
'return' => false,
'exit_error' => false,
)
);
}
WP_CLI::line();
}
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'mu-cron', 'WPMU_Cron_CLI' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment