WP_CLI
Runner::find_wp_root
Find the directory that contains the WordPress files. Defaults to the current working dir.
Method of the class: Runner{}
No Hooks.
Returns
String. An absolute path.
Usage
$Runner = new Runner(); $Runner->find_wp_root();
Runner::find_wp_root() Runner::find wp root code WP-CLI 2.13.0-alpha
public function find_wp_root() {
if ( isset( $this->config['path'] ) &&
( is_bool( $this->config['path'] ) || empty( $this->config['path'] ) )
) {
WP_CLI::error( 'The --path parameter cannot be empty when provided.' );
}
if ( ! empty( $this->config['path'] ) ) {
$path = $this->config['path'];
if ( ! Utils\is_path_absolute( $path ) ) {
$path = getcwd() . '/' . $path;
}
return $path;
}
if ( $this->cmd_starts_with( [ 'core', 'download' ] ) ) {
return getcwd();
}
$dir = getcwd();
while ( is_readable( $dir ) ) {
if ( file_exists( "$dir/wp-load.php" ) ) {
return $dir;
}
if ( file_exists( "$dir/index.php" ) ) {
$path = self::extract_subdir_path( "$dir/index.php" );
if ( ! empty( $path ) ) {
return $path;
}
}
$parent_dir = dirname( $dir );
if ( empty( $parent_dir ) || $parent_dir === $dir ) {
break;
}
$dir = $parent_dir;
}
return getcwd();
}