Update WordPress, plugins and themes using WP-CLI and Cron

by

in

Using CRON to automatically update themes, plugins and WordPress core saves you a lot of time and will keep your sites and server secure.


This tutorial guides you through how to set up a CRON job

[themedy_button url=”https://www.wpjeos.no/automatic-reboot-apache/” icon=”wrench” font_awesome_att=”” label=”Set up a cron job” colour=”blue” colour_custom=”” size=”large” edge=”rounded” target=”_self”]

 

You may update WordPress automatically using WP-CLI with CRON using this command:

There is one important thing to bear in mind.

The cron job should run as the user www-data

&& sudo -u www-data wp

The cron job that reboots the server and restarts Apache runs as root with the command “sudo crontab -e”

To set up cron job for WP-CLI use the command:

crontab -e

 

* 4 * * 1 cd /var/www/html && sudo -u www-data wp core update --quiet && sudo -u www-data wp core update-db --quiet && sudo -u www-data wp plugin update --all --quiet && sudo -u www-data wp theme update --all --quiet

This command updates WordPress at four in the morning the first day of the week s using WP-CLI.

Then you could set your next WordPress installation to be upgraded at the same time of the week but on the second day of the week.

Once twice a month should be sufficient for the most users.

You should verify that there is sufficient file and folder rights for WP-CLI to run:

sudo chown -R www-data /var/www/html/ && sudo chgrp -R www-data /var/www/html/ && sudo find /var/www/html/ -type f -exec chmod 644 {} \; && sudo find /var/www/html/ -type d -exec chmod 755 {} \; && sudo chown www-data:www-data -R /var/www/html/ && sudo usermod -aG www-data ubuntu && sudo chmod -R g+w /var/www/html/ && sudo chmod 660 /var/www/html/wp-config.php

This WP-CLI command is from Bjørn Johansen

Not all paid plugins

This command updates your plugins from the main WordPress repository and some 3. party paid plugins like Gravity Forms. The paid version of Yoast SEO is not upgradable this way. Using WordShell you could update Yoast SEO automatically but you would first need to manually download Yoast SEO to the local repository.

Cache purging the W3 Total Cache

After upgrading WordPress you would need to purge and flush the cache of W3 Total Cache

wp w3-total-cache flush all

Other WP-CLI commands for W3 Total Cache:

wp w3-total-cache cdn_purge
wp w3-total-cache fix_environment []
wp w3-total-cache flush [–post_id=] [–permalink=]
wp w3-total-cache opcache_flush
wp w3-total-cache opcache_flush_file
wp w3-total-cache option [] [–state] [–master] [–type=] [–delimiter=]
wp w3-total-cache pgcache_cleanup
wp w3-total-cache querystring