How to get DOCUMENT_ROOT to work on both localhost and web host? How to get DOCUMENT_ROOT to work on both localhost and web host? wordpress wordpress

How to get DOCUMENT_ROOT to work on both localhost and web host?


So it looks like the OP would be interested in either the WP_CONTENT_DIR or WP_PLUGIN_DIR predefined constants.

This is for Wordpress installs. But for non-WP situations, I've found the following snippet to be useful:

$docroot = realpath((getenv('DOCUMENT_ROOT') && ereg('^'.preg_quote(realpath(getenv('DOCUMENT_ROOT'))), realpath(__FILE__))) ? getenv('DOCUMENT_ROOT') : str_replace(dirname(@$_SERVER['PHP_SELF']), '', str_replace(DIRECTORY_SEPARATOR, '/', dirname(__FILE__))));

This is in similar situations where I've wanted things to work on both a localhost/dev environment and a server environment, where the docroot locations differed. BUT even more important is that this works in the situations where I need to run a PHP file standalone.