WordPress.org

Ready to get started?Download WordPress

Forums

Modifying WP-config's PHP to detect server and use appropriate database (2 posts)

  1. shalzers
    Member
    Posted 3 years ago #

    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?

  2. rafaelxy
    Member
    Posted 3 years ago #

    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','http://'.$_SERVER['HTTP_HOST']);
    define('WP_HOME','http://'.$_SERVER['HTTP_HOST']);

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

Topic Closed

This topic has been closed to new replies.

About this Topic