Get the Best WordPress Backup
& Migration Plugin Today
Get Duplicator Now
Announcement Duplicator WP-CLI commands

Announcing Duplicator WP-CLI Commands (And How They Speed Up Backups)

Looking for a smarter way to manage site backups? Explore Duplicator's new features, including customizable WP-CLI commands and permission settings!
How to use WordPress CLI

How to Use WordPress CLI 

Written By: author avatar Joella Dunn
author avatar Joella Dunn
Joella is a writer with years of experience in WordPress. At Duplicator, she specializes in site maintenance — from basic backups to large-scale migrations. Her ultimate goal is to make sure your WordPress website is safe and ready for growth.
     Reviewed By: reviewer avatar John Turner
reviewer avatar John Turner
John Turner is the President of Duplicator. He has over 20+ years of business and development experience and his plugins have been downloaded over 25 million times.

Have you ever clicked through endless menus in the WordPress dashboard just to update a plugin or two?

Or maybe you’ve spent precious time manually backing up your site, wishing there was a faster way? There is!

WP-CLI is a powerful tool that lets you manage your WordPress site directly from the command line. Think of it as having a secret control panel where you can do pretty much anything without logging into the WordPress admin area.

Learning WP-CLI might sound intimidating at first. But trust me, it’s a game-changer. It speeds up your workflow, automates repetitive tasks, and gives you more control over your site.

This tutorial will show you how to install, understand, and use WP-CLI effectively.

Ready to transform how you work with WordPress? Let’s dive in.

Table of Contents

What Is WordPress CLI?

WordPress CLI, or WP-CLI, is a command-line interface for WordPress. This might sound technical, but it simply means you can interact with your WordPress site using text commands instead of clicking buttons in the usual dashboard.

Instead of logging in through a web browser, you use a terminal or command prompt on your computer. You type in commands, and WP-CLI executes them, performing actions like updating plugins, creating users, or backing up your database. It’s a different way of managing your site.

Essentially, WP-CLI allows you to talk to WordPress directly using commands. This method is not only efficient but opens up new possibilities for automation and advanced management.

Developers love WP-CLI because it allows you to quickly manage and edit WordPress sites. You type precise commands that can update, configure, and troubleshoot your website in seconds.

The Benefits of Using WP-CLI

WP-CLI isn’t just another tool — it’s a game-changer for WordPress management. Here’s why it can revolutionize how you work with WordPress sites.

Speed is the first major advantage. Manual tasks that consume hours can be completed in minutes or even seconds.

Updating 20 plugins? That’s a single command. Creating multiple user accounts? Another quick line of text.

Automation becomes effortless with WP-CLI. Repetitive tasks can be scripted and scheduled. You can create bash scripts that manage multiple WordPress sites simultaneously, dramatically reducing workload.

For developers, WP-CLI offers unprecedented control. Want to quickly install a plugin, create a child theme, or perform complex database operations? These become straightforward command-line tasks.

Debugging becomes more accessible. You can retrieve site information, check plugin compatibility, and diagnose issues without navigating through multiple WordPress admin screens.

Remote management is another powerful benefit. You can manage WordPress installations without needing direct graphical access. A command line connection is all you need.

Scripting capabilities mean you can integrate WordPress management into larger automation workflows. Combine WP-CLI with other tools like Git, Ansible, or custom scripts for comprehensive site management.

Is WP-CLI Installed By Default?

No, WP-CLI does not come pre-installed with WordPress.

This means that even if you have a WordPress site running on your server, you’ll need to take extra steps to set up WP-CLI. It’s a separate tool that requires manual installation.

WP-CLI is not part of the standard WordPress core files or bundled with typical hosting control panels. You’ll need to install it manually on your server.

Most web hosting environments support WP-CLI, but it’s always good to check with your hosting provider to confirm compatibility and get specific installation guidance.

How to Install WP-CLI

To install WP-CLI, make sure your server meets the requirements. You’ll need:

  • PHP 5.6 or higher: WP-CLI is a PHP application, so make sure you have a compatible PHP version installed.
  • SSH access: You’ll need access to your server’s command line via SSH to run the installation commands. Most hosting providers provide SSH access.

Then, open terminal, puTTY, or Ubuntu. This depends on your computer’s operating system.

In the command line, connect to your server with this command:

Clean Code Block
Copied!

ssh username@hostname
        

Then, download the Phar build to install and manage WordPress CLI:

Clean Code Block
Copied!

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
        

Check to see if the file was downloaded:

Clean Code Block
Copied!

php wp-cli.phar --info
        

Finally, you’ll need to make this file executable. This is also the time to move it in your PATH. Use this command:

Clean Code Block
Copied!

chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
        

After this, WP-CLI should be installed! Run this command to check it:

Clean Code Block
Copied!

wp --info
        

If everything is working correctly, you’ll see an output that looks like this:

Clean Code Block
Copied!

OS:  Linux 4.10.0-42-generic #46~16.04.1-Ubuntu SMP Mon Dec 4 15:57:59 UTC 2017 x86_64
Shell:   /usr/bin/zsh
PHP binary:    /usr/bin/php
PHP version:     7.1.12-1+ubuntu16.04.1+deb.sury.org+1
php.ini used:   /etc/php/7.1/cli/php.ini
MySQL binary:
MySQL version:
SQL modes:
WP-CLI root dir:        /home/wp-cli/.wp-cli
WP-CLI packages dir:    /home/wp-cli/.wp-cli/packages/
WP-CLI global config:   /home/wp-cli/.wp-cli/config.yml
WP-CLI project config:
WP-CLI version: 1.5.0
        

How to Use WordPress CLI

Now that you have WP-CLI installed, let’s explore some practical ways you can use it. Get ready to see how much time and effort WP-CLI can save you!

1. Update WordPress

Keeping your WordPress site updated is crucial for security and performance. WP-CLI makes administrative tasks like backups easy.

To update your WordPress installation to the latest version, use this command:

Clean Code Block
Copied!

wp core update
        

To update plugins with WP-CLI, use:

Clean Code Block
Copied!

wp plugin update --all
        

To update a specific plugin, use its slug:

Clean Code Block
Copied!

wp plugin update plugin-slug
        

Updating themes works the same way.

Clean Code Block
Copied!

wp theme update --all
        
Clean Code Block
Copied!

wp theme update theme-slug
        

You can also update to a specific version if needed. For example, to update to WordPress 6.4.3, you would use:

Clean Code Block
Copied!

wp core update --version=6.4.3
        

To preview what updates would happen without actually making changes, use the --dry-run flag:

Clean Code Block
Copied!

wp core update --dry-run
        

This command will show you which updates are available without applying them. It is a good way to test before you do the update for real.

2. Retrieve Site Information

WP-CLI allows you to quickly retrieve various pieces of information about your WordPress website. This can be useful for debugging, checking configurations, or simply gaining a better understanding of your setup.

To see the current version of your WordPress installation, use:

Clean Code Block
Copied!

wp core version
        

This will output the version number, like 6.4.3.

To retrieve the main URL of your WordPress site, use:

Clean Code Block
Copied!

wp option get home
        

This will return the URL configured in your WordPress settings.

To find an administrator email, use:

Clean Code Block
Copied!

wp option get admin_email
        

To find your website’s title, use:

Clean Code Block
Copied!

wp option get blogname
        

You can get a list of users using:

Clean Code Block
Copied!

wp user list
        

You can get the details of a specific user by using their user ID. For example, for user ID 1:

Clean Code Block
Copied!

wp user get 1
        

These commands are helpful for quickly accessing important details about your WordPress site without logging into the dashboard. You can also use this information in scripts to automate certain tasks.

3. Back Up Your Site

Regular backups are essential for any WordPress site. WP-CLI can help you create backups quickly and easily.

The simplest backup you can perform with WP-CLI is a database export. You can use this command:

Clean Code Block
Copied!

wp db export
        

However, full backups get a little more complicated. To make them easier, use Duplicator. This backup plugin comes with WP-CLI commands so you can back up your site without leaving your WordPress command line interface.

Duplicator Pro plugin

Use this command to create a full site backup (database and files):

Clean Code Block
Copied!

wp duplicator build
        

Duplicator Pro’s WP-CLI commands are highly customizable. Here are some options:

  • --template=<ID>: Use a predefined backup template.
  • --dir=<path>: Specify a custom backup location.
  • --delete: Automatically remove the backup after creation.
  • --phpsqldump, --phpzip, --duparchive: Change the archive engine used for backups.

Use the command wp duplicator build --help for a full overview of the available options.

These commands provide an easy way to create, manage, and clean up your backups. You can automate backups by creating simple scripts that run these WP-CLI commands.

For example, you could create a bash script to back up multiple sites simultaneously.

Clean Code Block
Copied!

#!/bin/bash

# Define site path to user associations
declare -A site_configs=(
    ["/var/www/site1/public"]="user1"
    ["/var/www/site2/public"]="user2"
    ["/var/www/site3/public"]="user3"
    # Add more associations as needed
)

target_path="/path/to/backup/destination"

# Create and clean the destination folder
mkdir -p "$target_path" || { echo "Unable to create folder $target_path"; exit 1; }
cd "$target_path" || { echo "Unable to access folder $target_path"; exit 1; }

# Iterate through site path to user associations
for site_path in "${!site_configs[@]}"; do
    user="${site_configs[$site_path]}"

    # Check if the web folder exists
    if [ ! -d "$site_path" ]; then
        echo "Folder $site_path does not exist. Skipping..."
        continue
    fi

    # Execute backup
    cd "$site_path" || { echo "Unable to access $site_path"; continue; }
    echo "Creating backup for $site_path with user $user"
    sudo -u "$user" wp duplicator build --delete --dir="$target_path"
done

# Change ownership and create zip
cd "$target_path" || { echo "Unable to access $target_path"; exit 1; }
chown $USER:$USER *
zip ../backups.zip *
        

WP-CLI can handle basic database backups. For more flexible and automated backups, install Duplicator Pro. Its commands extend WP-CLI, making it a powerful tool for your backup strategy.

4. Manage Your Database

WP-CLI offers several commands to manage your WordPress database directly from the command line. This can be useful for optimizing performance, troubleshooting issues, and performing maintenance tasks.

To optimize your database, use:

Clean Code Block
Copied!

wp db optimize
        

This command helps to improve database performance by removing overhead. It’s good practice to run this regularly.

If you encounter issues with your database, you can try repairing it using:

Clean Code Block
Copied!

wp db repair
        

This command attempts to fix any corrupted tables.

We’ve already seen a basic database export command, but here’s another example. You can export the database with a custom filename:

Clean Code Block
Copied!

wp db export my_database_backup.sql
        

To import a previously exported database, use:

Clean Code Block
Copied!

wp db import my_database_backup.sql
        

Make sure the SQL file exists in the directory where you’re running the command.

This command will replace your existing database with the one in the specified file. Be careful when running this command on a live website. Always back it up first!

These commands make it easier to manage and maintain your WordPress database, without needing to log into phpMyAdmin or another database management tool. They give you direct access to database operations through the command line.

5. Manage Plugins and Themes

WP-CLI provides a convenient way to manage your WordPress plugins and themes from the command line. This is much faster than using the WordPress admin area, especially if you need to manage several plugins and themes.

To install a plugin, use the following command, replacing plugin-slug with the plugin’s actual slug:

Clean Code Block
Copied!

wp plugin install plugin-slug
        

For example, to install the popular All in One SEO plugin:

Clean Code Block
Copied!

wp plugin install all-in-one-seo-pack
        

You can also install a plugin from a ZIP file:

Clean Code Block
Copied!

wp plugin install /path/to/plugin.zip
        

To activate a plugin, use:

Clean Code Block
Copied!

wp plugin activate plugin-slug
        

For example, to activate AIOSEO:

Clean Code Block
Copied!

wp plugin activate all-in-one-seo-pack
        

To deactivate a plugin, use:

Clean Code Block
Copied!

wp plugin deactivate plugin-slug
        

To see a list of all installed plugins, use:

Clean Code Block
Copied!

wp plugin list
        

You can also use a flag to filter by status:

Clean Code Block
Copied!

wp plugin list --status=active
        

This will output a list of your installed plugins, including their status (active or inactive).

To uninstall a plugin, use:

Clean Code Block
Copied!

wp plugin uninstall plugin-slug
        

Use similar WP-CLI to manage themes, replacing “plugin” with “theme”. For example, here’s how you would install the Twenty Twenty-Four theme:

Clean Code Block
Copied!

wp theme install twentytwentyfour
        

These commands allow you to efficiently manage your plugins and themes. You can perform bulk actions, update, activate, or deactivate plugins and themes with only one command, saving you valuable time and effort.

6. Moderate Comments

Managing comments can be a time-consuming task. WP-CLI provides a quick way to moderate comments from the command line. This is especially useful if you have a large number of comments to handle.

To see a list of all comments, use:

Clean Code Block
Copied!

wp comment list
        

This command will output a list of all the comments, along with their ID, author, and status. You can use various flags to filter the comments.

To list only pending comments:

Clean Code Block
Copied!

wp comment list --status=hold
        

You can also list approved comments with:

Clean Code Block
Copied!

wp comment list --status=approve
        

Or spam comments with:

Clean Code Block
Copied!

wp comment list --status=spam
        

To approve a comment, use its ID:

Clean Code Block
Copied!

wp comment approve comment-id
        

Replace comment-id with the actual ID of the comment.

To unapprove a comment, use:

Clean Code Block
Copied!

wp comment unapprove comment-id
        

To mark a comment as spam:

Clean Code Block
Copied!

wp comment spam comment-id
        

To unmark a comment as spam:

Clean Code Block
Copied!

wp comment unspam comment-id
        

To move a comment to the trash:

Clean Code Block
Copied!

wp comment trash comment-id
        

To delete a comment permanently:

Clean Code Block
Copied!

wp comment delete comment-id
        

Be careful, this action is irreversible.

You can perform bulk actions by combining commands with filters. For example, to approve all pending comments:

Clean Code Block
Copied!

wp comment list --status=hold --format=ids | xargs wp comment approve
        

This command retrieves IDs of pending comments and approves all of them in one step.

These commands make it much easier to manage comments, especially if you receive a large volume of them. You can quickly filter, approve, delete, or mark comments as spam using simple commands.

7. Create Posts and Pages

WP-CLI allows you to create and manage content directly from the command line. This can be useful for quickly adding posts or pages.

To create a new post, use:

Clean Code Block
Copied!

wp post create --post_type=post --post_title="My New Post" --post_content="This is the content of my new post." --post_status=publish
        

Replace “My New Post” and “This is the content of my new post.” with the actual title and content you want. This command will create a new published post with the given title and content.

You can also set the post status to draft if you don’t want to publish it right away.

To create a new page, use:

Clean Code Block
Copied!

wp post create --post_type=page --post_title="My New Page" --post_content="This is the content of my new page." --post_status=publish
        

To update content with WP-CLI, you’ll need the post or page ID:

Clean Code Block
Copied!

wp post update post-id --post_title="Updated Title" --post_content="Updated Content."
        

Replace post-id with the ID of the post or page you want to update and “Updated Title” and “Updated Content” with the new content.

To delete a post or page, use the ID:

Clean Code Block
Copied!

wp post delete post-id
        

To list your posts:

Clean Code Block
Copied!

wp post list --post_type=post
        

To list your pages:

Clean Code Block
Copied!

wp post list --post_type=page
        

You can use various flags to filter the list by status, author, and more.

You can also set custom fields using WP-CLI. For example:

Clean Code Block
Copied!

wp post meta set post-id meta-key "Meta Value"
        

Replace post-id, meta-key, and Meta Value with the actual values.

These commands provide a quick way to add, update, or remove content without logging into the WordPress admin area. This can be very helpful when you want to automate content management tasks.

8. Create and Modify Users

You can create, update, and delete users with WP-CLI.

To create a new user, use this command:

Clean Code Block
Copied!

wp user create username user@example.com --user_pass="password" --role=author
        

Replace username, user@example.com, password, and author with the desired values.

This command will create a new user with the specified username, email, password, and role. You can use other roles, such as editor, administrator, subscriber, and more.

To see a list of all users on your site:

Clean Code Block
Copied!

wp user list
        

This will output a list of all the users, along with their IDs, usernames, and emails.

To get the details of a specific user, use:

Clean Code Block
Copied!

wp user get user-id
        

Replace user-id with the ID of the user.

To update an existing user, you can use the wp user update command. For example, to change the user’s email address:

Clean Code Block
Copied!

wp user update user-id --user_email=newemail@example.com
        

You can also update other fields such as the user’s password or role.

To reset a user’s password, use:

Clean Code Block
Copied!

wp user update user-id --user_pass=new_password
        

To delete a user:

Clean Code Block
Copied!

wp user delete user-id
        

You can manage user roles and capabilities using WP-CLI. For example, add the editor role to a user with this command:

Clean Code Block
Copied!

wp user set-role user-id editor
        

Use wp user remove-role to remove a user from a role.

Now you’ll have all the necessary tools to manage users on your WordPress site from the command line. This can be especially helpful when you need to perform bulk user actions.

9. Troubleshoot Errors

WP-CLI can be a useful tool for troubleshooting errors and debugging issues on your WordPress site. Here are a few commands that can help.

WP-CLI can show you if there are any errors in your WordPress setup. This command will check if all core WordPress files are intact. If any file is missing or modified, it will report an error.

Clean Code Block
Copied!

wp core verify-checksums
        

While WP-CLI can’t directly pinpoint errors in plugins or themes, you can use it to disable plugins or switch themes for troubleshooting.

To deactivate all plugins:

Clean Code Block
Copied!

wp plugin deactivate --all
        

To switch to a default theme:

Clean Code Block
Copied!

wp theme activate twentytwentyfour
        

After that, you can reactivate them one by one to isolate the error.

You can use the wp db check command to check for database errors, which may point to problems on your site:

Clean Code Block
Copied!

wp db check
        

You can use the commands mentioned in the database management section to repair your database if needed.

WP-CLI doesn’t directly interact with server logs. To see your web server’s error logs, you typically need to use command-line tools such as tail to view your web server’s error logs directly. The location of these logs vary based on your server configuration.

By using WP-CLI, you can quickly test and diagnose potential problems to get your site back up and running quickly.

10. Create a Child Theme

Creating a child theme is a good idea when you want to customize your WordPress theme without modifying the original theme files. WP-CLI makes this process simple and quick.

To create a child theme, use the wp scaffold child-theme command followed by the parent theme’s slug.

For example, to create a child theme for the Twenty Twenty-Four theme, use:

Clean Code Block
Copied!

wp scaffold child-theme twentytwentyfour
        

This command will create a new directory for your child theme with the basic files needed, including style.css and functions.php. It will also add the necessary header information to style.css to make it a valid child theme.

You can customize the child theme’s directory by adding the --theme_slug flag.

Clean Code Block
Copied!

wp scaffold child-theme twentytwentyfour --theme_slug=my-child-theme
        

You can also specify the child theme’s name with --theme_name.

Clean Code Block
Copied!

wp scaffold child-theme twentytwentyfour --theme_name="My Child Theme"
        

Find the full list of options with:

Clean Code Block
Copied!

wp help scaffold child-theme
        

This command makes it very easy to create child themes, which keep your customizations organized and ensure they won’t be overwritten when updating the parent theme.

11. Regenerate Thumbnails

When you change image sizes in WordPress settings, you often need to regenerate thumbnails for your existing images. This is a common task that WP-CLI can perform efficiently.

To regenerate all thumbnails, use:

Clean Code Block
Copied!

wp media regenerate
        

This command will go through all your existing media files and regenerate the thumbnails based on your current settings. This can take some time depending on how many images you have.

You can also regenerate thumbnails for specific images or certain sizes. View the full list of available flags by using:

Clean Code Block
Copied!

wp help media regenerate
        

12. Search and Replace URLs

The search and replace functionality in WP-CLI is extremely useful when you migrate a WordPress site. It’ll help you update URLs after a domain change.

To search for an old URL and replace it with a new URL, use the following command:

Clean Code Block
Copied!

wp search-replace 'oldurl.com' 'newurl.com'
        

Replace ‘oldurl.com’ with your previous site’s URL and ‘newurl.com’ with your new site’s URL.

This command will search your entire database and replace all instances of the old URL with the new one.

Always back up your database before running this command. This ensures that you can easily revert to the previous state in case something goes wrong.

Be very precise with the old and new URLs. Even a small typo can cause issues.

This command can be powerful but it is also very dangerous if used incorrectly. Test on a staging site before implementing it in production.

If you are using serialized data in your database (which is very common), use the --all-tables flag to ensure that all instances of the URLs are changed. For example:

Clean Code Block
Copied!

wp search-replace 'oldurl.com' 'newurl.com' --all-tables
        

To preview the changes without actually making them, use the --dry-run flag:

Clean Code Block
Copied!

wp search-replace 'oldurl.com' 'newurl.com' --dry-run
        

This command will show you a list of all the changes it would make, giving you a chance to verify them before you proceed.

Frequently Asked Questions (FAQs)

Does WP CLI work on Windows?

WP-CLI works on Windows with the help of tools like WSL (Windows Subsystem for Linux) or through supported PHP environments. Direct installation is possible, but compatibility issues can arise without proper configuration.

How do I register a WP CLI command?

Register a WP-CLI command by using the WP_CLI::add_command function in your PHP code. Define a callback function for the command’s behavior and include it in your plugin or theme’s codebase. Properly document arguments and usage to ensure functionality.

How do you check if CLI is installed or not?

Check if WP-CLI is installed by opening a terminal and running the command wp --info. If installed, the version number or path will be displayed; otherwise, an error message will appear.

How do I use WordPress CLI as a beginner?

Begin using WordPress CLI by installing WP-CLI and navigating to your WordPress directory in the command line. Run commands like wp plugin list to view plugins or wp theme activate to change themes. Start with basic commands and refer to the WP-CLI documentation for guidance.

Final Thoughts

WP-CLI is a powerful tool that can significantly improve your WordPress management workflow. From simple updates to complex database operations, it provides a faster, more efficient way to interact with your WordPress sites.

We encourage you to start experimenting with WP-CLI today. Begin with some basic commands from this WP-CLI tutorial and gradually explore its more advanced features.

The more you use it, the more comfortable you’ll become and the more you will realize its potential!

While you’re here, I think you’ll like these extra WordPress tutorials:

If you’re looking for extra flexibility in WP-CLI, consider using Duplicator Pro. It’s a backup tool that seamlessly integrates with WP-CLI, streamlining your site backups from the command line.

author avatar
Joella Dunn Content Writer
Joella is a writer with years of experience in WordPress. At Duplicator, she specializes in site maintenance — from basic backups to large-scale migrations. Her ultimate goal is to make sure your WordPress website is safe and ready for growth.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. We only recommend products that we believe will add value to our readers.