At a recent WordPress meetup we were discussing WP CLI and the comment came up, “When setting up a site, it would be nice to install a bunch of plugins with a single command.”
Fortunately a WordPress meetup is a great place to bounce ideas off of smart people and find a solution.
Solution 1: Using a Text File
The first solution we crafted was to use a text file with each plugin slug on a separate line, like the following.
stop-emails modify-comment-parent simple-google-analytics-tracking
Then we could run the following, which would install and activate each plugin.
wp plugin install --activate $(<plugin-slugs.txt)
Solution 2: A Long Command
While using a text file for the slugs is a great solution, we realized it would be even nicer if we didn’t need a separate file. Instead we wanted to provide a single line, even if it is a long line, to install multiple plugins.
The following will install and activate the listed plugins.
wp plugin install --activate stop-emails modify-comment-parent simple-google-analytics-tracking
Edited Thanks to Tim’s comment, I’ve removed the extraneous
echo that appeared in an earlier version of this command.
xargs throws root warning
Interestingly, I would expect the following to work as well
echo stop-emails modify-comment-parent simple-google-analytics-tracking | xargs wp plugin install --activate
however, it does not. Instead it displays an error.
Error: YIKES! It looks like you’re running this as root. You probably meant to run this as the user that your WordPress install exists under.
We are able to avoid this error and run the command but we have to use the
--allow-root flag, which seems like poor form.
Do not use this command, instead please use Solution 2: A Long Command
echo stop-emails modify-comment-parent simple-google-analytics-tracking | xargs wp plugin install --activate --allow-root