WP_Filesystem()
云策文档标注
概述
WP_Filesystem() 函数用于初始化和连接 WordPress 文件系统抽象类,提供统一的文件操作接口。它根据环境自动选择传输方法(如 direct、FTP),并支持插件通过过滤器扩展。
关键要点
- 函数初始化全局变量 $wp_filesystem,加载并实例化相应的 WP_Filesystem_* 类(如 WP_Filesystem_Direct)。
- 参数包括 $args(连接参数)、$context(上下文路径)和 $allow_relaxed_file_ownership(放宽文件所有权),默认使用 "direct" 方法。
- 返回值为 bool|null:成功返回 true,失败返回 false,若类文件不存在则返回 null。
- 初始化时机受限:不能在任意位置调用,最早可在 admin_init 钩子后使用,因为 request_filesystem_credentials() 仅在管理区域可用。
- 支持过滤器 filesystem_method_file 允许插件自定义传输方法类文件路径。
- 自动定义文件系统超时常量(如 FS_CONNECT_TIMEOUT)和权限常量(如 FS_CHMOD_DIR)。
代码示例
global $wp_filesystem;
require_once( ABSPATH . '/wp-admin/includes/file.php' );
if ( WP_Filesystem() ) {
// 使用 $wp_filesystem 进行文件操作,例如检查文件存在性
if ( $wp_filesystem->exists( $file_path ) ) {
$content = $wp_filesystem->get_contents( $file_path );
}
} else {
// 处理初始化失败情况
}注意事项
- 调用前需确保已包含 wp-admin/includes/file.php 文件。
- 务必检查 WP_Filesystem() 的返回值,避免在未初始化时使用 $wp_filesystem 对象。
- 在非管理区域使用时,需注意凭证请求限制,可能需要用户交互。
- 参考官方 Filesystem API 文档以获取正确用法,避免示例中的错误(如未检查返回值)。
原文内容
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.
Parameters
$argsarray|falseoptional-
Connection args, These are passed directly to the
WP_Filesystem_*()classes.
Default:
false $contextstring|falseoptional-
Context for get_filesystem_method() .
More Arguments from get_filesystem_method( … $context )
Full path to the directory that is tested for being writable. Default empty.
Default:
false $allow_relaxed_file_ownershipbooloptional-
Whether to allow Group/World writable.
Default:
false
Source
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;
}
Hooks
- apply_filters( ‘filesystem_method_file’, string $path, string $method )
-
Filters the path for a specific filesystem method class file.
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
Skip to note 2 content
Mahdi Yazdani
/** * 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; }