{"id":587,"date":"2014-06-13T23:28:06","date_gmt":"2014-06-13T23:28:06","guid":{"rendered":"http:\/\/localhost\/__sites\/3d_blog\/?p=587"},"modified":"2014-06-14T00:15:17","modified_gmt":"2014-06-14T00:15:17","slug":"wp-loaded-javascript-files-absolute-path-to-relative","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=587","title":{"rendered":"wp loaded javascript files &#8211; absolute path to relative one"},"content":{"rendered":"<blockquote><p>Goal: convert all the javascript file references to relative<\/p><\/blockquote>\n<p>The main problem appeared when I tried to check my blog from a virtual machine.<br \/>\nIn the machine my local blog address started with an IP like this http:\/\/192.168.0.100\/&#8230;<br \/>\nand the absolute links were like this: http:\/\/localhost\/&#8230; &#8211; crash<\/p>\n<p>To solve this problem I need a simple function to cut the starting from every absolute locations to convert them to relative ones.<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\nfunction wp_make_link_relative_ex( $link ) {\r\n  return str_replace ( 'http:\/\/localhost\/blablabla\/', '', $link );\r\n}\r\n<\/pre>\n<blockquote><p>May be on the production the replacement will be different!<\/p><\/blockquote>\n<p>I&#8217;ve put this function into file <strong>wp_includes\/formatting.php<\/strong> around existing function <strong>wp_make_link_relative<\/strong>.<\/p>\n<p><strong>1. Correct the header javascript fetching function<\/strong><\/p>\n<p>in the <strong>wp_includes\/class.wp-scripts.php<\/strong> file &#8211; find the following code:<\/p>\n<blockquote><p>$src variable contains the absolute path of the javascript file<\/p><\/blockquote>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\n...\r\n  if ( $this-&gt;do_concat )\r\n    $this-&gt;print_html .= &quot;&lt;script type='text\/javascript' src='$src'&gt;&lt;\/script&gt;\\n&quot;;\r\n  else\r\n    echo &quot;&lt;script type='text\/javascript' src='$src'&gt;&lt;\/script&gt;\\n&quot;;\r\n...\r\n<\/pre>\n<p>and insert before this<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\n  $src = wp_make_link_relative_ex($src);\r\n<\/pre>\n<p>This is done. All paths are relative now (only in the header section and only the js files)<\/p>\n<p>So my blog working in the virtual machine now.<\/p>\n<p><strong>2. Update the hosts file in the virtual machine<\/strong><\/p>\n<p>This is the global solution:<br \/>\n<strong>&#8230;windows\/system32\/drivers\/etc\/hosts<\/strong> file, here is the magic.<br \/>\nOverride the default localhost 127.0.0.1 to the host ip and that is all.<br \/>\nPut a line to the end of the file (change the ip to your host ip ofcourse):<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\n...\r\n192.168.1.255    localhost\r\n<\/pre>\n<p>To save the file just check this post: <a href=\"?p=585\" title=\"hosts \u2013 how can I save this\">hosts \u2013 how can I save this<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\/&#8230; and the absolute links were like this: http:\/\/localhost\/&#8230; &#8211; crash To solve this problem I need a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-587","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=587"}],"version-history":[{"count":14,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/587\/revisions"}],"predecessor-version":[{"id":606,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/587\/revisions\/606"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}