wp_delete_file()
云策文档标注
概述
wp_delete_file() 是 WordPress 核心函数,用于删除指定路径的文件。它通过应用过滤器 wp_delete_file 来允许修改文件路径,并返回删除操作的成功状态。
关键要点
- 函数接受一个必需参数 $file,表示要删除的文件路径字符串。
- 返回布尔值:成功时返回 true,失败时返回 false。
- 包含一个过滤器 wp_delete_file,允许在删除前修改文件路径。
- 内部使用 @unlink() 函数执行实际删除操作。
- 自 WordPress 4.2.0 引入,6.7.0 版本添加了返回值。
代码示例
$file_path = "/home/public_html/wp-content/uploads/2020/04/photo_img_1-8.png"; // 需要删除的文件路径
wp_delete_file( $file_path ); // 删除文件
原文内容
Deletes a file.
Parameters
$filestringrequired-
The path to the file to delete.
Source
function wp_delete_file( $file ) {
/**
* Filters the path of the file to delete.
*
* @since 2.1.0
*
* @param string $file Path to the file to delete.
*/
$delete = apply_filters( 'wp_delete_file', $file );
if ( ! empty( $delete ) ) {
return @unlink( $delete );
}
return false;
}
Hooks
- apply_filters( ‘wp_delete_file’, string $file )
-
Filters the path of the file to delete.
Skip to note 2 content
Mohit Mishra
$file_path = "/home/public_html/wp-content/uploads/2020/04/photo_img_1-8.png" // path of the file which need to be deleted. wp_delete_file( $file_path ); //delete file here.