• Hello,

    I am using wordpress with SVN and I want to find a way to modify the WP-config file so that it works both locally and remotely through an IF statement. Basically I want to use the PHP to detect which server it is running on and then use the appropriate database.

    I am not very familiar with PHP but have heard that if you modify the wp-config file to include:

    if($_SERVER['HTTP_HOST'] == 'example.com'){
    
        // PRODUCTION
        $conf['db_hostname'] = "xxx";
        $conf['db_username'] = "xxx";
        $conf['db_password'] = "xxx";
        $conf['db_name'] = "xxx";
    
    } else {
    
        // DEV OR WHATEVER
        $conf['db_hostname'] = "xxx";
        $conf['db_username'] = "xxx";
        $conf['db_password'] = "xxx";
        $conf['db_name'] = "xxx";
    
    }

    But I am not really certain how to implement this code. Any thoughts?

Viewing 1 replies (of 1 total)
  • You can like you are doing, but I rather rely on a apache setted variable. You can do this on your .htaccess.

    .htaccess
    SetEnv ENVIRONMENT production

    wp-config.php

    define('ENVIRONMENT', getenv('ENVIRONMENT'));
    
    if (ENVIRONMENT == 'production'){
        define('DB_NAME', 	        'productionSettings');
        define('DB_USER', 	        'productionSettings');
        define('DB_PASSWORD', 	'productionSettings');
        define('DB_HOST',           'productionSettings');
        define('DB_CHARSET', 	'productionSettings');
        define('DB_COLLATE', 	'productionSettings');
    }else{
        define('DB_NAME', 	        'otherSettings');
        define('DB_USER', 	        'otherSettings');
        define('DB_PASSWORD', 	'otherSettings');
        define('DB_HOST',           'otherSettings');
        define('DB_CHARSET', 	'otherSettings');
        define('DB_COLLATE', 	'otherSettings');
    }

    The $_SERVER[‘HTTP_HOST’] is more to rely on a url base, like wordpress options siturl and home, that you could use like that:

    wp-config.php

    define('WP_SITEURL','https://'.$_SERVER['HTTP_HOST']);
    define('WP_HOME','https://'.$_SERVER['HTTP_HOST']);

    You still have to update post’s guid when changing the environment

Viewing 1 replies (of 1 total)
  • The topic ‘Modifying WP-config’s PHP to detect server and use appropriate database’ is closed to new replies.