GICT knowledge base
[Ultimate Guide] How to use WP-CLI to manage WordPress sites
Published
WP-CLI is one of the most useful tool if you are managing multiple WordPress sites. Especially useful for developers and system administrators. WP-CLI is exactly as it sounds. It allows you to manage your WordPress sites from command line interface. It allows you to perform almost all the operations possible on a WordPress site. It also allows you to deploy new WordPress sites, change existing settings, manage plugins & themes and also enable/disable the maintenance mode for your WordPress sites.
It is also helpful if you want to automate WordPress deployment and management. You can create a simple shell script to fully deploy a new instance of a WordPress site with specific plugins, themes and settings. In this detailed guide, we will deep dive into WP-CLI and understand How to use it. Before we get started, Here are some prerequisites.
Prerequisites
To manage your WordPress sites with WP-CLI, You need.
- A VPS or VM or a Dedicated server. If you don’t have one, You can create a new one.
- Root access to your VPS/VM or dedicated server.
We need root access to install WP-CLI on the server. You also must have PHP installed on your server. We recommend you to have a full LAMP stack on Ubuntu to host a WordPress site without any issues.
Let’s get started with the installation.
Install WP-CLI on Ubuntu 22.04
To install wp-cli in Ubuntu, Follow the steps given below. First of all, execute the following command to download the phar file.
# curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Now, Execute the following command to make sure that the wp-cli file is working correctly.
# php wp-cli.phar --info
If it works correctly, We can execute it to manage our WordPress sites. However, We need to make it executable first. To make it executable, run the following command.
# chmod +x wp-cli.phar
Now, We can make it global by moving it to the /usr/local/bin directory in Ubuntu. We will rename it to wp. To do so, execute the following command.
# sudo mv wp-cli.phar /usr/local/bin/wp
You can now execute the wp command to verify that it is working correctly. Execute the following command to do so.
# wp --info
That’s all for the installation part. Now, Let’s learn how to deploy a new WordPress site using wp-cli in Ubuntu.
Create a WordPress site with WP-CLI
To create a new site with WP-CLI, Navigate to the document root of your WordPress site. If you have a basic LAMP stack installed, You can create your first site inside /var/www/html directory. Execute the following command to move to that directory.
# cd /var/www/html
Next, execute the following command to download WordPress core files on your server.
# wp core download
This command will download and extract latest WordPress core files on your server in the current directory. You can also use the path parameter to define the directory for your WordPress site. For example, If you want to create a WordPress site under blog directory, execute the following command.
# wp core download --path=blog
Once the files are downloaded, We can create the database connectivity. Considering you already have the database ready, execute the following command to create a config file with specified database information.
# wp config create --dbname=DATABASE_NAME --dbuser=DATABASE_USERNAME --prompt=dbpass
It will ask you for the database password. Enter the correct password and it will generate the basic wp-config.php for you along with the database information. Next, You can visit the domain or the IP address of the site in browser and continue with the installation.
If you do not want to install it from the browser, You can execute the following command to install it directly from the command line interface.
# wp core install --url=http://18.136.208.234 --title="WP CLI Installed" --admin_user=test --admin_password=test [email protected]
So, That’s all for the installation. Now let’s see how to manage other aspects of a WordPress site using WP-CLI.
Export WordPress Data using WP-CLI
You can use WP-CLI to export WordPress data to WXR file. Execute the following command to export WordPress data using wp-cli.
# wp export
It will create an xml file with all the WordPress data in the current directory. You can also specify the following options with this command.
- Directory: Set
--dir=/path/to/dirto the command to create a backup file in other directory. - Start and End Date: If you want to export data created between a specific time frame, You can specify
--start_dateand--end_datein the command. The date should be in YYYY-MM-DD format. - Post Type: If you only want to export a specific post type, pass the
--post_typeparameter in the command. - Author: To export data for a specific author only, use the
--authoroption in the command. - Category: You can use the
--categoryparameter of the wp export command to export posts from a specific category only. - Post Status: You can export posts based on the current status of the post using
--post_statusoption.
For example, If you want to export all the posts from 2022 by admin user, execute the following command.
$ wp export --dir=/tmp/ --author=admin --post_type=post --start_date=2022-01-01 --end_date=2022-12-31
Now, Let’s see how to import WordPress data from an XML file using wp-cli.
Import WordPress Data using WP-CLI
Import command is pretty straight forward. You just have to specify the XML file in the command just like the following example.
$ wp import example.wordpress.2022-05-21.xml --authors=create
Note that --authors=create parameter in the command tells WP-CLI to create new authors if does not exist.
Enable/Disable Maintenance Mode using WP-CLI
WP-CLI maintenance mode command can help you enable or disable maintenance mode for your WordPress site with a single command. Make sure to navigate to a specific WordPress installation before executing the mainteanance mode command.
Execute the following command to enable the maintenance mode.
$ wp mainteanance-mode activate
It will instantly activate the maintenance mode for your WordPress site. To disable the maintenance mode, execute the following command.
$ wp maintenance-mode deactivate
If you want to check if your WordPress site is in maintenance mode, You can execute the following command to check the status of your WordPress site.
$ wp maintenance-mode status
So, this is how you can manage the maintenance mode for your WordPress site using WP-CLI. Now, Let’s see how to manage themes and plugins using WordPress command line interface.
Manage WordPress Plugins with WP-CLI
WP-CLI provides you a set of commands to help you manage plugins in your WordPress sites. Here are some of the examples.
To list all the plugins currently installed on your WordPress site, execute the following command.
$ wp plugin list
This command will display a table with the plugin name, status (active/inactive), version, and other relevant details.
Now, Let’s see how to install a new plugin using wp-cli. Execute the following command.
$ wp plugin install hello
Here, we are using “hello” slug for example. You can replace it with the slug of the plugin you want to install. Similarly, You can replace install with activate, deactivate, update, and delete to perform install, activate, and delete action on the plugin respectively.
Manage WordPress Themes with WP-CLI
WP-CLI also provides commands to manage WordPress themes effectively. Here’s how you can interact with themes using the command line. First of all, execute the following command to list all the themes installed in your current WordPress installation.
$ wp theme list
This command will display a table with the theme name, status (active/inactive), version, and other relevant details. To install a new theme, execute the following command.
$ wp theme install theme-slug
Do not forget to replace theme-slug with the actual slug of the theme you want to install in your WordPress site. And, Just like plugins, You can replace install action with activate, deactivate, update, and delete actions.
Manage Users with WP-CLI
Just like themes and plugins, You can also manage users on your WordPress site using wp-cli. Execute the following command to get a list of users in your WordPress site.
$ wp user list
To create a new user, You can execute the following command.
$ wp user create username [email protected] --role=role_slug
Do not forget to replace username and [email protected] with the desired username and email address of the new user, respectively. Also, specify the user role using --role=role_slug, where role_slug is the desired user role (e.g., administrator, editor, author, etc.).
To update the user information, the command is as given below.
$ wp user update user_id --user_login=new_username --user_email=new_email
Note that you would have to enter the user_id in this command to identify the correct user and update the information. Now, If you want to delete a user, You can simply run the following command.
$ wp user delete user_id
In the delete command too, the user_id is required to make sure you are deleting the correct user.
Conclusion
In conclusion, WP-CLI is a powerful tool that simplifies WordPress site management through the command-line interface. From site deployment to data handling and plugin management, WP-CLI offers developers and administrators a fast and efficient way to handle multiple WordPress tasks. Embracing WP-CLI can significantly enhance productivity and streamline workflows for anyone managing WordPress sites. If you have any questions, feel free to let us know in the comment section given below.
