WP Cron is a nice feature and allows you to schedule one time or regularly executed tasks such as pulling data from a remote location at given times. With WordPress cron tasks not at the exact time as the Linux cron jobs but the tasks will run no matter if the time has passed.
WordPress uses them to check for updates and other stuff.
WordPress cron is triggered by a visit to the site. If the site doesn't get enough visitors in a given moment then WP cron won't be triggered.
You can use Linux cron to call WordPress cron so it runs at a more regular schedule regardless of the visitors coming to the site.
Here's video explanation:
Requirements:
- WP-CLI
- (S)FTP program or access to cPanel
- Willingness to learn
- Tea/Coffee/Beer/Wine ;)
Step 1: Turn the default WP Cron
define('DISABLE_WP_CRON', 'true');
This can be achieved by adding this line to the wp-config.php right after the starting <?php tag.
Step 2: Create this script
Each site in WordPress Multisite is different and has its own set of database tables and it will needs its cron job to be triggered specifically.
The following code needs to be uploaded in the root location of the WordPress multisite network. You can use an FTP program or do it via cpanel's file manager.
You need to have WP-CLI installed to you can download a local copy but you'll need to update the path to the wp-cli in the code below.
<?php // This tool uses wp-cli to trigger WP cron jobs for all sites in a WP multisite/network. // Slavi Marinov | https://orbisius.com // blog post: https://orbisius.com/4400 // // Installation: // Add this in the wp install dir as: zzz_cron.php // Call this script via linux cron // add this to wp-config.php define('DISABLE_WP_CRON', 'true'); // With some hostings (cPanel based?) we need to use full paths for php and wp-cli // To trigger it manually open this in your browser http://example.com/zzz_cron.php?go 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 ""; 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"; exit(0);
"; // Output stuff every X few showers if ($idx % 5 == 0) { flush(); ob_flush(); } } echo "Finished at " . date('r') . "\n"; echo "
Step 3: Schedule the script to be called regularly
Since we've turned off the regular WP cron functionality by defining the php constant in the wp-config.php we need to make sure the script is called so WordPress and plugins can perform their maintenance tasks.
Schedule the script above to be called regularly.
To get to the cron section within your cpanel you need search for it and you'll see it the Advanced section. Click on it.
This is page where you'd add the task to be run.
The command can use this if you don't remember how to get to full paths to the files.
wget http://example.com/zzz_cron.php?go > /dev/null 2>&1
or if you know the full path to the php binary and to your site's (document) root directory.
/usr/local/bin/php /path/to/site/zzz_cron.php
If you want to test to make sure it's working just visit this in your browser.
If you'd rather schedule the task manually via the Linux cron job here's the full command line.
*/5 * * * * wget http://example.com/zzz_cron.php?go > /dev/null 2>&1
Related
Disclaimer: The content in this post is for educational purposes only. Always remember to take a backup before doing any of the suggested steps just to be on the safe side.
Do you need any of the code in this post customized or packaged into a plugin? Get a free quote
Referral Note: When you purchase through a referral link (if any) on this page, we may earn a commission.
If you're feeling thankful, you can buy me a coffee or a beer