Automate the WordPress install

The famous five minute installation of WordPress can easily be automated. Why..? I hear you ask. Well..because we can! Together with the tools provided by WP-CLI we can make a bash script that is short and simple. I have added it to my .dotfiles so it’s available when needed.

The code below does the following:

  • Perfoms a check to see if dependencies are available
  • Asks a number of questions concerning the installation
  • Creates a new database
  • Adds a new database-user and assigns privileges to the newly created database
  • Downloads the latest version of WordPress
  • Configures and installs WordPress
  • Downloads the Underscores starter theme
  • Creates a package.json file
  • Installs node packages
  • Downloads a gulpfile from Github

If you plan on using this script be sure to change the value of the ‘targetdir’ variable to suit your needs. Inside the ‘target directory’ a new directory is created that will contain the WordPress files and folders.


function installwp() {

    #========================================
    # CHECK FOR DEPENDENCIES
    #========================================

    # http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script
    command -v wp cli version >/dev/null 2>&1 || { echo >&2 "WP-CLI is required for this script. Aborting."; exit 1; }
    command -v underscores >/dev/null 2>&1 || { echo >&2 "Underscores is required for this script - https://www.npmjs.com/package/underscores. Aborting."; exit 1; }
    command -v node --version >/dev/null 2>&1 || { echo >&2 "Node is required for this script. Aborting."; exit 1; }


    #========================================
    # PATHS
    #========================================

    target_dir=~/PATH/TO/YOUR/INSTALLATION/FOLDER/


    #========================================
    # USER INPUT
    #========================================

    echo "(1/5) ==================================================================="
    echo "Please enter a projectname:"
    echo "This name will be used for a new folder that contains the WP installation"
    echo "========================================================================="
    read -e project_name

    echo "(2/5) ==================================================================="
    echo "Please enter a name for the database:"
    echo "========================================================================="
    read -e db_name

    echo "(3/5) ==================================================================="
    echo "Please enter a database prefix :"
    echo "========================================================================="
    read -e db_prefix
         db_prefix=${db_prefix:-wp_}

    echo "(4/5) ==================================================================="
    echo "Please enter a name for the database user:"
    echo "========================================================================="
    read -e db_user

    echo "(5/5) ==================================================================="
    echo "Please enter a password for the database:"
    echo "========================================================================="
    read -s db_password


    #========================================
    # CREATE DATABASE AND DATABASE-USER
    #========================================

    echo "Creating the database..."
    mysql -uroot -proot -e "CREATE DATABASE $db_name; \
    GRANT ALL PRIVILEGES ON $db_name.* TO $db_user@localhost IDENTIFIED BY '$db_password'; \
    FLUSH PRIVILEGES;"


    #========================================
    # DOWNLOAD - CONFIGURE AND INSTALL WP
    #========================================

    cd $target_dir && \
    mkdir $project_name && \
    cd $project_name && \
    wp core download && \
    wp core config --dbname=$db_name --dbuser=$db_user --dbpass=$db_password --dbprefix=$db_prefix && \
    wp core install --prompt && \


    #========================================
    # DOWNLOAD THE UNDERSCORES STARTER THEME
    #========================================

    cd wp-content/themes && underscores -n $project_name && \
    cd $project_name && \


    #========================================
    # CREATE PACKAGE FILE AND INSTALL PACKAGES
    #========================================

    npm init && \
    npm install --save-dev gulp && \
    npm install --save-dev gulp-ruby-sass && \
    npm install --save-dev gulp-autoprefixer && \
    npm install --save-dev gulp-minify-css && \
    npm install --save-dev gulp-rename && \
    npm install --save-dev gulp-concat && \
    npm install --save-dev gulp-uglify && \
    npm install --save-dev gulp-watch && \
    npm install --save-dev gulp-livereload


    #========================================
    # DOWNLOAD GULPFILE FROM REPOSITORY
    #========================================

    curl -LO https://github.com/christian-van-deursen/starter/raw/master/gulp/gulpfile.js
}

Now when you open up the terminal and type the command “installwp” we have created a famous 1 minute install!