Getting started with WordPressget_bloginfo()Enqueuing scriptsMaking network requests with HTTP APIEnqueuing Styleshome_url()Custom Post Typestemplate_includeThe Loop (main WordPress loop)AJAXThe $wpdb ObjectActions and Filterswp_get_current_user()Add/remove contact info for users with user_contactmethods filter hookCreating a custom templateCustomizer Hello WorldCustomizer Basics (Add Panel, Section, Setting, Control)The Admin Bar (aka "The Toolbar")Querying postsAlternating main loop (pre_get_posts filter)ShortcodeCreate a Post Programmaticallyget_template_part()Taxonomiesget_template_part()ShortcodesPost FormatsCustom exerpts with excerpt_length and excerpt_morePlugin developmentSecurity in WordPress - EscapingTemplate hierarchyRemove Version from Wordpress and StylesheetsChild Theme Basicsadd_action()get_template_part()Shortcode with attributeSidebarsSecurity in WordPress - SanitizationinitCreate Template for Custom Post TypeFunction: add_action()Add ShortcodeHow Can I integrate Markdown editor with Advance Custom Field's repeater Add-on.Installation and Configurationwp_get_current_user()WP-CronSecure your installationOptions APIFunction : wp_trim_words()WP_Query() LoopUpdate WordPress ManuallyThemesWP-CLIDebuggingadd_menu_page()add_submenu_page()get_option()get_permalink()get_the_category()the_title()get_the_title()add_editor_style()add_theme_support()WordPress Plugin creationRun WordPress local with XAMPPAdmin Dashboard WidgetsSite MigrationMeta BoxRemove Auto Line Breaks From Content and Excerptget_home_path()Wordpress theme and child-theme developmentREST API

get_home_path()

Other topics

Remarks:

Important difference between get_home_path() and ABSTPATH

Please keep in mind the difference between ABSPATH and get_home_path() if you have WordPress installed in a subfolder.

The get_home_path() function will always return a path without the subfolder:

This is how it differs from ABSPATH, which will return different values:

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

get_home_path() checks if the site_url and home_url differ, and removes the substring from the path. Otherwise it returns ABSPATH value:

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 );
}

Using it in your code

Calling get_home_path() must be done in a context where wp-admin/includes/file.php has already been included.

For example using get_home_path() within the admin_init hook is fine, but using it within the init is not and will result in a PHP fatal error:

Call to undefined function get_home_path()

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');

Usage

$path = get_home_path();

Return value:

string

Full filesystem path to the root of the WordPress installation, even if it's installed in a subfolder.

Example:

/var/www/htdocs/example

Parameters:

ParameterDetails
NoneThis function does not accept any parameters.

Contributors

Topic Id: 9699

Example Ids: 29915

This site is not affiliated with any of the contributors.