Wordpress - What's the difference between get_home_path() and ABSPATH?

They should do the same thing, but under certain conditions, may not.

First of all note:

  • that the codex entry description is misleading
  • that wp-admin/includes/file.php must be included in context else calling get_home_path() will lead to calling an undefined function.

Regarding the codex entry,

Description

Get the absolute filesystem path to the root of the WordPress installation.

Return Value

Full filesystem path to the root of the WordPress installation. If you install wordpress in subfolder, it will show subfolder location

Examples

$path = get_home_path();
print "Path: ".$path; // Return "Path: /var/www/htdocs/" or "Path: /var/www/htdocs/wordpress/" if it is subfolder

It states that the return value will return the path of the subfolder if you have installed WordPress in a sub-directory. This is in fact, incorrect.

get_home_path() will return the root directory of your WordPress installation, even if it's installed in a sub-directory. That is the purpose of the function.

Assume your WordPress installation is within a sub-directory called /dev,

  • http://www.example.com/dev (site_url) (e.g. /var/www/htdocs/dev)
  • http://www.example.com/ (home_url)

If you log a call to ABSPATH, then the result of which will be, /var/www/htdocs/dev which is not the root of your installation. The root of your installation is /var/www/htdocs.

ABSPATH is first defined in wp-load.php which will be located at /var/www/htdocs/dev/wp-load.php hence this is where ABSPATH will take its definition from.

If you inspect the get_home_path() further you will note that if the site_url and home_url differ, then a sub-string is taken of the path governed by the position (first occurance) of the sub-directory found within the string.

function get_home_path() {
    $home    = set_url_scheme( get_option( 'home' ), 'http' );
    $siteurl = set_url_scheme( get_option( 'siteurl' ), 'http' );

    if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) {
        $wp_path_rel_to_home = str_ireplace( $home, '', $siteurl ); /* $siteurl - $home */
        $pos = strripos( str_replace( '\\', '/', $_SERVER['SCRIPT_FILENAME'] ), trailingslashit( $wp_path_rel_to_home ) );
        $home_path = substr( $_SERVER['SCRIPT_FILENAME'], 0, $pos );
        $home_path = trailingslashit( $home_path );
    } else {
        $home_path = ABSPATH;
    }

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

Therefore, as a result of this, get_home_path() and ABSPATH may return different results if you have WordPress installed in a sub-directory.

Secondly, calling get_home_path() must be done in a context where the afortmentioned wp-admin/includes/file.php has already been included.

As an example using get_home_path() within the admin_init hook is fine where as using it within init is not.

Seeing as this file only gets included from within the admin (dashboard) context, if you absolutely need it outside of this context you will need to include the file yourself before calling the function,

require_once(ABSPATH . 'wp-admin/includes/file.php');

Ironically (or not) which uses ABSPATH :D

Tags:

Core

Paths