钩子文档

sanitize_title

💡 云策文档标注

概述

sanitize_title 是一个 WordPress 过滤器,用于在 sanitize_title() 函数清理标题字符串后,对结果进行进一步修改,使其适合用作 URL 或 HTML 属性中的 slug。

关键要点

  • 过滤器名称:sanitize_title,通过 apply_filters() 调用。
  • 参数:$title(已清理的标题字符串)、$raw_title(原始标题字符串)、$context(清理上下文)。
  • 用途:允许开发者自定义标题清理后的输出,例如修改大小写或替换字符。
  • 相关函数:sanitize_title(),位于 wp-includes/formatting.php,用于将字符串转换为 slug。
  • 版本历史:自 WordPress 1.2.0 引入。

代码示例

// 将 URL 值转换为小写
add_filter( 'sanitize_title', 'strtolower' );

// 将加号替换为短横线
function wpdocs_plus_to_dash( $title ) {
    return str_replace( '+', '-', $title );
}
add_filter( 'sanitize_title', 'wpdocs_plus_to_dash' );

📄 原文内容

Filters a sanitized title string.

Parameters

$titlestring
Sanitized title.
$raw_titlestring
The title prior to sanitization.
$contextstring
The context for which the title is being sanitized.

More Information

sanitize_title is a filter applied to a value to be cleaned up for use in a URL by the function sanitize_title()

Source

$title = apply_filters( 'sanitize_title', $title, $raw_title, $context );

Changelog

Version Description
1.2.0 Introduced.

User Contributed Notes