Sometimes when developing my theme my WordPress website will be on a obscure domain such as http://localhost:8080/project/foo/bar.

Is there a WordPress API function (or maybe just a PHP function) to get the root folder without the domain? So I am looking for a function that will give me /project/foo/bar without the http://localhost:8080 part.

2 Answers 2


PHP stores this info in $_SERVER['REQUEST_URI'].

  • Note that $_SERVER values can be spoofed, so is not always reliable. Apr 14, 2018 at 17:37

WP have get_home_path function which can provide you the o/p you're looking for but if its multinetwork site with subdirectory then it will provide you the subdir in the path. Also, you need to include the dependency file in order to access this function.

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

$root_dir = get_home_path();

ref: https://codex.wordpress.org/Function_Reference/get_home_path

And if you need the root dir without any subdir in path you can use ABSPATH constant. http://example.com => /var/www/example.com/htdocs/

ref: What's the difference between get_home_path() and ABSPATH?

Just for ref, WP has some other functions and constant which you can use as per your need. https://codex.wordpress.org/Determining_Plugin_and_Content_Directories

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.