钩子文档

private_title_format

💡 云策文档标注

概述

private_title_format 是一个 WordPress 过滤器,用于修改私有文章标题前添加的文本。该过滤器仅在前端应用,允许开发者自定义私有文章标题的显示格式。

关键要点

  • 过滤器名称:private_title_format,用于过滤私有文章标题前的文本。
  • 应用范围:仅在前端生效,不影响后端管理界面。
  • 参数:$prepend(字符串,默认值为 'Private: %s')和 $post(WP_Post 对象)。
  • 返回值:应返回一个 sprintf 风格的格式字符串,例如 '%s' 表示仅显示原标题。
  • 注意事项:避免在返回的字符串中使用 HTML 标记,以免破坏如命令面板等需要纯文本标题的功能。

代码示例

// 移除私有文章标题前的“Private:”前缀
add_filter( 'private_title_format', 'wpdocs_remove_private_protected_from_titles' );

function wpdocs_remove_private_protected_from_titles( $format ) {
    return '%s';
}

注意事项

不要使用 private_title_format 过滤器添加 HTML 格式,这可能导致如命令面板等功能崩溃。应将格式化逻辑移至主题模板文件中处理。


📄 原文内容

Filters the text prepended to the post title of private posts.

Description

The filter is only applied on the front end.

Parameters

$prependstring
Text displayed before the post title.
Default ‘Private: %s’.
$postWP_Post
Current post object.

Source

$private_title_format = apply_filters( 'private_title_format', $prepend, $post );

Changelog

Version Description
2.8.0 Introduced.

User Contributed Notes

  1. Skip to note 8 content

    Tip: Do _not_ use private_title_format() to add formatting using HTML. This will break things like the command palette that expect the page title to be plain text with no markup. “Format” here means only that you’re returning a sprintf()-style format string, not that this is the way to format the appearance of a private title.

    (I had added an “unlock” icon using private_title_format(); worked great until 6.6 added the command palette to the editor which crashed when it tried to display a private page’s title. So I moved the formatting into the relevant template .php files in the theme.)