wp_unique_filename
云策文档标注
概述
wp_unique_filename 是一个 WordPress 过滤器钩子,用于在生成唯一文件名时修改结果。它允许开发者自定义文件名生成逻辑,例如添加前缀或后缀。
关键要点
- 过滤器钩子名称为 wp_unique_filename,用于过滤生成唯一文件名的结果。
- 参数包括 $filename(原始文件名)、$ext(文件扩展名)、$dir(目录路径)、$unique_filename_callback(回调函数)、$alt_filenames(替代文件名数组)和 $number(用于唯一化的最高数字)。
- 从 WordPress 5.8.1 版本开始,新增了 $alt_filenames 和 $number 参数,增强了冲突检查功能。
- 相关函数 wp_unique_filename() 用于获取经过清理且在给定目录中唯一的文件名。
注意事项
使用此过滤器时,应确保回调函数正确处理文件名冲突,避免覆盖现有文件。参数 $alt_filenames 和 $number 仅在 WordPress 5.8.1 及以上版本中可用。
原文内容
Filters the result when generating a unique file name.
Parameters
$filenamestring-
Unique file name.
$extstring-
File extension. Example: “.png”.
$dirstring-
Directory path.
$unique_filename_callbackcallable|null-
Callback function that generates the unique file name.
$alt_filenamesstring[]-
Array of alternate file names that were checked for collisions.
$numberint|string-
The highest number that was used to make the file name unique or an empty string if unused.
Source
return apply_filters( 'wp_unique_filename', $filename, $ext, $dir, $unique_filename_callback, $alt_filenames, $number );