Title: WP_Filesystem_Base::search_for_folder
Published: April 25, 2014
Last modified: February 24, 2026

---

# WP_Filesystem_Base::search_for_folder( string $folder, string $base, bool $loop = false ): string|false

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#wp--skip-link--target)

Locates a folder on the remote filesystem.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#description)󠁿

Expects Windows sanitized path.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#parameters)󠁿

 `$folder`stringrequired

The folder to locate.

`$base`stringrequired

The folder to start searching from.

`$loop`booloptional

If the function has recursed. Internal use only.

Default:`false`

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#return)󠁿

 string|false The location of the remote path, false to cease looping.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#source)󠁿

    ```php
    public function search_for_folder( $folder, $base = '.', $loop = false ) {
    	if ( empty( $base ) || '.' === $base ) {
    		$base = trailingslashit( $this->cwd() );
    	}

    	$folder = untrailingslashit( $folder );

    	if ( $this->verbose ) {
    		/* translators: 1: Folder to locate, 2: Folder to start searching from. */
    		printf( "\n" . __( 'Looking for %1$s in %2$s' ) . "<br />\n", $folder, $base );
    	}

    	$folder_parts     = explode( '/', $folder );
    	$folder_part_keys = array_keys( $folder_parts );
    	$last_index       = array_pop( $folder_part_keys );
    	$last_path        = $folder_parts[ $last_index ];

    	$files = $this->dirlist( $base );

    	foreach ( $folder_parts as $index => $key ) {
    		if ( $index === $last_index ) {
    			continue; // We want this to be caught by the next code block.
    		}

    		/*
    		 * Working from /home/ to /user/ to /wordpress/ see if that file exists within
    		 * the current folder, If it's found, change into it and follow through looking
    		 * for it. If it can't find WordPress down that route, it'll continue onto the next
    		 * folder level, and see if that matches, and so on. If it reaches the end, and still
    		 * can't find it, it'll return false for the entire function.
    		 */
    		if ( isset( $files[ $key ] ) ) {

    			// Let's try that folder:
    			$newdir = trailingslashit( path_join( $base, $key ) );

    			if ( $this->verbose ) {
    				/* translators: %s: Directory name. */
    				printf( "\n" . __( 'Changing to %s' ) . "<br />\n", $newdir );
    			}

    			// Only search for the remaining path tokens in the directory, not the full path again.
    			$newfolder = implode( '/', array_slice( $folder_parts, $index + 1 ) );
    			$ret       = $this->search_for_folder( $newfolder, $newdir, $loop );

    			if ( $ret ) {
    				return $ret;
    			}
    		}
    	}

    	/*
    	 * Only check this as a last resort, to prevent locating the incorrect install.
    	 * All above procedures will fail quickly if this is the right branch to take.
    	 */
    	if ( isset( $files[ $last_path ] ) ) {
    		if ( $this->verbose ) {
    			/* translators: %s: Directory name. */
    			printf( "\n" . __( 'Found %s' ) . "<br />\n", $base . $last_path );
    		}

    		return trailingslashit( $base . $last_path );
    	}

    	/*
    	 * Prevent this function from looping again.
    	 * No need to proceed if we've just searched in `/`.
    	 */
    	if ( $loop || '/' === $base ) {
    		return false;
    	}

    	/*
    	 * As an extra last resort, Change back to / if the folder wasn't found.
    	 * This comes into effect when the CWD is /home/user/ but WP is at /var/www/....
    	 */
    	return $this->search_for_folder( $folder, '/', true );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/class-wp-filesystem-base.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/class-wp-filesystem-base.php#L259)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/class-wp-filesystem-base.php#L259-L336)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Filesystem_Base::dirlist()](https://developer.wordpress.org/reference/classes/wp_filesystem_base/dirlist/)`wp-admin/includes/class-wp-filesystem-base.php` |

Gets details for files in a directory or a specific file.

  | 
| [WP_Filesystem_Base::cwd()](https://developer.wordpress.org/reference/classes/wp_filesystem_base/cwd/)`wp-admin/includes/class-wp-filesystem-base.php` |

Gets the current working directory.

  | 
| [WP_Filesystem_Base::search_for_folder()](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/)`wp-admin/includes/class-wp-filesystem-base.php` |

Locates a folder on the remote filesystem.

  | 
| [untrailingslashit()](https://developer.wordpress.org/reference/functions/untrailingslashit/)`wp-includes/formatting.php` |

Removes trailing forward slashes and backslashes if they exist.

  | 
| [path_join()](https://developer.wordpress.org/reference/functions/path_join/)`wp-includes/functions.php` |

Joins two filesystem paths together.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [trailingslashit()](https://developer.wordpress.org/reference/functions/trailingslashit/)`wp-includes/formatting.php` |

Appends a trailing slash.

  |

[Show 2 more](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#)

| Used by | Description | 
| [WP_Filesystem_Base::find_folder()](https://developer.wordpress.org/reference/classes/wp_filesystem_base/find_folder/)`wp-admin/includes/class-wp-filesystem-base.php` |

Locates a folder on the remote filesystem.

  | 
| [WP_Filesystem_Base::search_for_folder()](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/)`wp-admin/includes/class-wp-filesystem-base.php` |

Locates a folder on the remote filesystem.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_filesystem_base/search_for_folder/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.7.0](https://developer.wordpress.org/reference/since/2.7.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_filesystem_base%2Fsearch_for_folder%2F)
before being able to contribute a note or feedback.