钩子文档

{$field}

💡 云策文档标注

概述

本文档介绍了一个用于过滤特定文章字段显示值的 Hook,仅适用于以 post_ 为前缀的字段。该 Hook 允许开发者在显示文章字段时修改其值,支持多种上下文以适配不同场景。

关键要点

  • Hook 名称动态部分 $field 对应文章字段名,如 post_author、post_title 等。
  • 参数包括 $value(字段值)、$post_id(文章 ID)和 $context(上下文,如 'display'、'edit' 等)。
  • 主要用于 sanitize_post_field() 等函数,确保字段值在不同上下文中安全显示。

代码示例

$value = apply_filters( "{$field}", $value, $post_id, $context );

注意事项

  • 仅适用于以 post_ 为前缀的文章字段,非此类字段无效。
  • 上下文参数 $context 影响字段的清理方式,需根据实际使用场景选择。
  • 自 WordPress 2.3.0 版本引入,兼容性良好。

📄 原文内容

Filters the value of a specific post field for display.

Description

Only applied to post fields with a name which is prefixed with post_.

The dynamic portion of the hook name, $field, refers to the post field name. Possible filter names include:

  • post_author
  • post_date
  • post_date_gmt
  • post_content
  • post_title
  • post_excerpt
  • post_status
  • post_password
  • post_name
  • post_modified
  • post_modified_gmt
  • post_content_filtered
  • post_parent
  • post_type
  • post_mime_type

Parameters

$valuemixed
Value of the prefixed post field.
$post_idint
Post ID.
$contextstring
Context for how to sanitize the field.
Accepts 'raw', 'edit', 'db', 'display', 'attribute', or 'js'. Default 'display'.

Source

$value = apply_filters( "{$field}", $value, $post_id, $context );

Changelog

Version Description
2.3.0 Introduced.