get_home_path()
Gets the absolute filesystem path to the root of the WordPress installation.
No Hooks.
Return
String
. Full filesystem path to the root of the WordPress installation.
Usage
get_home_path();
Examples
#1 Demo
Suppose we have the WP core installed in the wp
sub-folder :
Admin-panel:
echo get_home_path(); echo ABSPATH; echo $_SERVER['SCRIPT_FILENAME']; echo $_SERVER['DOCUMENT_ROOT']; /* path/sites/site.com/www/ path/sites/site.com/www/wp/ path/sites/site.com/www/wp/wp-admin/options-permalink.php path/sites/site.com/www */
Frontpage:
It’s not available on the frontpage, it’s available only from wp-admin (backend). You should check if is_admin() otherwise you’ll get “Fatal error: Uncaught Error: Call to undefined function get_home_path()“.
// make sure that get_home_path() is defined require_once ABSPATH . 'wp-admin/includes/file.php'; echo get_home_path(); echo ABSPATH; echo $_SERVER['SCRIPT_FILENAME']; echo $_SERVER['DOCUMENT_ROOT']; /* / path/sites/site.com/www/wp/ path/sites/site.com/www/index.php path/sites/site.com/www */
On the front, the function does not work correctly because it relies on the difference between the options home
and siteurl
. In this case, it is (string) wp/
. Then from SCRIPT_FILENAME
everything from the beginning to this position is cut out - this is what is returned by the function with the slash added to the end. And since at the front SCRIPT_FILENAME
contains no substring wp/
at all, the path is empty - it cannot be defined.
It looks like a bug.
#2 Get the path to the main folder of the site
This folder usually contains file wp-config.php
and .htaccess
. Let's say we need to change the file .htaccess
:
require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/misc.php'; $home_path = get_home_path(); $htaccess_file = $home_path .'.htaccess'; /* * If the file doesn't already exist check for write access to the directory * and whether we have some rules. Else check for write access to the file. */ if ( ( ! file_exists($htaccess_file) && is_writable($home_path) ) || is_writable( $htaccess_file ) ) { $rules = ' RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] '; $rules = array_filter( explode( "\n", $rules ) ); $done = insert_with_markers( $htaccess_file, 'My Custom Rules', $rules ); } else { echo '$htaccess_file does not exist or is closed for posting.'; }
Changelog
Since 1.5.0 | Introduced. |
get_home_path() get home path code WP 6.7.2
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 ); }