I already searched here and there but couldn't find anything definitive.
I have WordPress multisite and I have a plugin (activated on each site) which requires to set a slug parameter in wp-config.php.
Unfortunately each site of the network requires a different slug parameter.
I'd like to know if it is possible to put an if statement in wp-config.php in order to define these parameters based on $_SERVER['HTTP_HOST']
variable.
Code is the following:
if ( stristr( $_SERVER['HTTP_HOST'], 'site' ) ) {
define('ABC_PRODUCT_SLUG', 'slug-for-en');
}
elseif ( stristr( $_SERVER['HTTP_HOST'], 'de.site' ) )
{
define('ABC_PRODUCT_SLUG', 'slug-for-de');
}
Is that possible and reliable? Are there any possible drawbacks?
Thanks
I already searched here and there but couldn't find anything definitive.
I have WordPress multisite and I have a plugin (activated on each site) which requires to set a slug parameter in wp-config.php.
Unfortunately each site of the network requires a different slug parameter.
I'd like to know if it is possible to put an if statement in wp-config.php in order to define these parameters based on $_SERVER['HTTP_HOST']
variable.
Code is the following:
if ( stristr( $_SERVER['HTTP_HOST'], 'site' ) ) {
define('ABC_PRODUCT_SLUG', 'slug-for-en');
}
elseif ( stristr( $_SERVER['HTTP_HOST'], 'de.site' ) )
{
define('ABC_PRODUCT_SLUG', 'slug-for-de');
}
Is that possible and reliable? Are there any possible drawbacks?
Thanks
Share Improve this question edited Jan 29, 2018 at 17:36 Nathan Johnson 6,5286 gold badges30 silver badges49 bronze badges asked Jan 29, 2018 at 17:06 AntonyAntony 1093 bronze badges 12 | Show 7 more comments1 Answer
Reset to default 1Following on from the comments thread...
Here's a method I've used for a long time for handling the 'not ideal' scenario of different configurations between environments. The following is a heavily cut-down representation of wp-config.php
but it demonstrates the idea...
<?php
/**
* The base configurations of the WordPress.
*
* This file has the following configurations: MySQL settings, Table Prefix,
* Secret Keys, WordPress Language, and ABSPATH. You can find more information
* by visiting {@link http://codex.wordpress/Editing_wp-config.php Editing
* wp-config.php} Codex page. You can get the MySQL settings from your web host.
*
* This file is used by the wp-config.php creation script during the
* installation. You don't have to use the web site, you can just copy this file
* to "wp-config.php" and fill in the values.
*
* @package WordPress
*/
/** Environment: LOCAL || STAGING || LIVE **/
define('BCUK_SITE_ENV', 'LOCAL');
/** System/CMS defaults **/
e.g. settings that are common across all environments
if (BCUK_SITE_ENV == "LOCAL"):
e.g. different db settings, memory limits, paths, etc.
elseif (BCUK_SITE_ENV == "STAGING"):
e.g. different db settings, memory limits, paths, etc.
else:
e.g. different db settings, memory limits, paths, etc.
endif;
// Remaining wp-config bits go here e.g. salts, lang settings, etc
As you can see I prefer to assume that 'else' means the live settings, just for safety's sake.
I have also used versions of this method where the definition of the environment is actually in a separate file that is included e.g. replacing define('BCUK_SITE_ENV', 'LOCAL');
with an include statement that pulls in a file (something like 'environment_config.inc.php') in which there is just one line that is used to change the environment constant. That way you can always push your wp-config.php between environments without actually affecting which environment is currently configured.
In all the above, you could easily adapt it to either base your logic directly on the value of $_SERVER['HTTP_HOST']
or to configure your environment variable/constant on the value of $_SERVER['HTTP_HOST']
.
Hope that all makes sense. It's definitely not the only way, but hopefully it demonstrates that it is definitely a robust and workable method. Whether it works for your particular plugin development scenario only you will know.
SITE_ENVIRONMENT
constant forlocal | staging | live
and have variable settings dependent on the environment. Ideally you would define the constant elsewhere (and then include/fetch it) so that you can still push your wp-config between environments without overwriting the environment value. Note: Some hosts - especially dedicated Wordpress managed hosts - don't let you manage wp-config. They always supply workarounds though. – onebc Commented Jan 29, 2018 at 17:28