A Better WordPress Config

Author: Chris Everson | 2 minute read

A Better WordPress Config

Keeping WordPress running smoothly in multiple environments throughout the development and support process can feel a bit like juggling chainsaws, but it doesn't have to.


Here at Flywheel, we develop and deploy code in a minimum of three separate environments: local, staging, and production. Each of these environments needs a dedicated configuration. There are a number of different ways to supply environment-specific configurations to WordPress, and over the years we have used most methods out there before finally landing on PHP dotenv to define configuration values to be consumed by a modified version of wp-config.php.

PHP dotenv is a library that provides an easy and unobtrusive method of defining custom environment variables. Environment-specific configuration values can then be set in a .env file. This new file should not be committed to your version control system. This way, it applies solely to the environment it resides on.

As a simple example, let's migrate some basic database configuration values to be supplied via the .env file.

Firstly, we need to require the PHP dotenv package using Composer:

composer require vlucas/phpdotenv

Once PHP dotenv is installed, we need to update our wp-config.php file to include the library and initialize it. Add the following code above the first configuration definition:

require_once 'vendor/autoload.php';

$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();

Now that we have PHP dotenv included and initialized, we can go ahead and update the configuration values to use our environment variables. For this tutorial, we'll be updating DB_NAME, DB_USER, DB_PASSWORD and DB_HOST to be retrieved from the .env file. In this example, we'll use a standard PHP 7 null coalescing operator for supplying a default, however you may find writing a dedicated function or method helpful as you expand your usage.

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', $_ENV['DB_NAME'] ?? 'database_name_here'));

/** MySQL database username */
define('DB_USER', $_ENV['DB_USER'] ?? 'username_here'));

/** MySQL database password */
define('DB_PASSWORD', $_ENV['DB_PASSWORD'] ?? 'password_here'));

/** MySQL hostname */
define('DB_HOST', $_ENV['DB_HOST'] ?? 'localhost'));

The final step is to create our .env file in the project root and define our new environment variable keys and values:

DB_NAME=database_name_here
DB_USER=username_here
DB_PASSWORD=password_here
DB_HOST=localhost

Our modified version of wp-config.php should now look like this:

<?php
/**
 * The base configuration for WordPress
 * [...]
 */

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', $_ENV['DB_NAME'] ?? 'database_name_here'));

/** MySQL database username */
define('DB_USER', $_ENV['DB_USER'] ?? 'username_here'));

/** MySQL database password */
define('DB_PASSWORD', $_ENV['DB_PASSWORD'] ?? 'password_here'));

/** MySQL hostname */
define('DB_HOST', $_ENV['DB_HOST'] ?? 'localhost'));

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

[...]

This same method can be used to create a tailored and powerful wp-config.php file by utilizing more of the available configuration variables. As time goes on, you'll continue finding ways in which you can implement PHP dotenv into your multi-environment development workflow, keeping management and configuration as fluid as possible.


Flywheel Co. : Chris Everson - Development Lead
Development Lead

Chris Everson

Chris is an extraordinary programmer and source of comedy at Flywheel. Although he once had a desk in the office, he has since relocated to Australia and is our international wing.

TOP

Keep Reading

Our Blog

More thoughts and ideas from the Flywheel team.