Single wp-config for web and localhost

Published: On Saturday, August 8, 2009

Pretty usual routine is to develop site on localhost server and after some progress move it online. This transfer is complicated by three not-always-necessary steps.
  1. database settings in wp-config.php file are different
  2. settings of home- and site- URLs are different (defaultly located in wp_options table)
  3. eventually the base-path in .htaccess file is different

First two points can be easily solved by using single wp-config. Especially is the benefit visible at the point #2, when you don't have to browse phpMyAdmin for that 2 particular rows, because these paths are written into wp-config.php file.

This is the code of example wp-config:

<?php
/**
 * Web first, Localhost later
 * Remember: no slash at the end of paths!
 */
if (false === strpos($_SERVER['HTTP_HOST'], 'localhost')) {
        define('WP_HOME', 'http://www.example.com');
        define('WP_SITEURL', 'http://www.example.com');

        define('DB_NAME', 'lorem');
        define('DB_USER', 'ipsum');
        define('DB_PASSWORD', 'lioprseumm');
        define('DB_HOST', 'localhost');

} else {
        define('WP_HOME', 'http://localhost:8888/work/lipsum');
        define('WP_SITEURL', 'http://localhost:8888/work/lipsum');

        define('DB_NAME', 'work');
        define('DB_USER', 'root');
        define('DB_PASSWORD', 'root');
        define('DB_HOST', 'localhost');

        define ('WP_POST_REVISIONS', false);
        define ('WP_DEBUG', true);
}

$table_prefix  = 'wp_lipsum_';

// define ('WP_CACHE', true);
// define ('WPLANG', 'cs_CZ');


/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

/**
 * Authentication Unique Keys.
 *
 * @link https://api.wordpress.org/secret-key/1.1/
 */
define('AUTH_KEY',        ';cO~7QnUU9q_2Va]UFM//B7_|$y_TG`{Xlx|k=N~iB`*Y)|DGU@aG^%%H+SeG3y4:');
define('SECURE_AUTH_KEY', 'Y/clCD>y%SU,KTS{)7}z&ia+y^y>5h|,:R5cX0#x7qK]YMm.+!ct$u.CaRdYCmGdX');
define('LOGGED_IN_KEY',   '41Bj/[_(&-c|1jh]hObd+c+ow#JYne;_0Topv#I]_Vw;V:DtFAFV?b@Ny*PaSY1v1');
define('NONCE_KEY',       'IMIcx#R2]hEXi^(CLhyQ9N:J%bY-+@R<}n7bAptFcf)=w~xM+5[+8a~X!4sAyB1_w');


/* That's all, stop editing! Happy blogging. */

/** WordPress absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
        define('ABSPATH', dirname(__FILE__) . '/');

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

For other optional settings inspiration look into Codex / Editing wp-config.

Interesting: did you know that the old b2/cafelog has these URLs configurable in config-file and the database way was added later? I can't understand why this change happened since having it in config-file I consider much more comfortable…

So I hope this trick will make you site movings little bit simpler.


Comments

  1. 1.  OndroNR wrote: On August 8., 2009 comment number 1

    what about sites like localhost.sk? :)

  2. 2.  Kahi [author] wrote: On August 9., 2009 comment number 2

    ↪ OndroNR :-) Well, for these rare cases you might get inspired by your own solution here http://kahi.cz/wor­dpress/localhost-web-switch-function/ or just get more specific in the first if-condition… which you already know, I suppose :)

I quit working with WordPress, comments are closed. My plugins will not be updated any more – at least not by me. Feel free to modify my source codes though… Also I am not able to provide support, sorry. –Kahi