6

I have a local Xampp wordpress installation that I am using as sort of an intranet with some people I work with. I require them to be able to access it on our LAN router -- I found that I needed to change the site url and links from http://localhost:8080 to my IP http://192.168.x.xx:8080 for images and css to show.

However, I have found that when we are connected to a different router, or my travel router, this IP changes and obviously makes it not work on the LAN. I'm not really wanting to search/replace and change the site name every time a new computer and/or server is hosting the local site.

Question: So I'm really interested to see if there is a way to make the site/home URL dynamic to the hosting computer's current IP or computername. Or if I'm looking for the wrong type of solution.

I have searched extensively for a solution to this, but I feel my problem is I'm not sure what terms to search for -- or if there is a better solution. I hope someone smart could point me in the right direction.

-Based on my internet searches, I have tried a couple plugins - Relative URLs and "Root relative URLS" in hopes it would fix but it has not made a difference. -I have also set a static IP address in my travel router - however, the problem persists that I would need to change the ip address in the site if the computer changes. -I've also tried this in my wp-config:

<?php
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);
//add the next line if you have a subdirectory install
define('WP_SITEURL', WP_HOME . '/wordpress');
2
  • Usually you can get around these problems by using the hosts file, preventing the need for a different URL completely
    – Tom J Nowell
    Feb 27, 2015 at 1:03
  • Thank you for the tip, I will start looking into that realm. Feb 27, 2015 at 2:46

2 Answers 2

16

I usually just avoid the issue entirely every time I create a new wordpress site:

define('WP_HOME', '/');
define('WP_SITEURL', '/');

will cause wordpress to use root-relative urls for everything. Makes site migrations to other domains far easier. Ofc, if you access your site using a folder (eg. "http://<domain>/blog") you could change them to:

define('WP_HOME', '/blog/');
define('WP_SITEURL', '/blog/');

For existing sites, make sure the database and any theme/plugin files are free from absolute urls generate by wordpress using the old WP_HOME and WP_SITEURL values.

EDIT: just to clarify, you add these defines to your wp-config.php.

1
  • 1
    Shouldn't this be the accepted answer? Mar 9, 2018 at 16:53
9

If anyone out there has a similar situation as me, I found a solution by adding:

/* That's all, stop editing! Happy blogging. */
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
/* THIS IS CUSTOM CODE CREATED AT ZEROFRACTAL TO MAKE SITE ACCESS DYNAMIC */
$currenthost = "http://".$_SERVER['HTTP_HOST'];
$currentpath = preg_replace('@/+$@','',dirname($_SERVER['SCRIPT_NAME']));
$currentpath = preg_replace('/\/wp.+/','',$currentpath);
define('WP_HOME',$currenthost.$currentpath);
define('WP_SITEURL',$currenthost.$currentpath);
define('WP_CONTENT_URL', $currenthost.$currentpath.'/wp-content');
define('WP_PLUGIN_URL', $currenthost.$currentpath.'/wp-content/plugins');
define('DOMAIN_CURRENT_SITE', $currenthost.$currentpath );
@define('ADMIN_COOKIE_PATH', './');

In the wp-config.php I found this solution on the site: http://davidmregister.com/dynamic-wp-siteurl/

Thanks everyone!

1
  • Confirmed, works like a charm!
    – crusaderky
    Mar 18, 2017 at 18:01

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .