pre_attachment_url_to_postid
云策文档标注
概述
pre_attachment_url_to_postid 是一个 WordPress 过滤器钩子,允许插件在查找附件 ID 时进行短路处理。插件可以通过返回特定值来控制查找流程。
关键要点
- 该钩子用于在 attachment_url_to_postid() 函数执行前拦截附件 ID 查找过程。
- 插件应返回整数 0 表示未找到附件,返回整数附件 ID 表示找到,或返回 null 让 WordPress 继续查找。
- 注意:post ID 可能为 null 或 0,两者在布尔上下文中都视为 false,开发时应使用 === 运算符进行严格比较。
代码示例
$post_id = apply_filters( 'pre_attachment_url_to_postid', null, $url );注意事项
参数 $post_id 为整数或 null,表示查找结果;$url 为字符串,表示被查找的 URL。该钩子自 WordPress 6.7.0 版本引入。
原文内容
Filters the attachment ID to allow short-circuit the function.
Description
Allows plugins to short-circuit attachment ID lookups. Plugins making use of this function should return:
- 0 (integer) to indicate the attachment is not found,
- attachment ID (integer) to indicate the attachment ID found,
- null to indicate WordPress should proceed with the lookup.
Warning: The post ID may be null or zero, both of which cast to a boolean false. For information about casting to booleans see the PHP documentation.
Use the === operator for testing the post ID when developing filters using this hook.
Parameters
$post_idint|null-
The result of the post ID lookup. Null to indicate no lookup has been attempted. Default null.
$urlstring-
The URL being looked up.
Source
$post_id = apply_filters( 'pre_attachment_url_to_postid', null, $url );
Changelog
| Version | Description |
|---|---|
| 6.7.0 | Introduced. |