Replacing WordPress Cron and Scheduled Actions with a real cronjob

This is part 14 of my article series 25+ Tutorials on How to boost the performance of your WooCommerce store. In this article we will install the Crontrol Plugin to get visibility into which fake cronjob Wordpress is executing and if applicable we will deactivate the Wordpress Cron logic altogether and instead have a real cronjob execute the events.

The name of this setting is actually misleading since setting it to true won’t disable this feature entirely, it just prevents the execution of what’s stored inside wp_options cron field. So it’s somewhat dangerous to deactivate the processing and not to monitor what happens to the option value constantly.

Step 1: Install WP Crontrol

Also, you need to make sure that no essential functions of your site are executed via the fake cron. For this install and activate the WP Crontrol Plugin by adding it to our project like so:

composer require wpackagist-plugin/wp-crontrol

This will give you more insight. Also, under Woocommerce -> Reports there is a tab called Action Scheduler which displays more fake cron activity. Things you want to look for are payment/shipment tracking/warehouse related tasks. If there’s nothing essential, then you should skip directly to the complete deactivation.

Step 2: Deactivating Fake Cron execution

wordpress/wp-config.php
defined('DISABLE_WP_CRON') or define('DISABLE_WP_CRON', true); defined('DISABLE_WP_HTTP_WORKER') or define('DISABLE_WP_HTTP_WORKER', true);

Step 3: Running the cron server side

If your site depends on the scheduled tasks/fake crons to be executed for some reason, you should call the  wp-cron.php file in a real cronjob. To edit cornjobs enter crontab -e on the command line. Add the following line and save it.

*/4 * * * * /usr/bin/wget -O - -q -t 1 https://example.com/wp-cron.php > /dev/null 2>&1

In this example, the cron is executed every 4 minutes.  Learn more about the syntax of cronjobs here.

Alternative complete deactivation

While DISABLE_WP_CRON only prevents the execution of fake crons, but does not prevent the wp_option field being filled up, I recommend to add this PHP snippet in order to prevent, that If you do not want to have any fake cron executed you might want to add the follwing snippet to your themes functions.php

wordpress/wp-content/themes/storefront-child/functions.php
add_filter( 'pre_update_option_cron', 'override_cron',99,3); add_filter( 'default_option_cron', 'override_cron',99,3); add_filter( 'pre_option_cron', 'override_cron',99,3); add_filter( 'option_cron', 'override_cron', 99, 3); function override_cron($one, $two, $three) : string { return ''; }

What this does is to empty the cron field on update and to return an empty string on retrieval. Thus nothing is executed.

And last but not least, do monitor the cron option in your wp_options table. If done wrong, the serialised object stored in there gets so big, that your whole website slows down. This has to do with the WordPress Core reading the value on many occasions, even though it’s not supposed to run the fake cron.

Continue to part 15 of my tutorial series: Utilise the WordPress Firewall to prevent plugins from phoning home.

tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo tech logo