Last active
January 4, 2019 21:48
-
-
Save Abban/5816521 to your computer and use it in GitHub Desktop.
WP Config for multiple environments. Code is for this blog post: http://abandon.ie/wordpress-configuration-for-multiple-environments/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Define different DB connection details depending on environment | |
switch(ENVIRONMENT){ | |
case 'local': | |
define('DB_NAME', 'bootstrap'); | |
define('DB_USER', 'root'); | |
define('DB_PASSWORD', 'root'); | |
define('DB_HOST', 'localhost'); | |
define('WP_DEBUG', true); | |
define('WP_SITEURL', 'http://bootstrap.local/'); | |
define('WP_HOME', 'http://bootstrap.local/'); | |
break; | |
case 'development': | |
define('DB_NAME', 'bootstrap'); | |
define('DB_USER', 'root'); | |
define('DB_PASSWORD', 'root'); | |
define('DB_HOST', 'localhost'); | |
define('WP_DEBUG', true); | |
break; | |
case 'staging': | |
define('DB_NAME', 'database_name_here'); | |
define('DB_USER', 'username_here'); | |
define('DB_PASSWORD', 'password_here'); | |
define('DB_HOST', 'localhost'); | |
break; | |
case 'preview': | |
define('DB_NAME', 'database_name_here'); | |
define('DB_USER', 'username_here'); | |
define('DB_PASSWORD', 'password_here'); | |
define('DB_HOST', 'localhost'); | |
break; | |
} | |
// If database isn't defined then it will be defined here. | |
// Put the details for your production environment in here. | |
if(!defined('DB_NAME'))define('DB_NAME', 'database_name_here'); | |
if(!defined('DB_USER')) define('DB_USER', 'username_here'); | |
if(!defined('DB_PASSWORD')) define('DB_PASSWORD', 'password_here'); | |
if(!defined('DB_HOST')) define('DB_HOST', 'localhost'); | |
if(!defined('DB_CHARSET')) define('DB_CHARSET', 'utf8'); | |
if(!defined('DB_COLLATE')) define('DB_COLLATE', ''); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Define Environments | |
$environments = array( | |
'local' => '.local', | |
'development' => '.dev', | |
'staging' => 'stage.', | |
'preview' => 'preview.', | |
); | |
// Get Server name | |
$server_name = $_SERVER['SERVER_NAME']; | |
foreach($environments AS $key => $env){ | |
if(strstr($server_name, $env)){ | |
define('ENVIRONMENT', $key); | |
break; | |
} | |
} | |
// If no environment is set default to production | |
if(!defined('ENVIRONMENT')) define('ENVIRONMENT', 'production'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function is_production(){ | |
if(ENVIRONMENT == 'production'){ | |
return true; | |
}else{ | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if(!is_production()): ?> | |
<script type="text/javascript">// <![CDATA[ | |
var less = { env: 'development' }; | |
// ]]></script> | |
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/assets/js/libs/less-1.3.0.min.js"></script> | |
<script charset="utf-8" type="text/javascript">// <![CDATA[ | |
less.watch(); | |
// ]]></script> | |
<?php else: ?> | |
<?php endif; ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function password_protected(){ | |
if(!is_user_logged_in() && (ENVIRONMENT == 'development' || ENVIRONMENT == 'staging')){ | |
wp_redirect(get_option('siteurl') .'/wp-login.php'); | |
} | |
} | |
add_action('template_redirect', 'password_protected'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function robots_access(){ | |
if(is_production() && get_option('blog_public') == '0') update_option('blog_public', '1'); | |
if(!is_production() && get_option('blog_public') == '1') update_option('blog_public', '0'); | |
} | |
add_action('init', 'robots_access'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment