wppaste
WordPress

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:2157
Initializes and connects the WordPress Filesystem Abstraction classes.

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

Reads or writes the filesystem via file_exists().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
15–84

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 88.

Plugin surface
1 hook

Third-party callbacks on 'filesystem_method_file' run inside this call, and their cost is not bounded by anything here.

Called by
10

10 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessfile_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.

WhenInstructionsCalls it makes
always15get_filesystem_method()
!file_exists()32get_filesystem_method(), apply_filters(), file_exists()
!is_wp_error()38–44get_filesystem_method(), is_wp_error(), ->connect()
is_wp_error() && ->has_errors()39–41get_filesystem_method(), is_wp_error(), ->has_errors()
is_wp_error() && !->has_errors()42–48get_filesystem_method(), is_wp_error(), ->has_errors(), ->connect()
!is_wp_error() && ->connect()52–55get_filesystem_method(), is_wp_error(), ->connect(), fileperms(), define()
file_exists() && !is_wp_error()53–59get_filesystem_method(), apply_filters(), file_exists(), is_wp_error(), ->connect()
file_exists() && is_wp_error() && ->has_errors()54–56get_filesystem_method(), apply_filters(), file_exists(), is_wp_error(), ->has_errors()
is_wp_error() && !->has_errors() && ->connect()56–59get_filesystem_method(), is_wp_error(), ->has_errors(), ->connect(), fileperms(), define()
file_exists() && is_wp_error() && !->has_errors()57–63get_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–65get_filesystem_method(), is_wp_error(), ->connect(), fileperms(), define(), fileperms(), define()
file_exists() && !is_wp_error() && ->connect()67–70get_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–69get_filesystem_method(), is_wp_error(), ->has_errors(), ->connect(), fileperms(), define(), fileperms(), define()
file_exists() && is_wp_error() && !->has_errors() && ->connect()71–74get_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–80get_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–84get_filesystem_method(), apply_filters(), file_exists(), is_wp_error(), ->has_errors(), ->connect(), fileperms(), define(), fileperms(), define()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8815–8410
8.58815–8410
8.48815–84102 fewer instructions than PHP 8.3
8.39015–8610
8.29015–8610
8.19015–8610
7.49015–8610

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:

  1. apply_filters( filesystem_method_file )filterline 2180 (+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

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 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.