What Is WP-CLI? How to Use WP-CLI (Beginner’s Guide)

Chưa được phân loại

WP-CLI is the official command-line interface for WordPress. It lets you manage your WordPress site, themes, and plugins from the terminal, which is often a more convenient way for moderate to advanced WordPress users (or developers) to work with WordPress sites.

In this post, you’ll learn what you need to know to get up and running with WP-CLI and some of the most useful commands you can run to make your life a lot easier and save time while working with WordPress.

📚 Table of contents:

👉 Note: While you don’t need to be a full-on WordPress developer to use and benefit from WP-CLI, it is still definitely a tool that’s only for more advanced users with at least some technical knowledge, such as the knowledge/ability to connect to your server via SSH.

If you’re a total beginner, we recommend just sticking with the WordPress dashboard and skipping WP-CLI. For example, if you have no idea what the “command line” is, you should probably skip WP-CLI.

What is WP-CLI? With some examples

WP-CLI, short for WordPress command-line interface, lets you manage all aspects of your WordPress site from a command-line interface that looks something like this:

WP-CLI Example

Once you connect to your site via SSH, you can enter simple commands to perform a variety of actions including the following:

  • Installing the WordPress software, themes, and/or plugins
  • Activating/deactivating/updating WordPress, themes, and plugins
  • Creating or managing users, including changing passwords and other user information
  • Running a search/replace on the WordPress database
  • Generating test data for a sandbox site
  • Editing or optimizing your database, such as deleting post revisions
  • Creating a child theme
  • Resetting WordPress (be careful with this one because it will completely blank your site)
  • Managing comments
  • …lots more

What do you need to use the WordPress command-line interface?

In order to use WP-CLI, you’ll need to have WP-CLI installed on your hosting server (it does not come with WordPress by default).

Note that WP-CLI is not a plugin that you can install on your site. Instead, it’s something you need to install directly on your server, which is a bit more complicated.

Thankfully, most WordPress-focused hosts offer WP-CLI installed by default. This is true for:

If your host doesn’t pre-install it for you, you can also install WP-CLI yourself via SSH as long as you have enough access to your server (which you may or may not have, depending on your host). You can find the full list of methods here.

The basic requirements for your server are:

  • A UNIX-like server environment – you can install WP-CLI on a Windows server, but it’s a lot more complicated. Note – this refers to the operating system of your WordPress hosting server, not the computer that you’re using.
  • PHP 5.6+ – ideally PHP 7.2+ (for many other reasons).
  • WordPress 3.7+ – ideally the latest version of WordPress.

How do you access the WordPress command-line interface?

Once you’ve verified that WP-CLI is installed on your hosting server, you can access it by connecting to your server via SSH, which you can accomplish using Terminal on macOS/Linux or a tool such as PowerShell or PuTTY on Windows.

Once you’re connected, you can work with WP-CLI using commands, which is what we’ll detail in the next section.

What can you do with WP-CLI? Examples of useful commands

Now that you know what the WordPress command-line interface is, let’s go over some of the many commands that you can run to manage your WordPress sites.

WP-CLI install WordPress commands

To create a new WordPress install, you can use the wp core install command with additional parameters to control the basic site details. For example:

wp core install --url=example.com --title=Example --admin_user=supervisor --admin_password=strongpassword --admin_email=info@example.com

👉 Full documentation

WP-CLI login and user-related commands

There are a number of WP-CLI login and user-related commands. You can manage all aspects of users, including resetting a user’s password.

Here are some examples of what you can do:

Change/reset a user’s password:

wp user update USERNAME --user_pass=PASSWORD

Create a new user:

wp user create bob bob@example.com --role=author

This will randomly generate a password, but you could also manually enter a password using the same user_pass command.

Assign a certain user role to a user:

wp user set-role USERNAME editor

👉 Full documentation

WP-CLI plugin commands

There are a number of WP-CLI plugin commands to help you install, activate, deactivate, or delete plugins.

The core command is wp plugin, followed by the action you want to perform and the slug of the plugin. Some examples:

  • wp plugin install feedzy-rss-feeds
  • wp plugin activate feedzy-rss-feeds
  • wp plugin deactivate feedzy-rss-feeds

👉 Full documentation

WP-CLI theme commands

You can also use the same types of commands to manage your site’s themes. The base command is wp theme and you can modify it as needed. Some examples:

  • wp theme install neve
  • wp theme activate neve

👉 Full documentation

WP-CLI update commands

Keeping your WordPress site and its extensions updated is essential to the security and functioning of your site. Thankfully, WP-CLI makes it quite easy to update various parts of your WordPress site:

  • wp core update – automatically updates to the most recent version (but you can also specify a specific version).
  • wp plugin update --all – updates all plugins. Or, you can also specify individual plugins. For example, wp plugin update feedzy-rss-feeds.
  • wp theme update --all – updates all themes. Or, you can also specify individual themes. For example, wp theme update neve.

👉 Full documentation

WP-CLI test data generation

If you need to create a sandbox WordPress site with dummy data to test something, WP-CLI makes it super easy to generate varying levels of dummy data.

The command here is “generate” and you can apply it to a bunch of different base commands. Some examples:

  • wp post generate --count=30 – generates 30 posts.
  • wp user generate --count=5 --role=author – generates five users with the author role.
  • wp term generate --count=15 – generates 15 taxonomy terms.
  • wp comment generate --count=5 --post_id=123 – generates five comments and assigns them to the post with ID 123.

WP-CLI search/replace commands

WP-CLI lets you run a search/replace on all or some of your database. For example, you could update all of your internal URLs to use HTTPS if you recently moved to HTTPS or change from a staging URL to the live URL of your site.

Be careful with this command as running a search/replace on your database can break your site if you make a mistake. You should also make sure to use the dry run feature to test your changes before making them permanently.

wp search-replace 'http://example.test' 'http://example.com'
  • changes all instances of http://example.test to http://example.com
wp search-replace 'http://example.test' 'http://example.com' --dry-run
  • performs a test run – you would remove --dry-run to run the actual search/replace.

👉 Full documentation

Explore all of the WP-CLI commands

The above example commands are just a sampling of the many things that you can do with WP-CLI on your site.

To learn more about the many core commands in WP-CLI, you can check out the full documentation at WordPress.org, which details every command with lots of examples.

Many plugins also support WP-CLI

The cool thing about WP-CLI is that, in addition to the “default” commands that we detailed above, many WordPress plugins also support their own WP-CLI commands, which you can often find by consulting your plugin’s documentation.

For example, let’s say you’re using the UpdraftPlus plugin to create backups of your site. Well, instead of creating those backups manually via your WordPress dashboard, you could manually or automatically initiate those backups using the following command:

wp updraftplus backup

There are lots of modifiers you can add, such as only backing up specific parts of your site. You can find all of this information in the UpdraftPlus documentation.

You could also run these commands using a Cron job, which lets you set up your own custom backup schedule, even if you’re only using the free version of UpdraftPlus (usually, creating such a schedule would require the paid version).

This is just one example – think about the plugins that you use on your own sites and you might be able to come up with some useful workflows.

Get started with the WordPress command-line interface today

For intermediate to advanced WordPress users and developers, WP-CLI provides a more efficient and flexible way to manage WordPress sites.

For the easiest way to get started, you can choose one of the many popular WordPress hosts that offer pre-installed WP-CLI. Then, play around with some of the commands in this post to start seeing how it can benefit you.

Do you have any questions about how to use the WordPress command-line interface? Ask away in the comments!

Free guide

5 Essential Tips to Speed Up
Your WordPress Site

Reduce your loading time by even 50-80%
just by following simple tips.

* This post contains affiliate links, which means that if you click on one of the product links and then purchase the product, we’ll receive a commission. No worries though, you’ll still pay the standard amount so there’s no cost on your part.

Bài viết liên quan

Trả lời

Email của bạn sẽ không được hiển thị công khai.