[wp-trac] [WordPress Trac] #52575: get_home_path() returns "/" instead of path to WordPress directory

WordPress Trac noreply at wordpress.org
Sat Jul 15 15:16:42 UTC 2023


#52575: get_home_path() returns "/" instead of path to WordPress directory
--------------------------------------------+------------------------------
 Reporter:  pixellogik                      |       Owner:  (none)
     Type:  defect (bug)                    |      Status:  new
 Priority:  normal                          |   Milestone:  Awaiting Review
Component:  Filesystem API                  |     Version:  5.6.1
 Severity:  major                           |  Resolution:
 Keywords:  reporter-feedback dev-feedback  |     Focuses:
--------------------------------------------+------------------------------

Comment (by arnolp):

 @peterwilsoncc;
 Sorry to say I think you've overlooked the issue :
 The linked junit tests only test the page "<server path>/wp/wp-admin
 /options-permalink.php", where it works fine.

 However on front-end pages, $_SERVER['SCRIPT_FILENAME'] will be <server
 path>/index.php , so will never contains relative path to wp install (/wp
 in your example), so get_home_path will always be "/",
 which triggers a lot of warning  by incorrectly reading the root of server

 Here is the get_home_path wpcore php code:

 {{{#!php
 <?php
 function get_home_path() {
         $home    = set_url_scheme( get_option( 'home' ), 'http' ); // --->
 example.com
         $siteurl = set_url_scheme( get_option( 'siteurl' ), 'http' ); //
 ---> example.com/wp

         if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) {
 // ---> true, always, when home and siteurl are different

                 $wp_path_rel_to_home = str_ireplace( $home, '', $siteurl
 ); // ---> '/wp'
                 $pos                 = strripos( str_replace( '\\', '/',
 $_SERVER['SCRIPT_FILENAME'] ), trailingslashit( $wp_path_rel_to_home ) );
                 //-- >will always return 0, relative path '/wp' is NOT in
 server script filename '/www/<myusername>/public/index.php'


                 $home_path           = substr(
 $_SERVER['SCRIPT_FILENAME'], 0, $pos ); // ---> '/'
                 $home_path           = trailingslashit( $home_path ); //
 ---> '/'
         } else {
                 $home_path = ABSPATH;
         }

         return str_replace( '\\', '/', $home_path );
 }
 }}}

 I don't get why this code would ever work apart from admin pages context;

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/52575#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list