How to deal with WordPress on localhost

You can use wp-config.php to change the site url depending on where the site is accesed from, using $_SERVER['REMOTE_ADDR']. Mine has something like this:

if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1' || $_SERVER['REMOTE_ADDR'] == '::1') { 
    // accesing site from my local server
    define('WP_SITEURL', 'http://localhost/mysite/');
    define('WP_HOME', 'http://localhost/mysite');
} else if (strpos($_SERVER['REMOTE_ADDR'],'192.168.0.') !== false) {
    // accesing site from another machine in my home network, 
    // all their (internal) network addresses begin with this number;
    // the next line provides the server's own internal network address 
    define('WP_SITEURL', 'http://192.168.0.192/mysite/');
    define('WP_HOME', 'http://192.168.0.192/mysite');
} else { //accesing site from outside home
    define('WP_SITEURL', 'http://89.*.*.*/mysite/'); //replace by your external home IP
    define('WP_HOME', 'http://89.*.*.*/mysite');
}
//error_log("Siteurl is ".WP_SITEURL);

This technique also helps a lot to simplify uploading the site to a production server or keeping in sync local and production versions of the site. (Though, obviously, the wp-config.php on the production server should not have this code.)

Note: For some reason, I cannot use my external home IP from other machines in my home network; if that’s not your case, you can remove the else if, leaving only the else part.

4 thoughts on “How to deal with WordPress on localhost”

  1. I have been browsing online more than three hours today, but I
    by no means found any attention-grabbing article like yours.
    It’s lovely price sufficient for me. In my view, if all web owners and
    bloggers made excellent content as you did, the web shall be
    much more helpful than ever before.

    Reply
  2. Thanks for a marvelous posting! I truly enjoyed reading it, you’re
    a great author.I will make sure to bookmark your blog and definitely will come back
    very soon. I want to encourage you to continue your great writing,
    have a nice weekend!

    Reply

Leave a Comment