They say if you want to learn something, teach.
Or scratch your own itch.
Or just do it.
In any case, I had an itch that I have been wanting to find a fix for for a while.
If you are a developer like me, you began figuring this “WordPress thing” out through tutorials, trial and (mostly) error, and trying a lot of different things out.
Also, if you’re like me, you got into this for several reasons, one of which is the joy of helping people out and getting them a website setup and launched. I love the satisfaction of getting that project launched and the joy these folks have when they realize they can manage their own publishing.
And of course, if you’re a developer like me, you have a shared hosting account with at least a dozen different websites that are small personal projects or folks on the “friends and family plan” (read “free hosting” 🙂 ). This means I have a dozen or so sites I want to make sure I keep updated, but don’t want to spend a lot of time updating.
So, I decided to learn enough bash to write a script that would execute WP-CLI commands on my local environment as well as my shared hosting.
Here’s how I did it…
.sh file that I am currently using to update all my sites from the command line.
This code does a few things. Let’s read through it together…
First, I double check that I actually want to run the command on line 4. The response to the
read prompt is stored in the
On line 6, I check that I want to run the command with an
if statement. Bash ends statements like this by reversing the lettering, which you can see on line 44:
Assuming we do want to run the script, we want to loop through several directories. We store an array of these directories in the
DIRECTORIES variable on line 9.
Then, we use a
for in; do loop to cycle through the directories one by one. Notice that the loop is ended on line 42 with
In between, we execute several commands. First we change directories into the WordPress install’s root folder with
cd, line 15.
Once we’re in the root install directory, I notify what directory we’re in (lines 17 to 23), and begin executing WP-CLI commands, which have their own outputs (line 25-31).
Once the WP-CLI commands are done, I notify that they directory we’re in is complete (lines 33-39), and change directories back out to the
www (for VVV) or
public_html (for shared hosting) directories on line 41.
And this seems to work for me.
While I am very interested in learning more Bash, I knew I just wanted to learn just enough to get this job done. I knew I needed to know how to do two things:
I googled my way to this online resource: LearnShell.org. I was able to get through the tutorials fairly quickly to learn what I needed to with variables and loops.
Then I found a WP-CLI presentation from Shawn Hooper on WordPress.tv here, and watching it reveled exactly what I was looking for. Towards the end of the presentation, Shawn uses a script he wrote to install and configure a new WordPress site
I reached out to Shawn, and he sent over a link to his public gist. You can see Shawn’s public bash script here.
Armed with all this information, I was able to write a script that saves me about 30 minutes a month. More than that, I actually want to use this script:-).
The most glaring area for improvement on this for me is the
cd logic. Currently, I’m changing this by hand, based on my environment (local or hosted). However, I could have the bash script check for all directories in an install (including the current root directory), and then use that to loop.
Expanding on that further, if I have a directory that is not a WordPress install, I could automatically check to see if the directory contains a WordPress install, core files, or something similar. If it doesn’t, head back out of the directory and on to the next.
This script could also be infinitely recursive, assuming I could have WordPress installs several directories deep with different file structures… But all that is for another day.
The script as is works great for me, and I’m feeling good about it… for now ;-).