search
HomeCMS TutorialWordPress5 Time-Saving Uses for WP-CLI Automation

5 Time-Saving Uses for WP-CLI Automation

5 Time-Saving Uses for WP-CLI Automation

This article is part of a series created in partnership with SiteGround. Thank you for supporting the partners who make SitePoint possible.

Key Takeaways

  • WP-CLI automation is a tool that lets developers perform actions on a WordPress installation from the command line, making it a valuable asset when managing multiple sites or creating similar new ones for testing.
  • WP-CLI automation can be used for a variety of tasks including installing or updating WordPress files, plugins, or themes, activating and deactivating plugins, performing database actions, and even automating the installation and setup of WordPress and its plugins.
  • WP-CLI automation can save significant time for maintainers of multiple WordPress sites, enabling them to update or backup multiple sites at once, create complex boilerplate installations with single commands, run backups, updates, migrations, and more.

What is WP-CLI?

The WP-CLI is a tool that allows you to perform actions on a WordPress installation straight from the command line. WP-CLI automation is the automating of repetitive manual tasks by using WP-CLI driven scripts. This may seem unnecessary, awkward, or too difficult a task to bother with when you’re deploying or managing a single WordPress installation, but when you’re managing many, or constantly creating similar new sites for testing, it becomes an extremely valuable tool to have in your developer toolkit.

About WP-CLI

With WP-CLI, you can essentially perform any action that you could have via the admin panel, but from the command line instead. You can install or update core WordPress files, plugins, or themes. You can activate and deactivate plugins or regenerate image thumbnails. You can also perform database actions, such as export and import of the database, or find and replace the database for information, such as a changed URL during a migration.

Some plugins have WP-CLI support as well — including many of the more popular ones. This means you can set up automated scripting to install and set up WordPress, install those plugins, and then to set up the plugins as well, using their own customized WP-CLI commands!

WP-CLI Automation

WP-CLI automation goes beyond simple command line usage when setting up or managing multiple WordPress installations. The ability to update or back up multiple sites at once, or create complicated boilerplate installations repeatedly with single commands are incredibly useful and can save a significant amount of time for maintainers of those sites.

If you don’t already have the WP-CLI installed, take a look at the installation documentation and get the WP-CLI up and running.

Five Use Cases for WP-CLI Automation

Installing WordPress

Once WordPress is installed, this example script could download, configure, and install WordPress core, remove starting plugins, add and activate a specified theme (saved in example-theme.zip), and install and activate a list of plugins you’d prefer to use with new installations.

Example:

#!/usr/bin/env bash

#plugins to install and activate (slugs)
WPPLUGINS=( test-plugin1 test-plugin2 test-plugin3 )

echo "Starting WordPress Installation Script"

# Site Name Input
echo "Site Name: "
read -e sitename

# Site URL Input
echo "Site URL: "
read -e siteurl

# Download WP and configure it
wp core download
wp core config --dbname=$dbname --dbuser=root --dbpass=root
wp db create
wp core install --url=$siteurl --title="$sitename" --admin_user="admin" --admin_password="examplePassword123" --admin_email="test@example.com"

# Remove default plugins, install plugins, install Base Theme
wp plugin delete --all
wp theme install example-theme.zip --activate
wp plugin install ${WPPLUGINS[@]} --activate

echo "WordPress installation complete!"

However, you could automate this process even further, by asking the user for relative path information, so that you don’t have to be in the installation directory to run it, by asking for database name and password, and more. You can also do (as you’ll see later in this article) a setup for a hosting environment that handles multiple WordPress installations on one server, and set up and install more than one site at once. Customize the script in the way that you need, so that it can be maximally effective for your own projects, and so that you won’t have to constantly rewrite it — make it efficient!

Backing Up WordPress

Backing up your WordPress installation is a must, but there are a variety of ways to do it. You can backup easily with a number of WordPress backup plugins, but you can also do so straight from the command line.

First, you’ll want to run (whether at the command line, or via a script) wp db export example.com_20170501T1420 from the website’s directory, with the last parameter being the filename you prefer. Of course, if automating that process entirely, it would be handy to add in a timestamp to file names.

Once that is done, your website’s root directory will contain a .sql file which is a backup of the site’s database at the time it was exported. You can then run a simple tar -vczf example.com_20170501T1420.gz . (using the same file name for this backup archive), which will compress both the website’s files, and the .sql file along with it. Now, via the command line, a script, or an SFTP client, you can copy that archive file to another computer, drive, or cloud storage, a backup of both files and database, within moments!

WordPress Core Updates

To update the WordPress core files for the site in your current directory, run the wp core update command. This command really shines when you set up a script to loop through a list of the installations on the current server, updating each in turn, all by entering a single command.

Example:

#!/usr/bin/env bash

# Assumes site directories are under /var/www/siteurl

WPSITES=( example.com example2.com example3.com )

WPPATH=/var/www/

echo "Starting WordPress Core Updates"

for i in "${WPSITES[@]}"
do
  : 
  wp core update --path:$WPPATH$i
  echo "Updates for $i Completed!"
done

echo "WordPress Core Updates Complete!"

WordPress Plugin and Theme Updates

Similarly to the core updates, loop through a list of your sites, running wp plugin update -all to update all plugins installed on each site, or wp theme update --all to do the same for themes.

Example:

#!/usr/bin/env bash

# Assumes site directories are under /var/www/siteurl

WPSITES=( example.com example2.com example3.com )

WPPATH=/var/www/

echo "Starting WordPress Plugin and Theme Updates"

for i in "${WPSITES[@]}"
do
  : 
  wp plugin update --all --path:$WPPATH$i
  wp theme update --all --path:$WPPATH$i
  echo "Updates for $i Completed!"
done

echo "WordPress Plugin and Theme Update Complete!"

If you wish to do core WordPress updates as well as plugins and themes, you could also combine those into one update script.

WordPress Migrations

As a part of your migration flow, when migrating a site between servers, to another domain, or between development and production or staging environments, you can handle all of your database concerns with WP-CLI as well.

Export the database from your old hosting server (run from the website root directory) with:

#!/usr/bin/env bash

#plugins to install and activate (slugs)
WPPLUGINS=( test-plugin1 test-plugin2 test-plugin3 )

echo "Starting WordPress Installation Script"

# Site Name Input
echo "Site Name: "
read -e sitename

# Site URL Input
echo "Site URL: "
read -e siteurl

# Download WP and configure it
wp core download
wp core config --dbname=$dbname --dbuser=root --dbpass=root
wp db create
wp core install --url=$siteurl --title="$sitename" --admin_user="admin" --admin_password="examplePassword123" --admin_email="test@example.com"

# Remove default plugins, install plugins, install Base Theme
wp plugin delete --all
wp theme install example-theme.zip --activate
wp plugin install ${WPPLUGINS[@]} --activate

echo "WordPress installation complete!"

Import it to your new hosting server (run from the website root directory) with:

#!/usr/bin/env bash

# Assumes site directories are under /var/www/siteurl

WPSITES=( example.com example2.com example3.com )

WPPATH=/var/www/

echo "Starting WordPress Core Updates"

for i in "${WPSITES[@]}"
do
  : 
  wp core update --path:$WPPATH$i
  echo "Updates for $i Completed!"
done

echo "WordPress Core Updates Complete!"

Then replace old information (like a URL) with new information (run from the website root directory) with:

#!/usr/bin/env bash

# Assumes site directories are under /var/www/siteurl

WPSITES=( example.com example2.com example3.com )

WPPATH=/var/www/

echo "Starting WordPress Plugin and Theme Updates"

for i in "${WPSITES[@]}"
do
  : 
  wp plugin update --all --path:$WPPATH$i
  wp theme update --all --path:$WPPATH$i
  echo "Updates for $i Completed!"
done

echo "WordPress Plugin and Theme Update Complete!"

The search-replace command replaces any instance of oldurl.com with newurl.com).

This process could also be automated, by extending the same scripts you might use for a backup. You could easily have an export script, then an import script that has added inputs for search and replace fields, and perhaps even extend it with options for new database credentials, if they’ve changed.

Conclusions

The number of tasks that can be automated with WP-CLI is simply amazing. You can customize an installation script to download WordPress core, create your configuration and your database, install WordPress, strip it of any bloat, add default plugins and themes and activate them, and more. You can also use it to run backups, updates, migrations, and more.

Choosing a good host is important when you want to use WP-CLI. Many hosts don’t support the usage of WP-CLI, so finding one that does is of paramount importance if you intend to utilize WP-CLI automation. SiteGround is one of the hosts that actively supports and invests in the maintenance of the WP-CLI project. It’s a great choice for hosting your WordPress website, especially when you need to use WP-CLI — it has WP-CLI enabled on all WordPress hosting plans. SiteGround also has a useful tutorial on using WP-CLI on their servers.

Check them out, and get to work automating your installation and maintenance of WordPress with WP-CLI!

Frequently Asked Questions (FAQs) about WP-CLI Automation

What is WP-CLI Automation and why is it important?

WP-CLI Automation is a powerful tool that allows you to manage your WordPress website from the command line. It’s important because it can save you a significant amount of time by automating repetitive tasks. For instance, you can use WP-CLI to update plugins, configure multisite installations, and much more without ever having to navigate through the WordPress backend.

How can I install WP-CLI on my WordPress website?

Installing WP-CLI is a straightforward process. You need to download the WP-CLI package using curl or wget, make it executable, and move it to a location in your PATH. Once installed, you can verify the installation by typing ‘wp –info’ in your command line.

Can I use WP-CLI to automate plugin updates?

Yes, you can. WP-CLI allows you to update all your plugins with a single command. This can be particularly useful if you manage multiple WordPress websites, as it can save you a lot of time.

What are some other uses of WP-CLI?

Besides plugin updates, WP-CLI can be used for a variety of tasks. These include database management, theme installation and updates, user management, and much more. Essentially, anything you can do from the WordPress backend, you can do from the command line with WP-CLI.

Is WP-CLI suitable for beginners?

While WP-CLI does require some familiarity with the command line, it’s not overly complex. There are plenty of resources available to help you get started, and once you’ve learned the basics, you’ll find that it can greatly streamline your WordPress management tasks.

Can WP-CLI be used with any WordPress website?

WP-CLI can be used with any WordPress website that’s hosted on a server where you have SSH access. It’s not typically available on shared hosting plans, but most VPS and dedicated hosting plans will allow you to use it.

How can I learn more about the commands available in WP-CLI?

The official WP-CLI website has a comprehensive list of commands, along with detailed explanations of what they do and how to use them. You can also type ‘wp help’ in your command line to get a list of commands.

Is it possible to automate the creation of new posts with WP-CLI?

Yes, it is. WP-CLI includes a command that allows you to create new posts with a specified title, content, and status. This can be particularly useful if you need to create a large number of posts at once.

Can I use WP-CLI to manage users on my WordPress website?

Absolutely. WP-CLI includes several commands for user management. You can create, delete, and edit users, change user roles, and much more.

What are the benefits of using WP-CLI over the WordPress backend?

The main benefit of using WP-CLI is that it can save you time. Tasks that would take several clicks in the WordPress backend can be done with a single command in WP-CLI. It’s also a powerful tool for bulk actions, like updating all plugins or creating multiple posts.

The above is the detailed content of 5 Time-Saving Uses for WP-CLI Automation. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to add a comment box to WordPressHow to add a comment box to WordPressApr 20, 2025 pm 12:15 PM

Enable comments on your WordPress website to provide visitors with a platform to participate in discussions and share feedback. To do this, follow these steps: Enable Comments: In the dashboard, navigate to Settings > Discussions, and select the Allow Comments check box. Create a comment form: In the editor, click Add Block and search for the Comments block to add it to the content. Custom Comment Form: Customize comment blocks by setting titles, labels, placeholders, and button text. Save changes: Click Update to save the comment box and add it to the page or article.

How to copy sub-sites from wordpressHow to copy sub-sites from wordpressApr 20, 2025 pm 12:12 PM

How to copy WordPress subsites? Steps: Create a sub-site in the main site. Cloning the sub-site in the main site. Import the clone into the target location. Update the domain name (optional). Separate plugins and themes.

How to write a header of a wordpressHow to write a header of a wordpressApr 20, 2025 pm 12:09 PM

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

How to display wordpress commentsHow to display wordpress commentsApr 20, 2025 pm 12:06 PM

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

How to upload source code for wordpressHow to upload source code for wordpressApr 20, 2025 pm 12:03 PM

You can install the FTP plug-in through WordPress, configure the FTP connection, and then upload the source code using the file manager. The steps include: installing the FTP plug-in, configuring the connection, browsing the upload location, uploading files, and checking that the upload is successful.

How to copy wordpress codeHow to copy wordpress codeApr 20, 2025 pm 12:00 PM

How to copy WordPress code? Copy from the admin interface: Log in to the WordPress website, navigate to the destination, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code. Copy from a file: Connect to the server using SSH or FTP, navigate to the theme or plug-in file, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code.

What to do if there is an error in wordpressWhat to do if there is an error in wordpressApr 20, 2025 am 11:57 AM

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

How to close comments with wordpressHow to close comments with wordpressApr 20, 2025 am 11:54 AM

How to turn off a comment in WordPress? Specific article or page: Uncheck Allow comments under Discussion in the editor. Whole website: Uncheck "Allow comments" in "Settings" -> "Discussion". Using plug-ins: Install plug-ins such as Disable Comments to disable comments. Edit the topic file: Remove the comment form by editing the comments.php file. Custom code: Use the add_filter() function to disable comments.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment