Here we introduce two easy solutions for retrieving WordPress Root Path using a PHP function and a constant: get_home_path WP function and ABSPATH constant.
In this tutorial, I introduced two examples of getting WordPress root Path in PHP. These simple codes will retrieve the absolute path to the WordPress home directory when needed.
Getting WP full home directory path is useful when referencing files in PHP.
For example, when you want to use require
, require_once
, include
, and include_once
in PHP, The get_home_path
function and the ABSPATH
constant can be useful for referencing a PHP file.
1- Get WordPress Root Directory Path with get_home_path Function
get_home_path
function retrieves the absolute path to the WordPress root directory.
Example of using the get_home_path
function:
//example: including update.php file from /wp-admin/ folder:
require_once( get_home_path() . 'wp-admin/update.php' );
The returned string is the absolute path to the WordPress home directory; for example, /var/www/html/
or /home/username/domain.com/public_html/
.
If you installed WordPress in a subfolder, it would be like /var/www/html/wordpress/
. It depends on your installation.
Note: the trailing slash
/
is included in the returning string by theget_home_path
function of WordPress.
2- Get WordPress Root Directory Path with ABSPATH Constant
There is a constant defined in the wp-config.php
file that contains the absolute path to the WordPress home.
ABSPATH
is the name of this constant that you can use everywhere you need the absolute path of WordPress.
Example of using ABSPATH contact:
$rootPath = ABSPATH;
//example: including update.php file from /wp-admin/ folder:
require_once( ABSPATH . 'wp-admin/update.php' );
Same as in the previous example, ABSPATH
returns the absolute path to the WordPress home directory.
Note: the trailing slash
/
is included in the returning string by theABSPATH
constant of WordPress.
For instance, this constant will return a string like /var/www/html/
or /home/username/domain.com/public_html
/. or the subfolder you’ve used to install WordPress.
Note: Do NOT confuse the WordPress root path with the WordPress home URL. Also you can get the plugin directory path in WordPress using
plugin_dir_path
function.
written by Mehdi Nazari about in WordPress WordPress Functions WordPress Plugin Development WordPress Theme Development
What do you think about "2 Simple Ways to Get WordPress Root Path"?