Access WordPress on localhost via IP fail on loading stylesheet or function, due to wrong get_template_directory_uri()

User avatar
axew3
w3all User
w3all User
Posts: 2883
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Access WordPress on localhost via IP fail on loading stylesheet or function, due to wrong get_template_directory_uri()

Post by axew3 »

Scenario:
WordPress located and installed on localhost, apache server 2.4.
Using any default WP template i experience this:
let say i need to test wordpress, accessing it via another device connected on network, that need so to point to an IP like this:
http://192.168.1.8/wordpress/
and not
http://localhost/root_integrations/wordpress/
WordPress will loop (i experience this on a Windows OS 8 test but may will be the same on others OS also, i will test as i can)
and i can't access it, let say via smartphone.
Reason:
WordPress fail on locate the right template stylesheet_uri() on file:
/wordpress/wp-content/themes/twentysixteen/functions.php
where lines:

Code: Select all

wp_enqueue_style( 'twentysixteen-style', get_stylesheet_uri() );
and:

Code: Select all

wp_enqueue_script( 'twentysixteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20160816', true );
you can see that if you change get_stylesheet_uri() with a custom test like this:

Code: Select all

wp_enqueue_style( 'twentysixteen-style', 'http://192.168.1.8/root_integrations/wordpress/wp-content/themes/twentysixteen/style.css' );
then all will work fine (apply the same also to second enqueue mentioned, or just comment out the second, and apply only to the first to test out that will work fine).

Resuming in two word the problem is:
WordPress fail to locate the correct template uri, if WP installed on localhost with localhost address, and is accessed not via localhost, but via IP address.
So maybe a patch for this could be applied on wp-includes/theme.php
function get_template_directory_uri()
if it is not an issue about how the server is configured ... or by the way should be patched to return the correct result in any case?
User avatar
axew3
w3all User
w3all User
Posts: 2883
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: Access WordPress on localhost via IP fail on loading stylesheet or function, due to wrong get_template_directory_uri

Post by axew3 »

to resolve, just change WordPress install url setting:
NOT something like
http://localhost/wordpress
BUT as value for the installed wordpress, where
WordPress Address (URL)
and
Site Address (URL)
USE something like:
http://192.168.1.8/wordpress
change
192.168.1.8
with your correct assigned IP.
Now all will work fine. And yes, may it can be also fixed by setting up the server in different way.
(i experience this on a Windows OS 8 test but may will be the same on others OS also, i will test as i can)
yes it will be the same, especially if the server will not be properly configured :) by the way this will resolve on fly in any case.
Post Reply