Here is the php function to determine wordpress base path in wordpress

This function checks in each directory level starting from the directory of the current file for the fileĀ wp-config.php. If it is found the directory is to be assumed the wordpress base path. The check can of course be changed to other wordpress core files.



function findWpBasePath() 
{
    $dir = dirname(__FILE__);
    do {
        //it is possible to check for other files here
        if( file_exists($dir."/wp-config.php") ) {
            return $dir;
        }
    } while( $dir = realpath("$dir/..") );
    return null;
}

$base_path = findWpBasePath()  . "/" ;

Leave a Comment

Your email address will not be published. Required fields are marked *