WP_Filesystem()
Initializes and connects the WordPress Filesystem Abstraction classes.
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.
1 time — 0.001446 sec (very slow) | 50000 times — 4.11 sec (fast)
Hooks from the function
Return
true|false|null
. True on success, false on failure, null if the filesystem method class file does not exist.
Usage
WP_Filesystem( $args, $context, $allow_relaxed_file_ownership );
- $args(array|false)
- Connection args, These are passed directly to the WP_Filesystem_*() classes.
Default: false - $context(string|false)
- Context for get_filesystem_method().
Default: false - $allow_relaxed_file_ownership(true|false)
- Whether to allow Group/World writable.
Default: false
Examples
#1 Using the wp_filesystem
global $wp_filesystem; // create a file interaction object, if it is not already created if( ! $wp_filesystem ){ require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); } // use the object echo $wp_filesystem->abspath(); // /home/www/example.com/public_html/ $wp_filesystem->delete( $maintenance_file ); $wp_filesystem->put_contents( $maintenance_file, $maintenance_string ); // etc.
A list of useful methods:
- abspath( ) — Returns the path on the remote filesystem of ABSPATH.
- copy( $source, $destination, $overwrite, $mode ) — Copies a file.
- cwd( ) — Gets the current working directory.
- delete( $file, $recursive, $type ) — Deletes a file or directory.
- dirlist( $path, $include_hidden, $recursive ) — Gets details for files in a directory or a specific file.
- exists( $path ) — Checks if a file or directory exists.
- get_contents( $file ) — Reads entire file into a string.
- get_contents_array( $file ) — Reads entire file into an array.
- is_binary( $text ) — Determines if the string provided contains binary characters.
- is_dir( $path ) — Checks if resource is a directory.
- is_file( $file ) — Checks if resource is a file.
- is_readable( $file ) — Checks if a file is readable.
- is_writable( $path ) — Checks if a file or directory is writable.
- mkdir( $path, $chmod, $chown, $chgrp ) — Creates a directory.
- move( $source, $destination, $overwrite ) — Moves a file.
- mtime( $file ) — Gets the file modification time.
- put_contents( $file, $contents, $mode ) — Writes a string to a file.
- rmdir( $path, $recursive ) — Deletes a directory.
- search_for_folder( $folder, $base, $loop ) — Locates a folder on the remote filesystem.
- size( $file ) — Gets the file size (in bytes).
- touch( $file, $time, $atime ) — Sets the access and modification times of a file.
- wp_content_dir( ) — Returns the path on the remote filesystem of WP_CONTENT_DIR.
- wp_lang_dir( ) — Returns the path on the remote filesystem of WP_LANG_DIR.
- wp_plugins_dir( ) — Returns the path on the remote filesystem of WP_PLUGIN_DIR.
- wp_themes_dir( $theme ) — Returns the path on the remote filesystem of the Themes Directory.
#2 Each call creates a new instance
Each call to WP_Filesystem() overwrites the variable global $wp_filesystem;
. So it may be a good idea to check if the object has already been created before calling this function.
require_once ABSPATH . 'wp-admin/includes/file.php'; global $wp_filesystem; // base init WP_Filesystem(); echo get_class( $wp_filesystem ); // WP_Filesystem_Direct // call one more time define( 'FS_METHOD', 'ssh2' ); WP_Filesystem(); echo get_class( $wp_filesystem ); // WP_Filesystem_SSH2
Notes
- Global. WP_Filesystem_Base. $wp_filesystem WordPress filesystem subclass.
Changelog
Since 2.5.0 | Introduced. |