WP_Filesystem( array|false $args = false, string|false $context = false, bool $allow_relaxed_file_ownership = false ): bool|null
- Since
- 2.5.0
- Source
wp-admin/includes/file.php:2175
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' filter.
Compatibility
- WordPress
- since 2.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$argsarray|falseoptional- Connection args, These are passed directly to the
WP_Filesystem_*()classes.
Default false.Default:false $contextstring|falseoptional- Context for get_filesystem_method().
Default false.Default:false $allow_relaxed_file_ownershipbooloptional- Whether to allow Group/World writable.
Default false.Default:false
Return value
bool|null- True on success, false on failure, null if the filesystem method class file does not exist.
Performance profile
How much work a call to WP_Filesystem() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 15–84
- Plugin surface
- 1 hook
- Called by
- 10
Reads or writes the filesystem via file_exists().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 88.
Third-party callbacks on 'filesystem_method_file' run inside this call, and their cost is not bounded by anything here.
10 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - filesystemfilesystem access
file_exists()called directly
What one call costs · 16 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Filesystem() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 15 | get_filesystem_method() |
!file_exists() | 32 | get_filesystem_method(), apply_filters(), file_exists() |
!is_wp_error() | 38–44 | get_filesystem_method(), is_wp_error(), ->connect() |
is_wp_error() && ->has_errors() | 39–41 | get_filesystem_method(), is_wp_error(), ->has_errors() |
is_wp_error() && !->has_errors() | 42–48 | get_filesystem_method(), is_wp_error(), ->has_errors(), ->connect() |
!is_wp_error() && ->connect() | 52–55 | get_filesystem_method(), is_wp_error(), ->connect(), fileperms(), define() |
file_exists() && !is_wp_error() | 53–59 | get_filesystem_method(), apply_filters(), file_exists(), is_wp_error(), ->connect() |
file_exists() && is_wp_error() && ->has_errors() | 54–56 | get_filesystem_method(), apply_filters(), file_exists(), is_wp_error(), ->has_errors() |
is_wp_error() && !->has_errors() && ->connect() | 56–59 | get_filesystem_method(), is_wp_error(), ->has_errors(), ->connect(), fileperms(), define() |
file_exists() && is_wp_error() && !->has_errors() | 57–63 | get_filesystem_method(), apply_filters(), file_exists(), is_wp_error(), ->has_errors(), ->connect() |
!is_wp_error() && ->connect() && !defined('FS_CHMOD_DIR') && !defined('FS_CHMOD_FILE') | 63–65 | get_filesystem_method(), is_wp_error(), ->connect(), fileperms(), define(), fileperms(), define() |
file_exists() && !is_wp_error() && ->connect() | 67–70 | get_filesystem_method(), apply_filters(), file_exists(), is_wp_error(), ->connect(), fileperms(), define() |
4 further outcomes, up to 84 instructions
is_wp_error() && !->has_errors() && ->connect() && !defined('FS_CHMOD_DIR') && !defined('FS_CHMOD_FILE') | 67–69 | get_filesystem_method(), is_wp_error(), ->has_errors(), ->connect(), fileperms(), define(), fileperms(), define() |
file_exists() && is_wp_error() && !->has_errors() && ->connect() | 71–74 | get_filesystem_method(), apply_filters(), file_exists(), is_wp_error(), ->has_errors(), ->connect(), fileperms(), define() |
file_exists() && !is_wp_error() && ->connect() && !defined('FS_CHMOD_DIR') && !defined('FS_CHMOD_FILE') | 78–80 | get_filesystem_method(), apply_filters(), file_exists(), is_wp_error(), ->connect(), fileperms(), define(), fileperms(), define() |
file_exists() && is_wp_error() && !->has_errors() && ->connect() && !defined('FS_CHMOD_DIR') && !defined('FS_CHMOD_FILE') | 82–84 | get_filesystem_method(), apply_filters(), file_exists(), is_wp_error(), ->has_errors(), ->connect(), fileperms(), define(), fileperms(), define() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 88 | 15–84 | 10 | |
| 8.5 | 88 | 15–84 | 10 | |
| 8.4 | 88 | 15–84 | 10 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 90 | 15–86 | 10 | |
| 8.2 | 90 | 15–86 | 10 | |
| 8.1 | 90 | 15–86 | 10 | |
| 7.4 | 90 | 15–86 | 10 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while WP_Filesystem() runs, in this order:
- apply_filters( filesystem_method_file )filterline 2198 (+23 into the body)
Filters the path for a specific filesystem method class file.
Uses · 3
- get_filesystem_method()Determines which method to use for reading, writing, modifying, or deleting files on the filesystem.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_wp_error()Checks whether the given variable is a WordPress Error.
Used by · 10
- WP_Site_Health::get_test_update_temp_backup_writable()Tests if plugin and theme temporary backup directories are writable or can be created.
- WP_Site_Health_Auto_Updates::test_all_files_writable()Checks if core files are writable by the web user/group.
- WP_Upgrader::fs_connect()Connects to the filesystem.
- WP_Upgrader::maintenance_mode()Toggles maintenance mode for the site.
- _wp_delete_all_temp_backups()Deletes all contents in the temporary backup directory.
- delete_plugins()Removes directory and files of a plugin for a list of plugins.
- delete_theme()Removes a theme.
- do_core_upgrade()Upgrades WordPress core display.
- wp_ajax_delete_plugin()Handles deleting a plugin via AJAX.
- wp_ajax_delete_theme()Handles deleting a theme via AJAX.
Source code
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; } 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;}Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-admin/includes/file.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.