#AI Plugins

5 Time-Saving Uses for WP-CLI Automation — SitePoint – SitePoint

Embark on a journey into the realm of AI plugins through our dedicated blog series. As we delve into the intricacies of these intelligent add-ons, discover how they redefine the landscape of software development and digital experiences. From streamlining workflows to introducing advanced functionalities, these articles aim to illuminate the transformative impact of AI plugins across various industries. Whether you’re a developer, business owner, or simply curious about the latest innovations, join us in exploring the powerful synergy between artificial intelligence and the evolution of software enhancements.
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.
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.
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 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.
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:
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 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!
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:
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:
If you wish to do core WordPress updates as well as plugins and themes, you could also combine those into one update script.
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:
Import it to your new hosting server (run from the website root directory) with:
Then replace old information (like a URL) with new information (run from the website root directory) with:
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.
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!
Jeff works for a startup as a technical writer, does contract writing and web development, and loves tinkering with new projects and ideas. In addition to being glued to a computer for a good part of his day, Jeff is also a husband, father, tech nerd, book nerd, and gamer.
© 2000 – 2024 SitePoint Pty. Ltd.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

source

5 Time-Saving Uses for WP-CLI Automation — SitePoint – SitePoint

Meta Rolls Out Generative AI Tool To

5 Time-Saving Uses for WP-CLI Automation — SitePoint – SitePoint

GPT-3 in Your Pocket? Why Not! –

Leave a comment

Your email address will not be published. Required fields are marked *