Skip to content

Instantly share code, notes, and snippets.

@kingkool68
Created November 27, 2021 04:10
Show Gist options
  • Save kingkool68/2de11379373ee74a76337a3638754665 to your computer and use it in GitHub Desktop.
Save kingkool68/2de11379373ee74a76337a3638754665 to your computer and use it in GitHub Desktop.
Managing `wp-config.php` files for different environments. This allows you to version control your configurations.
<?php
/** Loads environment specific config files for WordPress.
* See https://github.com/studio24/wordpress-multi-env-config and/or http://abandon.ie/notebook/wordpress-configuration-for-multiple-environments
*
* Environment can be:
* 1. Directly loaded via an Environment variable (Name: WP_ENV)
* 2. Looks the ServerName attribute
* 3. Default to production (if no other one is found)
*
* ** Usage **
* a. Add the following to your wp-config.php file.
*
* $environments = array(
* 'development' => 'coderpad.local',
* 'production' => 'coderpad.io'
* );
* include 'wp-config.environments.php';
*
* b. Add environment specific files with DB connection information, named like so:
* wp-config.production.php
* wp-config.development.php
**/
// Try environment variable 'WP_ENV'
if ( getenv( 'WP_ENV' ) !== false ) {
// Filter non-alphabetical characters for security
define( 'WP_ENVIRONMENT_TYPE', preg_replace( '/[^a-z]/', '', getenv( 'WP_ENV' ) ) );
}
if ( ! defined( 'WP_ENVIRONMENT_TYPE' ) ) {
// Get Server name
$server_name = $_SERVER['SERVER_NAME'];
foreach ( $environments as $key => $env ) {
if ( strstr( $server_name, $env ) ) {
define( 'WP_ENVIRONMENT_TYPE', $key );
break;
}
}
}
// If no environment is set default to production
if ( ! defined( 'WP_ENVIRONMENT_TYPE' ) ) {
define( 'WP_ENVIRONMENT_TYPE', 'production' );
}
// Load config file for current environment
require 'wp-config.' . WP_ENVIRONMENT_TYPE . '.php';
<?php
// You can git-ignore the local development wp-config file which may be different for different developers
define( 'WP_SITEURL', '' );
define( 'WP_HOME', '' );
define( 'DB_NAME', 'local' );
define( 'DB_USER', 'root' );
define( 'DB_PASSWORD', 'root' );
define( 'DB_HOST', 'localhost' );
define( 'WP_REDIS_DISABLED', true );
define( 'EMPTY_TRASH_DAYS', 0 );
define( 'WP_DEBUG', true );
// If WP_DEBUG is true then enable other fun goodies
if ( WP_DEBUG ) {
define( 'WP_DEBUG_LOG', true );
define( 'SAVEQUERIES', true );
}
<?php
// Add this to your wp-config.php file
$environments = array(
'local' => 'example.local',
'staging' => 'staging.example.com',
'production' => 'example.com',
);
require dirname( __FILE__ ) . '/wp-config.environments.php';
<?php
// See a list of wp-config constants https://gist.github.com/MikeNGarrett/e20d77ca8ba4ae62adf5
$host = $_SERVER['SERVER_NAME'];
define( 'WP_SITEURL', 'https://' . $host );
define( 'WP_HOME', 'https://' . $host );
define( 'DB_NAME', '' );
define( 'DB_USER', '' );
define( 'DB_PASSWORD', '' );
define( 'DB_HOST', 'localhost' );
// Prevent plugin updates and file editing
define( 'FS_METHOD', 'direct' );
define( 'DISALLOW_FILE_EDIT', true );
define( 'DISALLOW_FILE_MODS', true );
// Define directroy and file permissions
define( 'FS_CHMOD_DIR', ( 0775 & ~ umask() ) );
define( 'FS_CHMOD_FILE', ( 0664 & ~ umask() ) );
// Object caching
define( 'WP_CACHE', true );
define( 'WP_REDIS_PREFIX', 'prod_' );
// Use external cron instead of WordPress' cron
define( 'DISABLE_WP_CRON', true );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment