I’ve been using MAMP to develop WordPress sites on my local machine for years, but I continually refine my wp-config.php boilerplate file. Here’s the current iteration of how I handle the DB settings for local versus remote connections.
if( substr( $_SERVER['HTTP_HOST'], -4 ) == '.loc' ){ /* local environment */
define( 'DB_HOST', 'localhost' );
define( 'DB_NAME', 'your_db_name' );
define( 'DB_USER', 'root' );
define( 'DB_PASSWORD', 'root' );
define( 'WP_LOCAL_DEV', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', true );
ini_set( 'display_errors', 1 );
} else { /* remote environment */
define( 'DB_HOST', 'localhost' );
define( 'DB_NAME', 'DBNAME' );
define( 'DB_USER', 'DBUSER' );
define( 'DB_PASSWORD', 'DBPASS' );
define( 'WP_DEBUG', false );
define( 'WP_DEBUG_DISPLAY', false );
define( 'WP_LOCAL_DEV', false );
ini_set( 'display_errors', 0 );
}
In the conditional statement above, we’re using the PHP native function substr
to test whether we are using an HTTP_HOST with a top-level domain of “.loc” (which is arbitrary and can be anything). In addition to enabling debugging constants, I’m setting a constant WP_LOCAL_DEV which I use in my theme.
For my MAMP environment, I’m adding virtual hosts to my apache httpd.conf and hosts files. In my httpd.conf file, in the “Virtual Hosts” section, I have this:
Include /Applications/MAMP/conf/apache/vhosts/*.conf
Then I have a “vhosts” folder that contains all the definitions of my sites. For example, for site “examplesite”, the filename would be “httpd-vhosts-examplesite.conf” and inside it would have the following code:
<VirtualHost *>
DocumentRoot "/Users/shawn/Documents/My Sites/Example Site/"
ServerName examplesite.loc
</VirtualHost>
The ‘DocumentRoot’ is where your installation of WordPress will be. Then, in your hosts file (on a Mac it’s usually at /private/etc/hosts), you’d just add an IP directive like this:
127.0.0.1 examplesite.loc
The conditional statement above lets me use the same wp-config.php file for local and production servers making maintenance simpler.
Comment