- Your cart is currently empty.
WP-CLI Basics: When and How to Use It
WP-CLI has been around for a while, but has recently become increasingly popular among WordPress users and programmers. You’ve probably heard of the tool, but haven’t had the time or courage to use it yet.
Here’s what it is and how you can use it to get more done in less time and increase your productivity.
This tool allows you to interact and manage your WordPress websites via the command line. SSH access is available on our hosting packages with the WP-CLI tool pre-installed, so you don’t need to install it manually.
Using WP-CLI
Almost all the tasks that you can edit within the WordPress control panel can be done with the tool via the command line. You don’t have to open a browser and wait for the website administration to refresh for each task.
Some basic tasks that can be edited using WP-CLI commands:
- Installing a new WordPress website;
- updating WordPress, graphical templates and plugins;
- print out all plugins and status;
- making backup copies and restoring them;
- adding and editing user profiles;
- MySQL database editing(search & replace).
The full list of WP-CLI commands can be found at the following link: https://developer.wordpress.org/cli/commands/
To execute WP-CLI commands, you must first navigate to the root folder where your WordPress website is located. After a successful connection via SSH command line, you can navigate between directories using the cd command (e.g.“cd name-folder“). You can also execute commands from any folder, but in this case you need to add an additional parameter –path=PATH, which requires you to manually enter the path to your WordPress installation, which is more time consuming.
Examples of WP-CLI commands
Although the WP-CLI tool is pre-installed, you need to make sure that the appropriate packages are installed from the repository before you can use the commands.
Example of installing a package from the repository
1. Assuming you want to use WP-CLI to check the performance of your WordPress site, use the following command:
wp profile stage --all --spotlight
2. Since you haven’t installed the package yet, the command prompt gives you an error:
Error: 'profile' is not a registered wp command. See 'wp help'.
3. Therefore, to install a package that will allow the command to be executed, use the following command:
wp package install wp-cli/profile-command
Once the package installation is complete, you can try again with the command in step 1.
So, before we show you further examples of how to use the WP-CLI commands, install the packages you will need to execute them.
Link to the packages: https://wp-cli.org/package-index/
USER EDITING
List of all users:
wp user list
Add a new user:
wp user create USER_NAME E_Mail_NASLOV --role=USER
In the above command, instead of USERNAME, enter the username you want to assign to the user. Instead of E_MAIL_NAME, enter the email address of the new user. In addition, you must also assign the user rights or a role on the website. If you want to add a new administrator, you can enter administrator instead of ROLE.
Deleting a user:
wp user delete USER_NAME
Instead of USER_NAME, you can enter the actual username or user ID (you can check it with the first “wp user list” command).
For all commands, there are also additional parameters that you can use to customise the command to your needs(list of commands). Clicking on a command will open a new page where you can see all the additional parameters. Alternatively, you can also check the parameters by typing a basic command (e.g.“wp db“) in the command line and more information about that command will be displayed.
EDITING A MYSQL DATABASE
Export MySQL database:
wp db export
As the name of the command suggests, you can use this command to export a database to the root directory of your website. The name of the database that was exported will also be displayed.
MySQL database import:
wp db import IME_BASE
Instead of the IME_BAZE field, enter the name of the file you previously uploaded to the root directory of the WordPress site. You don’t need to enter a username and password to access the database, as the command gets the information from the wp-config.php file.
“Search & Replace” the data in the database:
wp search-replace 'http://www.vasadomena.si' 'https://www.vasadomena.si'
As the example above shows, you can use the search-replace command to replace all words or phrases in a MySQL database. The above example is useful when you install an SSL certificate on your hosting package and need to correct all URLs from “http” to “https”.
MySQL database optimization:
wp db optimize
This command is used to run the MySQL database optimization. In principle, you should not (negatively) affect the performance of your website with this command, but we advise you to make a backup copy with the wp db export command before any such intervention.
When making a backup copy of a MySQL database, it is very important that you also remove the database after the work has been done. If you leave the database in SQL format loaded on your hosting package, it may fall into the wrong hands. MySQL databases contain all the data about your website, orders, customers, etc.
The WP CLI tool is not just for the above tasks, but can do much more. The commands listed above are just a basic introduction to the tool, and you can find a list of all the commands on the official WP-CLI site.
There are also many WP-CLI add-ons available on the web that can be used to find causes of slow pages, createdummy content, etc.
COMMENT THE POST
Your comment has been successfully submitted
The comment will be visible on the page when our moderators approve it.