Welcome back to our dive into the world of WP-CLI! This is the second and final part. If you missed it, our first post went over the installation and basic usage of WP-CLI, you can go back and check it out here if you want to. Now that we’ve covered the basics, in our second part we have put together our top 10 WP-CLI tools. These tools will assist any WordPress user with a variety of different tasks, many of which usually require time-consuming, manual setting modifications or the installation of extra plugins.
- Changing Your Site URL
On some occasions it may be necessary to update your WordPress site URL. This can usually be done through your WordPress dashboard. However, this is no good in some situations, such as if you are unable to gain access to your dashboard due to theme or plugin errors. In those cases, you can use the following commands to set your new URL.
wp option update home https://www.yournewdomain.com
wp option update siteurl https://www.yournewdomain.com
- Downgrading Your WordPress Version
If you’ve ever updated your WordPress site, only to find it broken in the aftermath and without a recent backup on hand, you’ll know how tricky it can be to manually downgrade your WordPress version. WP-CLI supports a very simple way to downgrade to any WordPress version that you specify, saving you the headache of manually downgrading by copying files back and forth over FTP.
For example, you would run the following command if you wanted to go from 5.4.1 to 5.3.1. You can find a full list of WordPress versions here for your reference.
wp core update –version=5.3.1 –force
- Listing all wp-config.php options
Rather than digging through your wp-config.php file to find what settings you’re using, WP-CLI includes a handy command to list all of your settings in a neat table.
wp config get
You will see a table including options like your database table prefix, your database username and password, wp_debug settings, and any other custom options you may have set in wp-config.php.
- Database Search and Replace
Running a search and replace on your database generally requires a third party plugin such as Better Search Replace, or advanced knowledge of SQL queries and phpMyAdmin.
WP-CLI allows you to run a search and replace with one simple command, without the need for any other tools. One of the most common use cases for a database search and replace is when moving your site to a new domain, or updating one colour code to another across your entire site.
The following command specifies a “dry run”, so that you can review what will be updated before pulling the trigger. Simply execute the command without ‘–dry-run’ to actually make the changes to your database:
wp search-replace “old-domain.com” “new-domain.com” –all-tables –dry-run
WP-CLI will give you a full output of what was changed.
- Dump/Export Your Database
Building on the previous point of working with databases, the following instructions will allow you to quickly and easily export your database. No need to use a WordPress migration plugin, or manually log into phpMyAdmin to run an export/import. This method is generally much, much faster than the alternatives.
Export a database, where ‘dbfilename.sql’ is the destination to export the database to:
wp db export dbfilename.sql
- Import A Database
wp db import dbfilename.sql
- Reinstall WordPress Core
Sometimes a WordPress update will fail halfway through, corrupting your WordPress core files. This command is particularly useful if you have corrupt or missing WordPress files and want to reinstall without going through the hassle of changing your plugins/themes, or manually copying over fresh files.
wp core download –skip-content –force
The –skip-content flag will not download the default WordPress plugins and themes, such as Twenty Twenty and Hey Dolly.
- Manage Maintenance Mode
Without a plugin, WordPress maintenance mode cannot be controlled via your WordPress admin page. WP-CLI includes a command to activate and deactivate maintenance mode. If maintenance mode is enabled, it will display a ‘down for maintenance’ page to visitors.
wp maintenance-mode activate
wp maintenance-mode deactivate
wp maintenance-mode status
- Generating Dummy Content For Testing
Having plenty of placeholder content can be one of the most useful things to have on hand when building a new WordPress site. The following command will generate 30 WordPress posts, however you can set the count to any number you wish.
wp post generate –count=30
The same command can be performed to generate test users.
wp user generate –count=5 —role=editor
There are plenty of different flags you can use to change the criteria of the generated content. For a full list, you can visit the WP-CLI site for more information.
- Generate A List Of Your Current wp-cron Events
wp-cron handles scheduled events for a WordPress site. Over time, plugins that are installed will add to the list of actions that are executed when wp-cron runs. What those exact events are can be quite hard to track down without a plugin, however WP-CLI offers a command to list all wp-cron events.
wp cron event list
Running the above command will generate a table list of all cron events, what they are, and when they’re next scheduled to run.
As always, if you have any questions about this post or our shared hosting, VPS, reseller or dedicated server plans, simply call us on 1300 MY HOST (694 678) during business hours, or submit a ticket through our Support Portal and one of the crew will be in touch!