Title: WP_Filesystem
Published: April 25, 2014
Last modified: August 20, 2026

---

# WP_Filesystem( array|false $args = false, string|false $context = false, bool $allow_relaxed_file_ownership = false ): bool|null

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#user-contributed-notes)

Initializes and connects the WordPress Filesystem Abstraction classes.

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

This function will include the chosen transport and attempt connecting.

Plugins may add extra transports, And force WordPress to use them by returning the
filename via the [‘filesystem_method_file’](https://developer.wordpress.org/reference/hooks/filesystem_method_file/)
filter.

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

 `$args`array|falseoptional

Connection args, These are passed directly to the `WP_Filesystem_*()` classes.

Default:`false`

`$context`string|falseoptional

Context for [get_filesystem_method()](https://developer.wordpress.org/reference/functions/get_filesystem_method/).

More Arguments from get_filesystem_method( … $context )

Full path to the directory that is tested for being writable.

Default:`false`

`$allow_relaxed_file_ownership`booloptional

Whether to allow Group/World writable.

Default:`false`

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

 bool|null True on success, false on failure, null if the filesystem method class
file does not exist.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#more-information)󠁿

If no parameters are specified, the “direct” method is used. The method is determined
using the [get_filesystem_method()](https://developer.wordpress.org/reference/functions/get_filesystem_method/)
function.

One of the initial challenges for developers using the WP Filesystem API is you 
cannot initialize it just anywhere. The [`request_filesystem_credentials()`](https://developer.wordpress.org/reference/functions/request_filesystem_credentials/)
function isn’t available until AFTER the `wp_loaded` action hook, and is only included
in the admin area. One of the earliest hooks you can utilize is `admin_init`.

Filesystem API on Common APIs Handbook:
 [https://developer.wordpress.org/apis/handbook/filesystem/](https://developer.wordpress.org/apis/handbook/filesystem/)

Filesystem API Class Reference:
 Class: [WP_Filesystem_Base](https://developer.wordpress.org/reference/classes/wp_filesystem_base/)
Class: [WP_Filesystem_Direct](https://developer.wordpress.org/reference/classes/wp_filesystem_direct/)
Class: [WP_Filesystem_FTPext](https://developer.wordpress.org/reference/classes/wp_filesystem_ftpext/)
Class: [WP_Filesystem_ftpsocket](https://developer.wordpress.org/reference/classes/wp_filesystem_ftpsockets/)
Class: [WP_Filesystem_SSH2](https://developer.wordpress.org/reference/classes/wp_filesystem_ssh2/)

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

    ```php
    function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
    	global $wp_filesystem;

    	require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';

    	$method = get_filesystem_method( $args, $context, $allow_relaxed_file_ownership );

    	if ( ! $method ) {
    		return false;
    	}

    	if ( ! class_exists( "WP_Filesystem_$method" ) ) {

    		/**
    		 * Filters the path for a specific filesystem method class file.
    		 *
    		 * @since 2.6.0
    		 *
    		 * @see get_filesystem_method()
    		 *
    		 * @param string $path   Path to the specific filesystem method class file.
    		 * @param string $method The filesystem method to use.
    		 */
    		$abstraction_file = apply_filters( 'filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method );

    		if ( ! file_exists( $abstraction_file ) ) {
    			return null;
    		}

    		require_once $abstraction_file;
    	}
    	$method = "WP_Filesystem_$method";

    	$wp_filesystem = new $method( $args );

    	/*
    	 * Define the timeouts for the connections. Only available after the constructor is called
    	 * to allow for per-transport overriding of the default.
    	 */
    	if ( ! defined( 'FS_CONNECT_TIMEOUT' ) ) {
    		define( 'FS_CONNECT_TIMEOUT', 30 ); // 30 seconds.
    	}
    	if ( ! defined( 'FS_TIMEOUT' ) ) {
    		define( 'FS_TIMEOUT', 30 ); // 30 seconds.
    	}

    	if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
    		return false;
    	}

    	if ( ! $wp_filesystem->connect() ) {
    		return false; // There was an error connecting to the server.
    	}

    	// Set the permission constants if not already set.
    	if ( ! defined( 'FS_CHMOD_DIR' ) ) {
    		define( 'FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) );
    	}
    	if ( ! defined( 'FS_CHMOD_FILE' ) ) {
    		define( 'FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) );
    	}

    	return true;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/file.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-admin/includes/file.php#L2180)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-admin/includes/file.php#L2180-L2243)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#hooks)󠁿

 [apply_filters( ‘filesystem_method_file’, string $path, string $method )](https://developer.wordpress.org/reference/hooks/filesystem_method_file/)

Filters the path for a specific filesystem method class file.

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

| Uses | Description | 
| [get_filesystem_method()](https://developer.wordpress.org/reference/functions/get_filesystem_method/)`wp-admin/includes/file.php` |

Determines which method to use for reading, writing, modifying, or deleting files on the filesystem.

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

Calls the callback functions that have been added to a filter hook.

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

Checks whether the given variable is a WordPress Error.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#)

| Used by | Description | 
| [_wp_delete_all_temp_backups()](https://developer.wordpress.org/reference/functions/_wp_delete_all_temp_backups/)`wp-includes/update.php` |

Deletes all contents in the temporary backup directory.

  | 
| [WP_Site_Health::get_test_update_temp_backup_writable()](https://developer.wordpress.org/reference/classes/wp_site_health/get_test_update_temp_backup_writable/)`wp-admin/includes/class-wp-site-health.php` |

Tests if plugin and theme temporary backup directories are writable or can be created.

  | 
| [WP_Site_Health_Auto_Updates::test_all_files_writable()](https://developer.wordpress.org/reference/classes/wp_site_health_auto_updates/test_all_files_writable/)`wp-admin/includes/class-wp-site-health-auto-updates.php` |

Checks if core files are writable by the web user/group.

  | 
| [wp_ajax_delete_plugin()](https://developer.wordpress.org/reference/functions/wp_ajax_delete_plugin/)`wp-admin/includes/ajax-actions.php` |

Handles deleting a plugin via AJAX.

  | 
| [wp_ajax_delete_theme()](https://developer.wordpress.org/reference/functions/wp_ajax_delete_theme/)`wp-admin/includes/ajax-actions.php` |

Handles deleting a theme via AJAX.

  | 
| [WP_Upgrader::fs_connect()](https://developer.wordpress.org/reference/classes/wp_upgrader/fs_connect/)`wp-admin/includes/class-wp-upgrader.php` |

Connects to the filesystem.

  | 
| [WP_Upgrader::maintenance_mode()](https://developer.wordpress.org/reference/classes/wp_upgrader/maintenance_mode/)`wp-admin/includes/class-wp-upgrader.php` |

Toggles maintenance mode for the site.

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

Removes a theme.

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

Removes directory and files of a plugin for a list of plugins.

  | 
| [do_core_upgrade()](https://developer.wordpress.org/reference/functions/do_core_upgrade/)`wp-admin/update-core.php` |

Upgrades WordPress core display.

  |

[Show 5 more](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#)

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

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

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/wp_filesystem/?output_format=md#comment-content-2592)
 2.   [Mahdi Yazdani](https://profiles.wordpress.org/mahdiyazdani/)  [  9 years ago  ](https://developer.wordpress.org/reference/functions/wp_filesystem/#comment-2592)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_filesystem%2F%23comment-2592)
    Vote results for this note: 5[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_filesystem%2F%23comment-2592)
 4.     ```php
        /**
         * This method gets a list of the most popular Google web fonts. 
         * Initialize the WP filesystem and no more using file_put_contents function
         *
         * @see 	wp_filesystem()
         * @see 	get_parent_theme_file_path()
         * @return 	array 	A list of Google web fonts
         */
        function prefix_get_google_fonts() {
    
        	global $wp_filesystem;
    
        	require_once ( ABSPATH . '/wp-admin/includes/file.php' );
        	WP_Filesystem();
    
        	$local_file = 	get_parent_theme_file_path( '/assets/json/google-web-fonts.json' );
        	$content 	= 	'';
    
            if ( $wp_filesystem->exists( $local_file ) ) {
                $content = json_decode( $wp_filesystem->get_contents( $local_file ) );
            } // End If Statement
    
        	return $content;
    
        }
        ```
    
 5.  * This example is wrong; do not copy it. See a correct example at [https://codex.wordpress.org/Filesystem_API](https://codex.wordpress.org/Filesystem_API).
       Specifically, this example fails to check the return value of [WP_Filesystem()](https://developer.wordpress.org/reference/functions/wp_filesystem/),
       and contains no provision for requesting user credentials.
     * [David Anderson / Team Updraft](https://profiles.wordpress.org/davidanderson/)
       [6 years ago](https://developer.wordpress.org/reference/functions/wp_filesystem/#comment-4210)
 6.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_filesystem%2F%3Freplytocom%3D2592%23feedback-editor-2592)

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