Setup Real Cron Jobs for WordPress Multisite Network

Replace resource eating WordPress wp-cron with real cron jobs

Setting up real cron jobs to replace WordPress multisite wp-cron is little bit harder than for WordPress single sites. Each subsite on your multisite network needs its own entry in crontab or scheduled tasks will fail. This can become a problem if you have a large network. This tutorial will help you setup proper cron jobs for WordPress multisite network.

Cron Jobs for smaller Multisite Network

You can use this method if you think you can add a new line to crontab each time you create a new subsite. The benefit of using this method is that you can set up different schedules for each of your sites. Since you’re adding new crontab entry for each site, you can decide cron job run frequency for each site. Command you need to run for each site depend on whether you have a subdomain or subdirectory installation. You can run following command for subdomain and custom domain setups.

curl -s https://sub.example.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1

And command for subdirectory setup is,

curl -s https://example.com/[directory]/wp-cron.php > /dev/null 2>&1

You can add these commands to crontab using Virtualmin. Process is same as described in WordPress single site cron jobs with Virtualmin. But for this tutorial, we’ll edit crontab from CLI. Login to your server as root user and open crontab for editing. You’ll be prompted to select an editor if this is your first time editing crontab. Use the recommended editor.

crontab -e

Now add the applicable command,

# For subdomain installation
*/10 * * * * curl -s https://sub.example.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1

# For subdirectory installation
*/10 * * * * curl -s https://example.com/[sub]/wp-cron.php?doing_wp_cron > /dev/null 2>&1

These commands will setup cron jobs for WordPress multisite to run every 10 minutes. You can change */10 to */5 to run it every 5 minutes. As I said before, you should add a new line for each of your sites in the network.

This can be very inconvenient if you have large network or you’re adding new sites frequently. We can overcome this by creating a simple php script to extract site list from WordPress itself.

Cron Jobs for larger Multisite Network

The php script we’re going to create will query WordPress database for active sites. It’ll load WordPress to extract this information. Create an empty php file for our script,

nano /home/username/public_html/vpsfix-multisite-cron.php

Now paste the following lot,

<?php
require('./wp-load.php');
global $wpdb;
$sql = $wpdb->prepare("SELECT domain, path FROM $wpdb->blogs WHERE archived='0' AND deleted ='0' LIMIT 0,300", '');

$blogs = $wpdb->get_results($sql);

foreach($blogs as $blog) {
    $command = "http://" . $blog->domain . ($blog->path ? $blog->path : '/') . 'wp-cron.php';
    $ch = curl_init($command);
    $rc = curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
    $rc = curl_exec($ch);
    curl_close($ch);
}
?>

You can close the file by hitting Ctrl+X. Now we can create a cron job to execute this script. Adding following line to crontab will execute above script every 10 minutes which will trigger wp-cron on all subsites and main site.

*/10 * * * * curl -s https://example.com/vpsfix-multisite-cron.php > /dev/null 2>&1

The benefit of this method is that it’ll work for any subsite you create in the future as well. So you can just forget about cron jobs and focus on your business. And finally, we can disable default wp-cron behaviour of WordPress by adding following code to wp-config.php.

define('DISABLE_WP_CRON', true);

Do let me know which method above you like to use to setup cron jobs for WordPress multisite in the comments section.

Tags
Show More

Tharindu

Hey!! I'm Tharindu. I'm from Sri Lanka. I'm a part time freelancer and this is my blog where I write about everything I think might be useful to readers. If you read a tutorial here and want to hire me, contact me here.

6 Comments

  1. Hi Tharindu,
    You saved me again with this article, I am one of your biggest fan. Just a question: how can I simply check that everything is working well? I have a multisite WordPress with shared hosting and managed by a cPanel.
    Looking forward to reading you,
    Regards.

    1. Hi Ludovic,

      Glad you found it helpful. There are plugins like WP Crontrol that let’s you see what’s going on with WP Cron, but if you don’t want to add another plugin to your site, you can simply schedule a post and see if it gets published.

  2. Hi Tharindu, I followed the steps but it does not work, I tested with a post and it is not published, what did I miss? WP-CLI must be installed? The only code customization must be "https://example.com" or is there something else?
    My regards.

  3. Do you know if a cron job will work with a custom domain on a multisite?

    my multi site is
    sub.example.com

    but I have a few sites with

    customdomainname.com

    mapped to a subsite on it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

We use cookies to give you the best online experience. By agreeing you accept the use of cookies in accordance with our cookie policy.

Close