• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP Realm

WordPress news, community news, reviews and more from all over the world

  • News
  • Reviews
  • Tutorials
  • eCommerce
  • About
  • Contact

WordPress wp-cli Kung-Fu Made Simple

26 September 2013 by Nuno Morgadinho 8 Comments

With every new site or project, there comes a new WordPress install, and a few steps are needed before you can start to work. They usually involve one or more of the following:

  • download / update WordPress
  • create a new database and database user
  • set up Apache vhost
  • set up vhost url on system hosts file
  • install your favorite plugins
  • install a theme

This is a fairly simple process, which takes up a few minutes every time you start on a new project. However, since I like to automate things to decrease the chance of screwing up, and saving time, I’ve started to play around to see if I could somehow streamline this process.

While I’ve seen people come up with scripts to automate command-line stuff with WordPress, I haven’t seen anything near as complete, or as supported by the community as wp-cli. It was only natural to try using it for the task at hand.

I like to start work on a new website or theme by using Underscores, and thus wanted the script to install and activate the theme alongside the latest version of WordPress.  I spend most of the time just trying to get a downloadable file from their website automatically but eventually came to a solution using curl to get it.

After showing it around to friends I got the feedback that it would be handy to have it install a list of favorite plugins. Here is how the script ended up:

#!/bin/bash
# test the number of arguments
if [ ! $# == 9 ]; then
echo "Usage: $0 project_name path dbname dbuser dbpass url admin_email admin_password theme_slug favorite_plugins_file"
exit
fi
# directory that will hold the WordPress files and be the document root for your site
PROJECT_NAME="$1"
WP_DIR="$2"
DBNAME="$3"
DBUSER="$4"
DBPASS="$5"
WP_URL="$6"
ADMIN_EMAIL="$7"
ADMIN_PASSWORD="$8"
THEME_SLUG="$9"
# EDIT DEFAULT VALUES HERE
: ${PROJECT_NAME:="wptest"}
: ${WP_DIR:="/Users/nuno/Projects/".$PROJECT_NAME}
: ${DBNAME:="$PROJECT_NAME"}
: ${DBUSER:="user"}
: ${DBPASS:="pass"}
: ${WP_URL:="$PROJECT_NAME.dev"} # the url you will use when accessing this on localhost, e.g. http://wptest.dev/
: ${ADMIN_EMAIL:="email"}
: ${ADMIN_PASSWORD:="pass"}
: ${THEME_SLUG:="$PROJECT_NAME"}
: ${FILE_FAV_PLUGINS:="favorite-plugins.txt"}
# Detect paths
CAT=$(which cat)
AWK=$(which awk)
GREP=$(which grep)
PLUGINS=$($CAT $FILE_FAV_PLUGINS | $GREP -v ^# )
mkdir $WP_DIR; cd $WP_DIR
# to-do: check if wp-cli is available
wp core download
wp core config --dbname=$DBNAME --dbuser=$DBUSER --dbpass=$DBPASS
mysqladmin -u $DBUSER -p$DBPASS create $DBNAME
wp core install --url=$WP_URL --title="Your Newly WordPress" --admin_email=$ADMIN_EMAIL --admin_password=$ADMIN_PASSWORD
(sudo sh -c "echo '\n<VirtualHost *:80>
DocumentRoot "$WP_DIR"
ServerName $WP_URL
ServerAlias $WP_URL
<Directory "$WP_DIR">
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
' >> /etc/apache2/extra/httpd-vhosts.conf")
(sudo sh -c "apachectl restart")
(sudo sh -c "echo -e '\n127.0.0.1 $WP_URL' >> /etc/hosts" )
# install the _s theme
curl -d "underscoresme_generate=1&underscoresme_name=$THEME_SLUG&underscoresme_slug=&underscoresme_author=&underscoresme_author_uri=&underscoresme_description=&underscoresme_generate_submit=Generate" http://underscores.me > me.zip
wp theme install me.zip
wp theme activate $THEME_SLUG
for p in $PLUGINS
do
echo "Installing $p plugin..."
wp plugin install $p
done
for p in $PLUGINS
do
echo "Activating $p plugin..."
wp plugin activate $p
done
view raw wordpress-install.sh hosted with ❤ by GitHub

We start with the wp core download and wp core config commands; they will download the latest WordPress version and create a config file based on the supplied parameters at the command line interface. After that, we issue the wp core install command, which starts the installation (much like if you were visiting the page on a regular browser). Next, we add the vhost to Apache and restart it. After that, the vhost gets added to the system’s host file, and finally, we download and install our starter theme.

If you don’t immediately grasp how wp-cli it works, you might well be tempted to skip it. However, if you stick with it for a while and learn how to leverage it, you will find that it can save you a good few hours of work. This is just an example of how wp-cli can be useful.

ShareTweet

Filed Under: Practical Tagged With: command-line, wp-cli

Reader Interactions

Comments

  1. scribu says

    26 September 2013 at 14:33

    Note that WP-CLI already has a command for downloading the Underscores theme: wp scaffold _s

    Reply
    • Nuno Morgadinho says

      26 September 2013 at 16:55

      Thanks Scribu, I didn’t notice this going in and it makes it even easier.

      Reply
    • Remkus de Vries says

      28 September 2013 at 22:04

      Oh fancy.. I did not know that one either.

      Reply
  2. dan a says

    16 February 2014 at 02:42

    Not sure if you are familiar with Serverpress’s Desktopserver app. It allows you to build local wordpress sites. You can store a zip version of a “blueprint” site that loads exactly how you want it to and directly deploy it to a live site as well. Just something to consider.

    Reply
  3. KostasX says

    14 January 2015 at 01:54

    You can also replace the `mysqladmin -u $DBUSER -p$DBPASS create $DBNAME` command with this: wp db create

    Reply
  4. KostasX says

    14 January 2015 at 02:06

    You can further shorten this really cool script by activating the plugins and themes, immediately after installation using the –activate flag

    Example: wp theme install –activate

    Reply

Trackbacks

  1. L’Hebdo WordPress n°201 : WordCamp Europe – Ressources – Sécurité | WordPress Francophone says:
    1 October 2013 at 07:01

    […] projet original d’automatisation de l’installation de WordPress (en). Une méthode pour gagner du temps et éviter les […]

    Reply
  2. WordPress Francophone : L’Hebdo WordPress n°201 : WordCamp Europe – Ressources – Sécurité - WordPress Actualités : WordPress Actualités says:
    1 October 2013 at 12:37

    […] projet original d’automatisation de l’installation de WordPress (en). Une méthode pour gagner du temps et éviter les […]

    Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

SUBSCRIBE

Our posts to your inbox. It's a wonderful thing.

Recent Comments

  • Remkus de Vries on Building a Multilingual Website? These are the Questions to Ask.
  • Tuba on Building a Multilingual Website? These are the Questions to Ask.
  • WPML, qTranslate eller Polylang: hvilken du skal velge og hvorfor? (2020) - My Blog on Building a Multilingual Website? These are the Questions to Ask.
  • Marian Malahin on Building a Multilingual Website? These are the Questions to Ask.
  • WPML,qTranslate X或Polylang – 三种最流行的多语言WordPress插件比较(2019) – wordpress on Building a Multilingual Website? These are the Questions to Ask.
Copyright © 2012 – 2023 WP Realm
Have you seen how fast Servebolt serves this site?