Automating WooCommerce and WordPress Updates with WP-CLI Script

WordPress and WooCommerce updates are critical nowadays. Keeping your site or store secure from the latest security exploits that may have been disclosed and reported on WPScan Vulnerability Database (see here how to install WP Scan and use it) or ThreatPress.

WP-CLI can help automate the monotonous update dance of plugins, themes and WordPress core all site owners or maintainers need to do. Previously I have shown how to automate the database upgrade for WooCommerce using WP-CLI so this post is a precursor to that which will also be useful for vanilla WordPress sites.

If you want to do updates for WordPress or WooCommerce automatically on a scheduled cronjob see this post

In addition to showing how to automate the update of WordPress and WooCommerce plugins and themes (as well as core) you will also learn how to run the database updates.

You should always test updates on a staging site first! Kinsta and Cloudways provide staging sites

Automating WordPress Updates with WP-CLI

For simplicity I am organizing this into theme, plugin and core updates with WP-CLI

Updating WordPress Plugins

To update all WordPress plugins using WP-CLI you can use this command

wp plugin update --all

If you only want to update a specific plugin then the syntax is different

wp plugin update akismet

You can also update multiple plugins

wp plugin update akismet,jetpack

You can even update all plugins but exclude some

wp plugin update --all --exclude=akismet,jetpack

Updating WordPress Themes

To update all WordPress themes installed (not necessarily activated) using WP-CLI you can use this command

wp theme update --all

If you only want to update a specific theme then the syntax is different

wp theme update storefront

You can also update multiple plugins

wp theme update storefront,twentynineteen

You can even update all plugins but exclude some

wp theme update --all --exclude=storefront

Updating WordPress Core

This is the big one!

You can update WordPress core with this command

wp core update

There may also be a database upgrade required which you can handle very easily

wp core update-db

It can also be a good idea to flush the internal object cache and flush the rewrite rules after a core update

wp cache flush
wp rewrite flush

Putting it all together we end up with this easy to use WP-CLI script for updating WordPress core and taking care of the other maintenance that may be required.

wp core update
wp core update-db
wp cache flush
wp rewrite flush

Now we can put it all together in one bash script

Automating WordPress Updates with WP-CLI

Here is the whole WP-CLI script for updating WordPress plugins, themes, core and doing any database updates required

wp plugin update --all
wp theme update --all
wp core update
wp core update-db
wp cache flush
wp rewrite flush

When you execute the entire script you will get some output looking like this

Enabling Maintenance mode...
Downloading update from https://connect.advancedcustomfields.com/v2/plugins/download?token=eyJwIjoicHJvIiwiayI6ImIzSmtaWEpmYVdROU5UWXhPRGg4ZEhsd1pUMWtaWFpsYkc5d1pYSjhaR0YwWlQweU1ERTFMVEExTFRFMUlESXhPak15T2pFMCIsIndwX3VybCI6Imh0dHBzOlwvXC9uZXcuc3RhZ2luZy5jb2RlYWJsZS5pbyJ9...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/wordpress-seo.14.3.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Disabling Maintenance mode...
Success: Updated 2 of 2 plugins.
+----------------------------+-------------+-------------+---------+
| name                       | old_version | new_version | status  |
+----------------------------+-------------+-------------+---------+
| advanced-custom-fields-pro | 5.8.9       | 5.8.12      | Updated |
| wordpress-seo              | 13.5        | 14.3        | Updated |
+----------------------------+-------------+-------------+---------+
Downloading update from https://downloads.wordpress.org/theme/twentytwenty.1.4.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the theme...
Theme updated successfully.
Success: Updated 1 of 1 themes.
+--------------+-------------+-------------+---------+
| name         | old_version | new_version | status  |
+--------------+-------------+-------------+---------+
| twentytwenty | 1.2         | 1.4         | Updated |
+--------------+-------------+-------------+---------+
Updating to version 5.4.2 (en_US)...
PHP Warning:  Declaration of WP_CLI\CoreUpgrader::download_package($package) should be compatible with WP_Upgrader::download_package($package, $check_signatures = false) in /www/newcodeableio_534/.wp-cli/packages/vendor/wp-cli/core-command/src/WP_CLI/CoreUpgrader.php on line 145
Warning: Declaration of WP_CLI\CoreUpgrader::download_package($package) should be compatible with WP_Upgrader::download_package($package, $check_signatures = false) in /www/newcodeableio_534/.wp-cli/packages/vendor/wp-cli/core-command/src/WP_CLI/CoreUpgrader.php on line 145
Downloading update from https://downloads.wordpress.org/release/wordpress-5.4.2-partial-0.zip...
Unpacking the update...
Success: WordPress updated successfully.
Success: WordPress database already at latest db version 47018.
Success: The cache was flushed.
Success: Rewrite rules flushed.

Now we can move on to WooCommerce 🙂

Automating WooCommerce Updates with WP-CLI

WooCommerce plugins we can use a combination of bash commands to only list plugins that contain the string WooCommerce thanks to StackOverflow as a comma-separated list.

wp plugin update $(wp plugin list --field=name | grep woocommerce | awk '{print $1}' | paste -s -d, -)

Often after a WooCommerce update you also need to update the database. This requires a WP-CLi package that can communicate via the REST API which WooCommerce also uses.

This snippet will install the restful package if it is not installed

wp plugin update --all
RESTFULTEST=$(wp package list --fields=name ${WPFLAGS} | grep restful)
if [[ -z "${RESTFULTEST}" ]]; then
    wp package install wp-cli/restful ${WPFLAGS}
fi

Then we can update the WooCommmerce database by specifying an admin user ID, here it is 2

wp wc update --user=2

Putting this all together for automatic scripted updates for WooCommerce plugins looks like this

wp plugin update $(wp plugin list --field=name | grep woocommerce | awk '{print $1}' | paste -s -d, -)
RESTFULTEST=$(wp package list --fields=name ${WPFLAGS} | grep restful)
if [[ -z "${RESTFULTEST}" ]]; then
    wp package install wp-cli/restful ${WPFLAGS}
fi
wp wc update --user=2
wp cache flush
wp rewrite flush

Now let’s combine them both to tackle both WordPress and WooCommerce.

Automation Script for Updating WordPress and WooCommerce

Here is the whole script 🙂

#!/usr/bin/env bash
# Purpose: automate core, plugin and theme updates for WordPress and WooCommerce
# Author: Mike from WP Bullet
# custom flags for WP-CLI, can leave blank
WPFLAGS="--allow-root"

# admin user ID for updating WooCommerce
ADMINUSERID=2

wp plugin update --all ${WPFLAGS}
wp theme update --all ${WPFLAGS}
wp core update ${WPFLAGS}
wp core update-db ${WPFLAGS}

# WooCommerce check
if $(wp plugin is-installed woocommerce); then
    RESTFULTEST=$(wp package list --fields=name ${WPFLAGS} | grep restful)
    if [[ -z "${RESTFULTEST}" ]]; then
        wp package install wp-cli/restful ${WPFLAGS}
    fi
    wp wc update --user=${ADMINUSERID} ${WPFLAGS}
fi
wp cache flush ${WPFLAGS}
wp rewrite flush ${WPFLAGS}

It is a true pleasure watching all of the output be produced 😀

Enabling Maintenance mode...
Downloading update from https://connect.advancedcustomfields.com/v2/plugins/download?token=eyJwIjoicHJvIiwiayI6ImIzSmtaWEpmYVdROU5EUTVOamQ4ZEhsd1pUMWtaWFpsYkc5d1pYSjhaR0YwWlQweU1ERTBMVEV4TFRJMElESXlPak01T2pVMCIsIndwX3VybCI6Imh0dHBzOlwvXC9ibGFja29wYWxkaXJlY3QuY29tIn0=...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/black-studio-tinymce-widget.2.6.8.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/contact-form-7.5.1.3.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/disqus-comment-system.3.0.17.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/facebook-for-woocommerce.1.9.14.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/google-analytics-for-wordpress.7.7.1.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/google-sitemap-generator.4.1.0.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://s3.amazonaws.com/gravityforms/releases/gravityforms_2.4.10.zip?AWSAccessKeyId=AKIAJC3LQNDWHBOFBQIA&Expires=1561763195&Signature=%2FTCLodWsM7e7nLcEpXLDOZEDgak%3D...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://s3.amazonaws.com/gravityforms/addons/mailchimp/gravityformsmailchimp_4.6.zip?AWSAccessKeyId=AKIAJC3LQNDWHBOFBQIA&Expires=1561763195&Signature=P6SqpfkzoCno5GrGtxyGShbhbnQ%3D...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://s3.amazonaws.com/gravityforms/addons/userregistration/gravityformsuserregistration_4.1.zip?AWSAccessKeyId=AKIAJC3LQNDWHBOFBQIA&Expires=1561763195&Signature=Lc7JY9TEX3h%2BAi%2BfbW0TuEBP0kw%3D...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/iq-block-country.1.2.5.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/mailchimp-for-woocommerce.2.1.17.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/metorik-helper.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/post-smtp.2.0.2.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/post-types-order.1.9.4.1.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/regenerate-thumbnails.3.1.1.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/shortpixel-image-optimiser.4.14.1.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/simple-301-redirects-addon-bulk-uploader.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/sumome.1.31.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from http://download.thrivethemes.com/thrive-architect-2.2.2.2.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/user-role-editor.4.51.1.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/woocommerce.3.6.4.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/woocommerce-google-analytics-integration.1.4.9.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/yith-infinite-scrolling.1.1.8.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/wordpress-seo.11.5.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Disabling Maintenance mode...
+------------------------------------------+-------------+-------------+---------+
| name                                     | old_version | new_version | status  |
+------------------------------------------+-------------+-------------+---------+
| advanced-custom-fields-pro               | 5.7.7       | 5.8.1       | Updated |
| black-studio-tinymce-widget              | 2.6.3       | 2.6.8       | Updated |
| contact-form-7                           | 5.0.5       | 5.1.3       | Updated |
| disqus-comment-system                    | 3.0.16      | 3.0.17      | Updated |
| facebook-for-woocommerce                 | 1.9.10      | 1.9.14      | Updated |
| google-analytics-for-wordpress           | 7.3.0       | 7.7.1       | Updated |
| google-sitemap-generator                 | 4.0.9       | 4.1.0       | Updated |
| gravityforms                             | 2.3.6       | 2.4.10      | Updated |
| gravityformsmailchimp                    | 4.4         | 4.6         | Updated |
| gravityformsuserregistration             | 3.9         | 4.1         | Updated |
| iq-block-country                         | 1.2.0       | 1.2.5       | Updated |
| mailchimp-for-woocommerce                | 2.1.15      | 2.1.17      | Updated |
| metorik-helper                           | 1.0.4       | 1.3.0       | Updated |
| post-smtp                                | 1.9.5       | 2.0.2       | Updated |
| post-types-order                         | 1.9.3.9     | 1.9.4.1     | Updated |
| regenerate-thumbnails                    | 3.0.2       | 3.1.1       | Updated |
| shortpixel-image-optimiser               | 4.12.1      | 4.14.1      | Updated |
| simple-301-redirects-addon-bulk-uploader | 1.2.3       | 1.2.4       | Updated |
| sumome                                   | 1.30        | 1.31        | Updated |
| thrive-visual-editor                     | 2.1.2       | 2.2.2.2     | Updated |
| user-role-editor                         | 4.47        | 4.51.1      | Updated |
| woocommerce                              | 3.5.1       | 3.6.4       | Updated |
| woocommerce-google-analytics-integration | 1.4.6       | 1.4.9       | Updated |
| yith-infinite-scrolling                  | 1.1.2       | 1.1.8       | Updated |
| wordpress-seo                            | 9.1         | 11.5        | Updated |
+------------------------------------------+-------------+-------------+---------+
Success: Updated 25 of 25 plugins.
Enabling Maintenance mode...
Downloading update from https://downloads.wordpress.org/theme/storefront.2.5.0.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the theme...
Theme updated successfully.
Disabling Maintenance mode...
+------------+-------------+-------------+---------+
| name       | old_version | new_version | status  |
+------------+-------------+-------------+---------+
| storefront | 2.3.2       | 2.5.0       | Updated |
+------------+-------------+-------------+---------+
Success: Updated 1 of 1 themes.
Updating to version 5.2.2 (en_US)...
Downloading update from https://downloads.wordpress.org/release/wordpress-5.2.2-new-bundled.zip...
Unpacking the update...
Cleaning up files...
No files found that need cleaning up.
Success: WordPress updated successfully.
Installing package wp-cli/restful (dev-master)
Updating /www/blackopaldirect_583/.wp-cli/packages/composer.json to require the package...
Using Composer to install the package...
---
Loading composer repositories with package information
Updating dependencies
Resolving dependencies through SAT
Looking at all rules.

Dependency resolution completed in 0.477 seconds
Analyzed 7043 packages to resolve dependencies
Analyzed 620901 rules to resolve dependencies
Package operations: 1 install, 0 updates, 0 removals
Installs: wp-cli/restful:dev-master ba21220
 - Installing wp-cli/restful (dev-master ba21220)
Writing lock file
Generating autoload files
---
Success: Package installed.
Found 13 updates (wc_update_343_cleanup_foreign_keys, wc_update_343_db_version, wc_update_344_recreate_roles, wc_update_344_db_version, wc_update_350_reviews_comment_type, wc_update_350_db_version, wc_update_352_drop_download_log_fk, wc_update_354_modify_shop_manager_caps, wc_update_354_db_version, wc_update_360_product_lookup_tables, wc_update_360_term_meta, wc_update_360_downloadable_product_permissions_index, wc_update_360_db_version)
Updating database  100% [========================================================================================================================] 0:04 / 0:06
Success: 13 update functions completed. Database version is 3.6.0
Success: The cache was flushed.
Success: Rewrite rules flushed.

So wonderful to watch! Happy automated updates 🙂

Sources

wp plugin update
wp theme update
wp core update
wp core update-db
wp wc wiki