Web Development eventually involves dealing with a wordpress site (or multiple), and if you’re like me - you have a bunch of private git repos that all lack wp-config.php files.

And if you’re also like me, you probably have a custom git command for hard resets and cleans (I call mine <code>git bleach</code> ) - which means you do a lot of rebuilding, and probably waste a lot of time creating your configs over and over again. So I automated that ish.

This post assumes you know some basic node/bash behavior. I also prefer to use Sage for Wordpress theme development.

I use two files (specific to each repo) for bootstrapping

My package.json at the project root looks like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{
  "name": "client-wordpress-site",
  "private": true,
  "version": "1.0.0",
  "description": "client wordpress site",
  "repository": "git://github.com/troyxmccall/client-wordpress-site",
  "scripts": {
      "blog:bower": "cd wp-content/themes/sage && bower install",
      "blog:build": "cd wp-content/themes/sage && gulp",
      "blog:watch": "cd wp-content/themes/sage && gulp watch",
      "postinstall": "npm install wp-content/themes/sage  --prefix ./wp-content/themes/sage"
    },
  "devDependencies": {
    "bower": "^1.7.9",
    "gulp": "^3.9.1",
    "gulp-shell": "^0.5.2"
  }
}

The important things to note here are the scripts, which we’ll call from our bootstrap.sh shell script which looks something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env bash

function install_dependencies() {
  echo -e "\033[32m Installing needed Node Modules on OSX"
  yarn
}

function setup_wp_config() {
  if [ ! -e wp-config.php ]; then
    echo "setting up your wp-config";
    cp wp-config-sample.php wp-config.php
    sed -i "/DB_HOST/s/'[^']*'/'localhost'/2" wp-config.php
    sed -i "/DB_NAME/s/'[^']*'/'YOUR_DB_NAME_HERE'/2" wp-config.php
    sed -i "/DB_USER/s/'[^']*'/'vagrant'/2" wp-config.php
    sed -i "/DB_PASSWORD/s/'[^']*'/'vagrant'/2" wp-config.php

    sed -i "s/define('WP_DEBUG'.*/define('WP_DEBUG', true);/g" wp-config.php

    SECRETKEYS=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)
    EXISTINGKEYS='put your unique phrase here'
    printf '%s\n' "g/$EXISTINGKEYS/d" a "$SECRETKEYS" . w | ed -s wp-config.php
  fi
}

function is_osx() {
 [[ "$OSTYPE" =~ ^darwin ]] || return 1
}

is_osx || return 1
install_dependencies
setup_wp_config

now just run source bootstrap.sh from your project root and you’re ready to gulp and get to work debugging whatever the fuck is broken. Make sure to update YOUR_DB_NAME_HERE and my default vagrant db username/password if you have different mysql users.