WordPress as a submodule
TL;DR Get my WordPress Boilerplate or follow the instructions below to add WordPress as a git submodule. It’ll make bits of your WordPress project more modular, and living with WordPress a little bit nicer.
Making stuff easier is fun. Making tools for making stuff easier is fun. And sharing those tools is the best of funs.
Which is why I’ve created a WordPress Boilerplate. I use this boilerplate for all WordPress projects I do, and even converted a bunch of older projects to use this model. It’s awesome.
Please note, my boilerplate kinda requires you to use git. You need to recursively clone the boilerplate to get WordPress core and updating using the submodule is easy. Submodules are like external Git repos inside your repo, which you can update independently. Updating WordPress as a git submodule is as easy as git fetch && git checkout 3.5.1
. Apache and mod_rewrite for Apache are also required (but they’re pretty standard stuff).
The idea here is to separate the WordPress site’s components into their own directories right at the root. This creates a simple modular structure, where parts of a WordPress site are totally on their own. This makes things less complex and less frightening. Especially changing and updating things is easy, because you can just tweak and update a small part of a bigger system. Modularity <3
Benefits:
- Keeping WordPress up-to-date in the version control is easy with git
- Core WordPress has its own directory
- Themes, plugins and uploads are separated from the WordPress installation
- Modularity ensues and things are less frightening
Also, since the boilerplate currently works by cloning my git repo, you will get my version history. I’m thinking that some kind of a “create-wordpress-boilerplate” script, which would create an empty git repo with all the required things, could be a nicer solution. We’ll see…
Do-It-Yourself
You don’t have to use my boilerplate if you don’t want to. Below is the rough process on how to do it yourself with git. If you like some other version control system better, like SVN, you can of course use SVN externals instead. Or no version control at all and update WordPress manually (the pain!).
First, create a project directory:
mkdir my-project
cd my-project
git init
And add Wordpress as submodule:
git submodule add git://github.com/WordPress/WordPress.git wordpress
cd wordpress
git checkout 3.5
Then go back and create & edit the Wordpress config file:
cd ..
cp wordpress/wp-config-sample.php wp-config.php
nano wp-config.php
Add these path definitions to the config right after define('DB_COLLATE', …
, around line 35:
/** Absolute path to the WordPress directory. */ if ( !defined('ABSPATH') ) define('ABSPATH', dirname(__FILE__) . '/wordpress/'); define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']); define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/wordpress'); define('WP_CONTENT_DIR', realpath(ABSPATH . '../wp-content/')); define('WP_CONTENT_URL', WP_HOME . '/wp-content'); define('UPLOADS', '../uploads');
Create a .htaccess -file:
nano .htaccess
This is the meat. Add these rewrite rules to the .htaccess:
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / # BEGIN WordPress RewriteRule ^$ wordpress/index.php [L] # Skip real files and directories RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Otherwise send it to WordPress RewriteRule .* wordpress/index.php [L] # END WordPress </IfModule>
Finally, create the directories for uploads, themes and plugins, as we declared them into wp-config.php:
mkdir uploads mkdir -p wp-content/plugins wp-content/themes touch wp-content/index.php
Oh, and to update WordPress:
cd wordpress
git fetch --tags
git checkout 3.5.1
Phew! That’s a lot of steps, but now you should be all set. Now you just need to add your themes to the wp-content/themes directory and start working on the site.
If you have any questions or need assistance, don’t hesitate to tweet me or leave a comment below!