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

WP-CLI Automation

< img src="" alt =" WP-CLI Automation" width =" 800" height=" 400" > This post belongs to a series produced in partnership with< a href="" rel=" nofollow" > SiteGround. Thank you for supporting the partners who make SitePoint possible.What is WP-CLI?

The WP-CLI is a tool that allows you to perform actions on a WordPress installation directly from the command line. WP-CLI automation is the automating of recurring manual tasks by utilizing WP-CLI owned scripts. This might seem unneeded, awkward, or too difficult a task to bother with when you’re deploying or handling a single WordPress installation, however when you’re managing lots of, or constantly developing comparable new websites for screening, it ends up being an incredibly important tool to have in your designer toolkit.About WP-CLI With WP-CLI

, you can essentially perform any action that you might have via the admin panel, but from the command line rather. You can set up or upgrade core WordPress files, plugins, or styles. You can activate and shut off plugins or restore image thumbnails. You can also carry out database actions, such as export and import of the database, or find and replace the database for info, such as a changed URL throughout a migration.Some plugins have WP-CLI support as well– including numerous of the more popular ones. This implies you can establish automatic scripting to install and set up WordPress, install those plugins, and after that to establish the plugins also, using their own tailored WP-CLI commands!WP- CLI Automation WP-CLI automation surpasses basic command line use when establishing or managing numerous WordPress installations. The ability to update or back up numerous sites at as soon as, or develop complex boilerplate installations repeatedly with single commands are extremely helpful and can save a considerable amount of time for maintainers of those sites.Five Usage Cases for WP-CLI Automation Setting up WordPress As soon as WordPress is installed, this example script could download, configure, and install


core, eliminate starting plugins, add and activate a specific style( SitePoint’s own Base Theme), and install and activate a list of plugins you ‘d prefer to use with new setups. #!/ usr/bin/env bash #plugins to install and activate( slugs). WPPLUGINS =-LRB- test-plugin1 test-plugin2 test-plugin3). echo “Beginning WordPress Setup Script “. # Website Name Input.

echo" Site Name: ".

read- e sitename. # Website URL Input.
echo" Website URL:". read- e siteurl.

# Download WP and configure it. wp core download.

wp core config-- dbname=$ dbname-- dbuser
= root-- dbpass= root. wp db develop.
wp core set up-- url=

$ siteurl-- title= "$ sitename "--
admin_user=" admin
"-- admin_password=" examplePassword123
"-- admin_email="" # Remove default plugins, install plugins, install Base Theme. wp plugin erase-- all. wp style set up sitepoint-base. zip--
trigger. wp plugin set up $WPPLUGINS [@]
-- trigger. echo "WordPress installation total! ". However, you could automate this procedure even further, by asking the
user for relative path details, so that you

don’t have to be in the setup directory to run it, by requesting for database name and password, and more. You can also do( as you’ll see later on in this short article) a setup for a hosting environment that deals with several WordPress installations on one server, and established and install more than one site at the same time. Customize the script in the manner in which you need, so that it can be maximally efficient for your very own projects, therefore that you will not need to continuously reword it– make it efficient!Backing Up WordPress Supporting your WordPress installation is a must, however there are a range of ways to do it. You can backup quickly with a variety of< a href ="" > WordPress backup plugins, but you can likewise do so directly from the command line. Initially, you’ll wish to run (whether at the command line, or by means of a script) wp db export _ 20170501T1420 from the website’s directory site, with the last parameter being the filename you choose. Obviously, if automating that process completely, it would be convenient to include a timestamp to submit names.Once that is done, your website’s root directory site will consist of a. sql file which is a backup of the site’s database at the time it was exported. You can then run an easy tar- vczf _ 20170501T1420.gz.( utilizing the exact same file name for this backup archive), which will compress both the website’s files, and the. sql file along with it. Now, by means of the command line, a script, or an SFTP client, you can copy that archive file to another computer system, drive, or cloud storage, a backup of both files and database, within moments!WordPress Core Updates To update the WordPress core declare the website in your current directory site, run the wp core upgrade command. This command really shines when you set up a script to loop through a list of the setups on the current server, upgrading each in turn, all by entering a single command. #!/ usr/bin/env bash. # Assumes site directory sites are

under/ var/www/siteurl.

WPSITES =-LRB- ). WPPATH =/ var/www/. echo” Beginning WordPress Core Updates”. for i in”$ “. do.:. wp core update– course:$ WPPATH$ i. echo” Updates for $i Completed!”. done. echo” WordPress Core Updates Total!”. WordPress Plugin and Theme Updates To the core updates, loop through a list of your websites, running wp plugin upgrade -all to upgrade all plugins set up on each website, or wp theme update–
all to do the same for themes.

#!/ usr/bin/env bash. # Assumes site directories are under/ var/www/siteurl
WPSITES =-LRB- WPPATH =/ var/www/.
echo “Beginning WordPress Plugin and Style Updates”. for i in”$

WPSITES [@] “. do.:. wp plugin upgrade– all– path:$ WPPATH$ i. wp theme upgrade– all– course: $WPPATH$ i. echo “Updates for $i Completed! “. done. echo” WordPress Plugin and Theme Update Total!”. If you wish to do core WordPress updates

along with plugins and styles, you could also combine those into one upgrade script.WordPress Migrations As a part of your migration flow, when moving

a website in between servers, to another domain, or in between development and production or staging environments, you can handle all of your database issues with WP-CLI as well.Export the database from your old hosting server(
run from the website root directory site) with: wp
db export _ 20170501T1420 Import
it to your brand-new hosting server( run from the website root directory site) with: wp

db import _ 20170501T1420 Replace old information (like a URL) with brand-new info( run from the site root directory) with

: wp search-replace The search-replace command replaces any instance of with This procedure could also be automated, by extending the exact same scripts you might utilize for a backup. You might easily have an export script, then an import script that has actually included inputs for search and change fields, and perhaps even extend it with choices fornew database qualifications, if they've changed.Conclusions The variety of tasks that can be automated with WP-CLI is simply incredible. You can customize a setup script to download WordPress core, develop your configuration and your database

, set up WordPress, strip it of any bloat, add default plugins and themes and trigger them, and more. You can likewise utilize it to

run backups, updates, migrations, and more. Choosing

a great host is essential when you want to use WP-CLI. Many hosts don't support the usage of WP-CLI, so discovering one

that does is of vital importance if you mean to utilize WP-CLI automation. SiteGround is among the hosts that actively supports and buys the upkeep of

the WP-CLI project

. It's an excellent choice for hosting your WordPress website, specifically when you have to use WP-CLI-- it has WP-CLI enabled on all WordPress hosting strategies. SiteGround also has a beneficial< a href="" rel =" nofollow" > tutorial on utilizing WP-CLI on

their servers.Check them out, and get to work automating your setup and upkeep of WordPress with WP-CLI!

Written by 

Related posts