Announcing Duplicator WP-CLI Commands (And How They Speed Up Backups)
Announcing Duplicator WP-CLI Commands (And How They Speed Up Backups)
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.
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.
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.
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.
To install WP-CLI, make sure your server meets the requirements. You’ll need:
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:
ssh username@hostname
Then, download the Phar build to install and manage WordPress CLI:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Check to see if the file was downloaded:
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:
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:
wp --info
If everything is working correctly, you’ll see an output that looks like this:
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
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!
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:
wp core update
To update plugins with WP-CLI, use:
wp plugin update --all
To update a specific plugin, use its slug:
wp plugin update plugin-slug
Updating themes works the same way.
wp theme update --all
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:
wp core update --version=6.4.3
To preview what updates would happen without actually making changes, use the --dry-run
flag:
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.
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:
wp core version
This will output the version number, like 6.4.3.
To retrieve the main URL of your WordPress site, use:
wp option get home
This will return the URL configured in your WordPress settings.
To find an administrator email, use:
wp option get admin_email
To find your website’s title, use:
wp option get blogname
You can get a list of users using:
wp user list
You can get the details of a specific user by using their user ID. For example, for user ID 1:
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.
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:
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.
Use this command to create a full site backup (database and files):
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.
#!/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.
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:
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:
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:
wp db export my_database_backup.sql
To import a previously exported database, use:
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.
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:
wp plugin install plugin-slug
For example, to install the popular All in One SEO plugin:
wp plugin install all-in-one-seo-pack
You can also install a plugin from a ZIP file:
wp plugin install /path/to/plugin.zip
To activate a plugin, use:
wp plugin activate plugin-slug
For example, to activate AIOSEO:
wp plugin activate all-in-one-seo-pack
To deactivate a plugin, use:
wp plugin deactivate plugin-slug
To see a list of all installed plugins, use:
wp plugin list
You can also use a flag to filter by status:
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:
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:
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.
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:
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:
wp comment list --status=hold
You can also list approved comments with:
wp comment list --status=approve
Or spam comments with:
wp comment list --status=spam
To approve a comment, use its ID:
wp comment approve comment-id
Replace comment-id with the actual ID of the comment.
To unapprove a comment, use:
wp comment unapprove comment-id
To mark a comment as spam:
wp comment spam comment-id
To unmark a comment as spam:
wp comment unspam comment-id
To move a comment to the trash:
wp comment trash comment-id
To delete a comment permanently:
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:
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.
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:
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:
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:
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:
wp post delete post-id
To list your posts:
wp post list --post_type=post
To list your pages:
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:
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.
You can create, update, and delete users with WP-CLI.
To create a new user, use this command:
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:
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:
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:
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:
wp user update user-id --user_pass=new_password
To delete a user:
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:
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.
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.
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:
wp plugin deactivate --all
To switch to a default theme:
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:
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.
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:
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.
wp scaffold child-theme twentytwentyfour --theme_slug=my-child-theme
You can also specify the child theme’s name with --theme_name
.
wp scaffold child-theme twentytwentyfour --theme_name="My Child Theme"
Find the full list of options with:
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.
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:
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:
wp help media regenerate
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:
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:
wp search-replace 'oldurl.com' 'newurl.com' --all-tables
To preview the changes without actually making them, use the --dry-run
flag:
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.
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.
Register a WP-CLI command by using the WP_CLI::add_comman
d 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.
Check if WP-CLI is installed by opening a terminal and running the command
. If installed, the version number or path will be displayed; otherwise, an error message will appear.wp --info
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.
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.
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.