How to Debug Your WordPress Website from the Command Line Using WP-CLI
In this series of blog posts, we will cover the basic but useful tasks WP-CLI has to offer, such as upgrading WordPress core and managing your plugins. By the end of the series, you’ll be comfortable using WP-CLI and discovering even more ways you can use it to simplify your life!
In some cases, it is useful to have direct access to your database for debugging purposes or for manually editing an entry. WP-CLI has the ability to use the authentication information in the wp-config.php file to give you access to your database. To do this run the following:
[user@server]$ wp db cli
There is a possibility that when you update something, it may not update immediately; this could be caused by caching plugins not invalidating some objects when they should have. In order to fix this, you need to flush your cache. WP-CLI has the ability to do this as well:
[user@server]$ wp cache flush
If you have added a rewrite rule, it is likely that you need to manually flush your rewrite rules in order for it to take effect. You can do that with WP-CLI as well:
[user@server]$ wp rewrite flush
A –hard flag may be needed in some circumstances to properly flush rewrite rules.
There may also be circumstances where you would want to replace all occurrences of a word on your site with another word, such as a company rename or rebranding. WP-CLI lets you do this very easily:
[user@server]$ wp search-replace <old> <new>
This command also has several useful flags you may want to look into, including –regex, –dry-run, and –verbose. These give you different functionality or debugging information:
- –regex flag: allows you to search using regular expressions.
- –dry-run flag: shows you what it would change but not actually make changes.
- –verbose flag: shows extra information that can be useful for debugging.
Some WordPress plugins have WP-CLI support as well. For example, Varnish lets you flush the Varnish cache from the command line using:
[user@server]$ wp varnish purge
To get more information about any of these commands, run:
[user@server]$ wp help <command>
[user@server]$ wp help cache flush
And that’s a wrap on this series! We hope you enjoyed it. Since discovering WP-CLI, our love for WordPress went up to 11, and we hope this introductory series has done the same for you!