Run WP Cron on Multisite

If you are already using WP Rocket, you are probably advised to disable wp’s default Cron and run it at the server level.

For normal websites, just follow the plugin’s instructions. However, wp multisite has to change a bit. Here is the author’s method Slavi Marinov , works on WP-CLI. Let’s proceed, right?

Disable WP Cron by default

Openwp-config.php add the following code if not already

define('DISABLE_WP_CRON', true);

Install Multisite Cron

Create filemulti_cron.php in the root directory with the content:

<?php
if (!isset($_REQUEST['go'])) {
    if (php_sapi_name() != "cli") {
        return;
    }
}

// We want to see all errors
error_reporting(E_ALL);
ini_set('display_errors', 1);
 
header( 'Content-type: text/plain; charset=utf-8' );
 
ignore_user_abort(true);
set_time_limit(30 * 60);
 
$php_cmd = 'php';
$php_cli_options = array(
    '/usr/local/bin/php',
    '/usr/bin/php',
);
 
foreach ($php_cli_options as $php_bin_opt) {
    if (@is_file($php_bin)) {
        $php_cmd = $php_bin_opt;
        break;
    }
}
 
$wp_cli_options = array(
    '/usr/local/bin/wp',
    '/usr/bin/wp',
    './wp-cli.phar',
);
 
$wp_cmd = $wp_cli_options[0];
 
foreach ($wp_cli_options as $wp_cli_option) {
    if (@is_file($wp_cli_option)) {
        $wp_cmd = $wp_cli_option;
        break;
    }
}
 
if (preg_match('#win#si', PHP_OS)) { // on windows we call wp directly
    $main_cmd = $wp_cmd;
} else {
    $main_cmd = "$php_cmd $wp_cmd";
}
 
$cmd = "$main_cmd site list --field=url --archived=0 --deleted=0 --skip-packages --skip-plugins --skip-themes --format=json";
$json_str = trim(`$cmd`);
 
$blogs = empty($json_str) ? array() : json_decode($json_str, true);
$blogs = empty($blogs) ? array() : $blogs;
$total = count($blogs);
 
echo "<pre>";
echo "Starting at " . date('r') . "\n";
 
foreach ($blogs as $idx => $url) {
    $cur = $idx + 1;
    echo "$cur of $total) Processing: [$url]\n";
    $url_esc = escapeshellarg($url);
    $cmd = "$main_cmd cron event run --due-now --url=$url_esc";
    $cmd_res = trim(`$cmd`);
    echo "cron: [$cmd_res]\n";
    echo "\n<br/>";
 
    // Output stuff every X few showers
    if ($idx % 5 == 0) {
        flush();
        ob_flush();
    }
}
 
echo "Finished at " . date('r') . "\n";
echo "</pre>";
 
exit(0);

Set up Cron jobs on the server

ssh go to the server and run the command:

crontab -e

Add

*/15 * * * * wget -q -O - https://domain.com/multi_cron.php?go &>/dev/null 2>&1

Note: insteaddomain.com Using your network’s primary domain name, 15 is the number of minutes the cron takes – this can be set up for you.

To save, press Ctrl + X, y and Enter . Don’t forget to restart nginx.

We will be happy to hear your thoughts

Leave a reply

DATVNN.COM
Logo
Compare items
  • Total (0)
Compare
0
Shopping cart