Skip to main content
changed the solution to be able to work in the wordpress base path
Source Link
xaedes
  • 1.3k
  • 1
  • 11
  • 14

It is checkedI came up with this solution.

This function checks in each directory level starting from the directory of the current file for the file wp-config.php. ThisIf 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 find_wordpress_base_path() {
    $dir = dirname(__FILE__);
    while( $dir = realpath("$dir/..") )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;
}

It is checked for the wp-config.php. This can of course be changed to other wordpress core files.

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

I came up with this solution.

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 find_wordpress_base_path() {
    $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;
}
Source Link
xaedes
  • 1.3k
  • 1
  • 11
  • 14

It is checked for the wp-config.php. This can of course be changed to other wordpress core files.

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