I’ve started experimenting with WP CLI, which stands for WordPress Command Line Interface. Essentially, it allows an efficiency loving developer the option to do many of the routine maintenance and set up of a WP site completely through a command line, rather than the GUI backend interface.
While on paternity leave, I finally had a chance to dive into this and I’m happy to have discovered how much faster these tasks run. It seems like the routine updating of plugins, themes, and core would take several minutes. I can now install and activate a plugin in a matter of seconds with a command like:
wp plugin install --activate simple-history
the process for updating themes is just as simple
wp theme update --all
For downloading and updating both a premium privately hosted theme and TwentyFifteen took only 18 seconds on WP CLI
Some plugins won’t update this way, for example, the version of WPBakery Page Builder that comes packaged with a premium theme I frequently use, The7. This has to be done via the GUI instead, since the update process goes through a separate plugins menu than the default WP one.
Setting this up for local work required a little configuration work to get it working with my MAMP Pro environment. First I need to go into the MySQL tab and check the box for “Allow network access to MySQL“
Second, for each site I wish to work on this way, I need to change the setting for
127.0.0.1 instead of
Otherwise, I’ll get an error notice in the terminal like
Error: Error establishing a database connection. This either means that the username and password information in your `wp-config.php` file is incorrect or we can’t contact the database server at `localhost`. This could mean your host’s database server is down.
The commands and workflows I use the most:
Force re-install all plugins.
This is useful in the event that you think your website has been hacked or corrupted, and you want to have a clean start.
wp plugin install $(wp plugin list --field=name) --force
or for themes:
wp theme install $(wp theme list --field=name) --force=
or for core:
wp core download --skip-content --force
Other more basic commands can be found at https://developer.wordpress.org/cli/commands/
Change a User’s Password via the Command Line
wp user update USERNAME user_pass="PASSWORD"
once can also substitute the userid for the username.
For more actions you can do to update an existing user, refer to the documentation at https://developer.wordpress.org/cli/commands/user/update/
Converting WordPress Database Tables from MyISAM to InnoDB with WP-CLI
1) Check if any of your tables are using MyISAM instead of InnoDB
$: wp db query "SHOW TABLE STATUS WHERE Engine = 'MyISAM'" --allow-root
You can easily back up the database with this command
$: wp db export before-engine-change.sql --all-tablespaces --add-drop-table --allow-root
You can easily restore with this command
$: wp db import before-engine-change.sql --allow-root
2) While you can convert each table through phpMyAdmin, it’s not very practical if you have lots of tables to convert. The following commands will batch convert all MyISAM tables to InnoDB over SSH with WP-CLI.
$: wp db query "SELECT CONCAT('ALTER TABLE ', TABLE_SCHEMA,'.', TABLE_NAME, ' ENGINE=InnoDB;') FROM information_schema.TABLES WHERE ENGINE = 'MyISAM'" --skip-column-names > db_optimize.sql
$: wp db query < db_optimize.sql
$: rm db_optimize.sql
3) Check the website to make sure it still works. If it doesn’t, restore it. If it does, hooray! Database will be faster, use less server resources and be more recoverable. Go ahead and remove the backed up db file since it’s no longer needed.
$: rm before-engine-change.sql
If you run into permission problems doing this, then consider just using a plugin for this operation, Vevida Optimizer. After installing it, go to Tools -> Convert DB tables. This will launch the utility that converts MyISAM tables to InnoDB. The awesome LiteSpeed Cache plugin also has a feature that lets you see which tables are using MyISAM and convert them over to InnoDB
Cleaning Up Post Revisions
Easily deleting post revisions from your site database will help keep the database cleaned up. Streamlining the database can result in performance improvements, especially as the size of the database grows. First, you’ll need to install a 3rd party package. Read about here: https://github.com/trepmal/wp-revisions-cli
To install the package for WP-CLI, run the following command:
wp package install trepmal/wp-revisions-cli
After the package WP Revisions has been installed, to clean all post revisions, you can use the following command. Please note: this command can be slow, since it will query post revisions before deleting them.
wp revisions clean -1
If you want to remove all but the latest 5 revisions, use:
wp revisions clean 5
If you wanted to delete all post revisions before a specific date, you can include that in the command. For example:
wp revisions clean --before-date=2019-06-10
If you needed to clean all post revisions other than those for a specific post type, include that post type at the end of the command. For example, revisions for the WooCommerce created product post type would not be deleted if you run this command:
wp revisions clean --post_type=product
For a faster method to delete all post revisions, you can run this command:
wp revisions dump --hard
To list all existing post revisions, you can run this command:
wp revisions list
Deleting All Files from Media Library
wp post delete --force $(wp post list --post_type='attachment' --format=ids)
wp post list for additional information.
Install an alternate version of WordPress Core
With version 5.6, the long awaited upgrade of jQuery does introduce some problems with certain themes/plugins. In those cases it’s nice to be able to quickly take things back to where they were, without the usage of a plugin like WP Rollback. You’ll first want to figure out what version of WP Core you’re downgrading/upgrading to. Viewing the documentation for wp-cli’s wp core options tells us the command is:
wp core update --force --version=5.5.3