Keeping WordPress Under [Version] Control with Git

Steve Grunwell /

Warning:

This is not an "Introduction to Git"-type of course. While previous Git/version control experience is not required it is recommended!

Want to learn Git basics? Buy me a beer at the afterparty!

Git resources

WTF is Git?

  • Distributed Version Control System (VCS)
  • Created by Linus Torvalds (see: Linux)

Advantages of Version Control

  • Keep track of what changed, who made it, and when it happened
  • Easily roll back changes when necessary
  • With some systems (including Git), branching is cheap and easy, which makes it easier to develop features in parallel
  • Makes it easier to collaborate with other developers
The Github OctoCat mascot dressed as Walter White (a.k.a. Heisenberg) from Breaking Bad

Jesse, it's time to code!

What version control is not

Version control is not meant to be a replacement for real, scheduled backups.

Since every little change is tracked, it's not a great place to do debugging. Avoid committing untested or broken code to the repository!

Using Git with WordPress

Goals of the Workflow

  1. Make setup + deployments easier
  2. Keep sensitive information out of the repository
  3. Never have a git status return modified files on production

Bonus: Go home on time, free of headaches.

What's included?

This strategy includes WordPress core and plugins in the repository.

Components could be adapted for another workflow that excludes these items.

New environments in five steps

  1. Clone repository
  2. Import database
  3. Fill out wp-config.php
  4. Setup virtual host (if necessary)
  5. Party

The .gitignore file

# Keep these files out of the repo
/wp-content/plugins/hello.php
/wp-content/themes/twenty*
/wp-content/upgrade
/wp-content/uploads
/sitemap.*
/wp-config.php
*.sql

# Hidden files
*.DS_Store
*Thumbs.db
*.sass-cache*
*~imageoptim*

# Capistrano
REVISION

Why keep out so much?

  • Config information will vary between environments
  • Generated content (user- or otherwise) has no place in a code repository
  • Remember: Version control !== backup

Reasons to keep uploads out of the repo

  • Uploads in the repo mean as soon as anything's uploaded your repo is out of date
  • Uploads + thumbnails = heavy repo w/ no benefit
  • Again, version control is not a site backup!

Behold: A Better Way

<IfModule mod_rewrite.c>
  RewriteEngine on

  # Attempt to load files from production if they're not in our local version
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule wp-content/uploads/(.*) http://{PROD}/wp-content/uploads/$1 [NC,L]
</IfModule>

What does it do?

Does the requested file exist in the local filesystem?

  • Yes

    Load it as we normally would
  • No

    Attempt to load the file from production

What does it mean?

No more FTP-ing files to your dev/staging environments!

Ron Burgundy and his news team jumping for joy

Dealing with wp-config.php

Create a fresh wp-config.php file

$ git clone {REPO} {DIRECTORY}
$ cd {DIRECTORY}
$ cp wp-config-sample.php wp-config.php
$ vi wp-config.php

WP_SITEURL and WP_HOME

Defining these values in wp-config.php eliminate the need to edit these values in the wp_options database table

// NO trailing slash!
define('WP_SITEURL', 'http://example.com');
define('WP_HOME', WP_SITEURL);

DISALLOW_FILE_MODS or DISALLOW_FILE_EDIT

Prevent non-developers from installing/updating plugins or themes.

// Disable the theme and plugin installers and editors...
define('DISALLOW_FILE_MODS', true);

// ...or just disable the theme/plugin editor
define('DISALLOW_FILE_EDIT', true);

WordPress automatic updates

WordPress automatic updates won't run when evidence of version control is found, which is pretty baller.

Captain Kirk and Dr. McCoy nodding in approval

Other important items in wp-config.php

  • Database credentials
  • Keys + salts
  • Debug mode (where applicable)

Put it all together

  1. Clone repository
  2. Import database
  3. Fill out wp-config.php
  4. Setup virtual host (if necessary)

Step 5: Party!

A DJ rocking a party

Deploying new updates

$ git pull

…and that's it.

(unless you use better deployment tools)

Questions?

Steve Grunwell
Developer, Buckeye Interactive
stevegrunwell.com
@stevegrunwell

Slides:
stevegrunwell.github.io/wordpress-git