WordPress offers the possibility to carry out updates automatically. In addition, the updates are also carried out during the main time (during the day). WP-CLI can be used wonderfully to carry out the updates at a defined time.
Another aspect is the translation files (so-called translation files) of WordPress itself, the plugins and themes are currently not automatically updated by WordPress. Everyone knows it, these updates have to be carried out regularly by hand in the dashboard. This can also be automated with WP-CLI. If we omit the updates, we run the risk of English terms appearing on a non-English page or in the WordPress panel.
Cronjobs (“Scheduled Tasks”) in Plesk
In order to automate the updates, we need a so-called cron job (at Plesk it is called “Planned Tasks” in German and is located under Websites & Domains in the right side menu) with which we run WP-CLI on our WordPress installation. Cronjobs can be used to execute commands or call up URLs at scheduled times. A mail notification can also be set up for each call or in the event of an error. The following explanations refer to Linux hosting, which is the case in > 99% of most cases. If you are one of the few who have Windows hosting, the steps will have to be adjusted a little.
So that WP-CLI can be used, we have to download it and store it in our own web hosting or server. The package contains the executable “wp”. This should now be stored in the file system in the web hosting (use either FTP, SFTP or the Plesk file manager for this). In order for the file to actually be executed, it needs execution rights. To do this, set the execution rights with the Plesk file manager or FTP program (see Plesk documentation). Now we are ready to create the scheduled task.
Create scheduled tasks in Plesk
In Linux command line shells, in which the cron jobs are executed, there is the option of chaining commands. && is used to concatenate commands and execute them in left-to-right order (e.g. Command1 && Command2 –MyParameters, etc.). The operator & can also be used instead of &&. Then the following commands are only executed if the previous command has been successfully completed. In this case, it doesn’t make much difference which operator we use for our automatic updates.
So that everything is really updated, including the translation files, we use the following command:
cd /wordPressInstallation && wp core update && wp plugin update – all && wp theme update – all && wp language core update && wp language plugin update – all && wp language theme update – all
First, we change to the WordPress installation directory with cd /wordPressInstallation (the WordPress installation is often located under /httpdocs). If there are several WordPress instances, a scheduled task can be created for each page and the steps are repeated for each appearance. Then the update (or update check) of WordPress itself is triggered via wp core update and the update of all plugins and themes is started via wp plugin update -all and wp theme update -all. Here you can also restrict which plugins/themes the updates should be made for or which should be ignored.
The most interesting point is the wp language command. This allows the said translation files to be updated. This can be done accordingly for core (WordPress itself), plugins and themes.
So that wp can be used as a command, the path to the directory in which the executable file wp is located must be contained in the so-called PATH variable. To do this, add the following line to the .profile file (create file if necessary) in the main directory of the hosting:
/mybin must be replaced by the path where wp is located. Alternatively, the absolute path can be used in the cron jobs instead of wp, e.g. /mybin/wp core update. Then the extension of the PATH variable is not necessary.
Update WP-CLI regularly
To ensure that WP-CLI is always up-to-date and therefore compatible with the current WordPress version, it must also be updated regularly. This is done with the following command.
wp cli update
We also create a scheduled task for this. I run it in my customer hostings once a month. This is perfectly sufficient for the update frequency of WP-CLI.