This package extends Symfony's symfony/dotenv
component to allow streamlined WordPress config via Environment Variables.
Please refer to the Symfony component's documentation about how .env files
should be used. It is important to note that .env files are ignored if the
APP_ENV var has already been set by the server. For performance purposes,
production environments should ideally rely on pre-configured environment variables,
rather than environment variable values loaded from .env files.
composer require unleashedtech/dotenv-wordpress
First, you'll need to configure WordPress to use this package.
WordPress is typically configured via the wp-config.php file.
To use this package with WordPress, some code will need to be added to the top of
the relevant wp-config.php:
<?phpuseUnleashedTech\WordPress\Dotenv\Dotenv;
$dotenv = $dotenv ?? newDotenv();
$dotenv->setConfig();If you have multiple WP applications running on your webserver, you'll want to specify
the name of the WP app in each wp-config.php file:
<?phpuseUnleashedTech\WordPress\Dotenv\Dotenv;
$dotenv = $dotenv ?? newDotenv();
$dotenv->setAppName('foo');
$dotenv->setConfig();That's it! WordPress will now attempt to load essential connection information from generic Environment Variables.
This package will provide many default setting & configuration values based on the detected environment. Some of these values can be populated by environment variables.
Environment variables can be set in .env files, or via modifying server configuration.
For production environments, environment variables should ideally be defined via server
configuration.
Multi-site installations often need config that differs from the default site.
This package first checks for variables following the {{ app }}__{{ site }}__{{ var }}
naming convention, before falling back to the {{ var }} naming convention.
You can provide site-specific information via namespaced environment variables.
- DATABASE_URL
- DOMAINS
- PUBLIC
- SITES
- More configuration options coming soon!
The default database connection can be configured via a DSN:
DATABASE_URL=driver://user:password@host:port/databaseFor example:
DATABASE_URL=mysql://foo:bar@localhost:3306/bazFor multi-site installations, do not specify a database name nor credentials in
the DATABASE_URL variable:
DATABASE_URL=mysql://localhost:3306For an "earth" WordPress App with a "default" site & an "antarctica" site:
DATABASE_URL=mysql://localhost:3306EARTH__DEFAULT__DATABASE_USER=fooEARTH__DEFAULT__DATABASE_PASSWORD=barEARTH__ANTARCTICA__DATABASE_USER=bazEARTH__ANTARCTICA__DATABASE_PASSWORD=quxThe default WordPress App Name is "default". For most use cases, you'll want to set the
WordPress App Name in the default settings.php file, as shown below:
<?phpuseUnleashedTech\WordPress\Dotenv\Dotenv;
$dotenv = $dotenv ?? newDotenv();
$dotenv->setAppName('earth');
// ...A CSV list of domains used by the given environment:
DOMAINS=foo.example,bar.example,baz.exampleA string allowing the enabling/disabling of basic auth functionality.
If true, basic will not be enabled.
If false, the username will be the App Name & the password will
be the Site Name.
A CSV list of WordPress "sites" (e.g. "subdomains") used by the given environment:
SITES=foo,bar,baz,quxThe machine name of the WordPress App (e.g. "default" or "earth").
The site name for a WordPress App Site (e.g. "default" or "antarctica").
With these few Environment Variables, you will be able to configure WordPress in a streamlined fashion similar to the way Symfony is configured. Support for many more common WordPress features can be expected soon. Please consider creating a Pull Request with features you would like to this package to support.