wp loaded javascript files – absolute path to relative one  [ 784 views ]

Goal: convert all the javascript file references to relative

The main problem appeared when I tried to check my blog from a virtual machine.
In the machine my local blog address started with an IP like this http://192.168.0.100/…
and the absolute links were like this: http://localhost/… – crash

To solve this problem I need a simple function to cut the starting from every absolute locations to convert them to relative ones.

function wp_make_link_relative_ex( $link ) {
  return str_replace ( 'http://localhost/blablabla/', '', $link );
}

May be on the production the replacement will be different!

I’ve put this function into file wp_includes/formatting.php around existing function wp_make_link_relative.

1. Correct the header javascript fetching function

in the wp_includes/class.wp-scripts.php file – find the following code:

$src variable contains the absolute path of the javascript file

...
  if ( $this->do_concat )
    $this->print_html .= "<script type='text/javascript' src='$src'></script>\n";
  else
    echo "<script type='text/javascript' src='$src'></script>\n";
...

and insert before this

  $src = wp_make_link_relative_ex($src);

This is done. All paths are relative now (only in the header section and only the js files)

So my blog working in the virtual machine now.

2. Update the hosts file in the virtual machine

This is the global solution:
…windows/system32/drivers/etc/hosts file, here is the magic.
Override the default localhost 127.0.0.1 to the host ip and that is all.
Put a line to the end of the file (change the ip to your host ip ofcourse):

...
192.168.1.255    localhost

To save the file just check this post: hosts – how can I save this

#sidebar a { color:#fff; } #sidebar ul ul li { color: #DEF585; } #sidebar h2 { color: #fff; } #sidebar ul p, #sidebar ul select { color: #BEDDBE; } #backfly { background: url(images/golfBallWallPaper.jpg) left bottom fixed repeat-x #65a51d; }