سؤال

I'm running a WP site on my laptop using localhost, and I often need my coworkers to see it. If they enter my home's IP, they were able to access the site, but all the URL's in WP that used site_url() or similar were echoing out "localhost" which of course didn't work for outsiders.

So I changed WP to use my IP for the site URL which solved that problem, but created another. If I bring my laptop away from home and try to view my site, now all the links appear as http://home-ip/ which isn't available. Furthermore, I'm unable to to get into wp-admin to change the site URL back to localhost since I'm being redirected to http://home-ip/site/wp-admin/.

Is there a way to deal with this without having to constantly change the URL every time I want someone else to access it from outside, or every time I leave the house.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

Although the accepted answer looks to be a good one, just changing WP_SITEURL/WP_HOME doesn't work in my case because there always ends up being lots of absolute links in the text content on pages (i.e. images or links added through the WYSIWYG editor). In other words, I pretty much have to use the same domain if I want the site to work properly.

So in case it helps someone else who comes across this, my solution was an outside-of-Wordpress one - although it may not help in all cases.

I use a domain, for example me.mycompany.com, and then in my HOSTS file I make it resolve to 127.0.0.1 (in other words, localhost). For any of my colleagues who need to view the site, I add my domain to their HOSTS file with my local IP.

This solution also gets extended when clients outside our network need to view the site; we simply make sure me.mycompany.com resolves to our public IP (usually your web host can help with this, and it definitely helps if you have a static IP for your Internet connection too), and then at our router, route the web requests to my internal IP, effectively setting up an easy web hosting solution (for development only of course, not production!)

Hope this helps someone. The HOSTS file can be edited on any platform, obviously the instructions differ for Windows, Mac and Linux so best you Google for help if you need it, but in Windows the file is C:\Windows\System32\drivers\etc\hosts - open it in Notepad and make the neccessary changes, following the format in the file (you will need to run as Administrator in order to save the file).

From WordPress 5.5.1 you can use the wp_get_environment_type fuction. Set wp_get_environment_type within wp-config.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى wordpress.stackexchange
scroll top