钩子文档

{$action}_prefilter

💡 云策文档标注

概述

本文档介绍了 {$action}_prefilter 钩子,用于在文件上传到 WordPress 前过滤其数据。这是一个动态钩子,$action 部分对应具体的上传操作,如 wp_handle_sideload_prefilter 或 wp_handle_upload_prefilter。

关键要点

  • 钩子名称:{$action}_prefilter,其中 $action 是动态部分,代表上传操作类型。
  • 主要用途:过滤文件上传前的数据,允许开发者修改文件信息。
  • 参数:$file 数组,包含文件原始名称、MIME 类型、临时路径、大小和错误代码等。
  • 相关函数:_wp_handle_upload() 用于处理 WordPress 中的 PHP 上传。
  • 版本历史:从 2.9.0 版本引入,4.0.0 版本转换为动态钩子。

代码示例

$file = apply_filters( "{$action}_prefilter", $file );

📄 原文内容

Filters the data for a file before it is uploaded to WordPress.

Description

The dynamic portion of the hook name, $action, refers to the post action.

Possible hook names include:

  • wp_handle_sideload_prefilter
  • wp_handle_upload_prefilter

Parameters

$filearray
Reference to a single element from $_FILES.

  • name string
    The original name of the file on the client machine.
  • type string
    The mime type of the file, if the browser provided this information.
  • tmp_name string
    The temporary filename of the file in which the uploaded file was stored on the server.
  • size int
    The size, in bytes, of the uploaded file.
  • error int
    The error code associated with this file upload.

Source

$file = apply_filters( "{$action}_prefilter", $file );

Changelog

Version Description
4.0.0 Converted to a dynamic hook with $action.
2.9.0 Introduced.