Local WordPress Development

I want to share tips and tricks about local WordPress development. I’m using similar setup for handle large WordPress sites.

What we need?

  • Git for version control (You have your site in a Git repository)
  • VirtualBox (because we’ll use vagrant)
  • Vagrant
  • Varying Vagrant Vagrants

Requirements

1) Setup Local Site

Download and install virtualbox (for your OS) and then install vagrant after that download Varying Vagrant Vagrants and setup your site. It’s really to setup, you can find detailed information on wiki pages.

2) Local Configurations

We’ll follow Mark Jaquith‘s way for local configuration.

First of all, you never commit configuration files to git.  So (if you didn’t before) add wp-config and local-config.php to .gitignore

a) Modify Wp-config

Your database user and password are (or should) be different on your localhost than they are on your production environment. One way to handle this is to have wp-config.php not be in version control and have everyone make their own. Boo. Stop taking things out of version control. Another solution I’ve seen is that people modify the wp-config.php and just try to be really careful not to commit their local changes.

Here’s how to do it. Open up your wp-config.php file.

if ( file_exists( dirname( __FILE__ ) . '/local-config.php' ) ) {
include( dirname( __FILE__ ) . '/local-config.php' );
define( 'WP_LOCAL_DEV', true ); // We'll talk about this later
} else {
define( 'DB_NAME', 'production_db' );
define( 'DB_USER', 'production_user' );
define( 'DB_PASSWORD', 'production_password' );
define( 'DB_HOST', 'production_db_host' );
}

We need to create local-config.php

b) local-config.php

create local-config.php file and define your database settings and real prodoction url.

You can download and modify my config file.


define( 'UBP_SITEURL', 'http://example.com'); // We'll talk about this later
define( 'UBP_IS_LOCAL', true );

define('DB_NAME', 'dev_db_name');
define('DB_USER', 'dev_db_username');
define('DB_PASSWORD', 'dev_db_pass');
define('DB_HOST', 'localhost');

Our site working with our local configuration. Yay! we can jump to next step.

3) Plugins plugins…

Time to explain   ‘WP_LOCAL_DEV’ and  ‘UBP_SITEURL’

a) Update Site Url

We need to update ‘siteurl’ and ‘home’ for development stage. You can do this with directly change on database but we have easier solution.

Create a php file in wp-content/mu-plugins directory and add this lines; (if mu-plugins doesn’t exist, create it)

if ( defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV ) {
	update_option('siteurl','http://local.example.dev');
	update_option('home','http://local.example.dev');
}

You can visit ‘local.example.dev’ and you should see it’s work ( Of course you need to define local.example.dev in hosts file with vagrant ip address )

b) Uploads!

upload-proxy

We need to access our media files but we shouldn’t keep them on git. (Think about a WordPress site and they have over 100G static content)
(ideally) We can access our static contents with proxy and there is a WordPress plugin working pretty well.

Download and activate “Uploads by Proxy” plugin and return local-config.php file, make sure UBP_SITEURL is your website url.

But I don’t want to activate manually everytime so,

create a new php file in mu-plugins directory and add these lines.


if ( defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV ) {

function activate_local_plugin( $plugin ) {
 $current = get_option( 'active_plugins' );
 $plugin = plugin_basename( trim( $plugin ) );

if ( !in_array( $plugin, $current ) ) {
 $current[] = $plugin;
 sort( $current );
 do_action( 'activate_plugin', trim( $plugin ) );
 update_option( 'active_plugins', $current );
 do_action( 'activate_' . trim( $plugin ) );
 do_action( 'activated_plugin', trim( $plugin) );
 }

return null;
 }

activate_local_plugin('uploads-by-proxy/uploads-by-proxy.php');
}

Putting all together: Here is example https://gist.github.com/mustafauysal/2487dc1ff3eceff2804e

c) Disable Plugins

Sometimes you need to disable plugins on development stage, because they only work “production-only” way. Think about vaultpress, backup plugins etc.. probably they won’t work on local.

” We need to disable them.”

Thanks Mark Jaquith again handy  “must use” plugin – get it here and put it under mu-plugins directory.

The part you modify is at the bottom:

 new CWS_Disable_Plugins_When_Local_Dev( array( 'vaultpress.php' ) ); 

Now those plugins won’t be active when doing local dev (based on the presence of local-config.php).

Links: