Ok so this is a very very small shell script but it is always fun when you can write a ‘program’ to do your work for you.
Let me set the scene for you:
At my work we have a development server, in which we host a lot of WordPress websites, some are inactive clients others are currently in development. Those websites need to be updated and maintained regularly for security reasons and so that we can test new code before working on a live website.
Until recently in order to update WordPress on our development sites we had to literally login to each one and update manually. Then wp-cli came into our lives and made things much easier, but I still had to move into each sub-domain via commandline and run the appropriate wp-cli commands. This was still a million times faster than using a browser but became very repetitive once we got over 10 (now 20+) websites on our dev server.
The last time I did a WordPress update I said to myself that there had to be a better way. So I spent all of 10minutes and wrote it. The concept, write a shell script that will move into every sub-domain and run a wp-cli command (based on variables passed).
Here is what I came up with, I created a file called runwpcli.sh with the following code in my root directory:
for f in ./*.maindomain.com;
[ -d $f ] && cd "$f" && echo Entering into $f && wp $1 $2 $3
To explain the code a bit, I assign the variable ‘f’ to each sub-domain, enter it and display the name. Then I run the wp-cli command provided via the “$1 $2 $3” variables (which will also display the result).
Here is an example of how to run the script:
./runwpcli.sh plugin list
This will list all the plugins on each sub-domain. I haven’t used it for anything more advanced than updating core, plugins or themes but in theory I could get pretty automated and do things like create posts or edit users.
If you have multiple WordPress installations on the same server and are sick of updating them one by one, this little snippet of code may save you a ton of time.