If you’re a #WordPress developer, you’ve probably heard of WP-CLI, the command-line interface for WordPress. But what exactly is it? And why should you care? And did you know you can also use it locally?
WP-CLI is a tool that allows you to manage your WordPress sites from the command line. This means you can perform tasks like creating and updating posts, managing plugins and themes, and even backing up your site, all without having to log in to your dashboard.
So, why should you use WP-CLI?
It’s faster. It’s often faster than using the WordPress dashboard, especially for tasks that you repeat often.
It’s more powerful. It can do things that you can’t do with the dashboard, like managing your site’s .htaccess file or running scripts.
It’s more flexible. It can be used to automate tasks and integrate with other tools, making it a powerful tool for developers.
I recently started freelancing on a WordPress project and decided to try it out. Since I prefer to run WordPress locally on my machine and because my Mac is still kicking at 10+ years old, I went old school and used #MAMP. (Docker doesn’t work on older machines š«).
I wanted to try running a search and replace on the imported database locally to find all instances of the staging url and replace it with my localhost url. Normally, I would use Migrate DB Pro, which handles this automatically but wanted to try a different method. WordPress plugins? Donāt know her. š
After installing, I navigated to the root level of my WP install, the same level as my wp-config file. I ran “wp option get siteurl” to test the correct url was getting returned and I got an error.
After reading through the WP-CLI docs, #StackOverflow, and general Googling, I found out that in your wp-config file, you need to add the port number to your database hostname.
Once that was done, I ran “wp search-replace ‘staging-url’ ‘localhost url'” and it worked like a charm. If you want, you can even target the search and replace to specific tables.
There are a ton of commands available, so it’s recommended to learn the basics before you can use it effectively.
There are a few great resources available for learning WP-CLI:
The official WP-CLI documentation: https://make.wordpress.org/cli/
The WP-CLI Cookbook: https://github.com/wp-cli/wp-cli
The WP-CLI Community: https://wordpress.slack.com/messages/C02RP4T41
WP-CLI is a powerful tool that can make life much easier. If you’re not already using it, I highly recommend that you give it a try!