sanitize_title

函数
sanitize_title ( $title, $fallback_title = '', $context = 'save' )
参数
  • (string) $title The string to be sanitized.
    Required:
  • (string) $fallback_title Optional. A title to use if $title is empty. Default empty.
    Required:
    Default: (empty)
  • (string) $context Optional. The operation for which the string is sanitized. When set to 'save', the string runs through remove_accents(). Default 'save'.
    Required:
    Default: 'save'
返回值
  • (string) The sanitized string.
定义位置
相关方法
sanitize_termsanitize_urlsanitize_keysanitize_optionsanitize_file_name
引入
1.0.0
弃用
-

sanitize_title: 这是一个WordPress的函数,可以对文章或术语的标题进行净化处理。它用于删除任何HTML标签和特殊字符,并创建一个用户友好的标题: 这个函数有一个参数,就是要净化的标题。

将一个字符串净化成一个lug,可以在URL或HTML属性中使用。

默认情况下,将重音字符转换为ASCII字符,并进一步限制输出为字母数字字符、下划线(_)和破折号(-)。
通过{@see ‘sanitize_title’}过滤器。

如果`$title’为空而`$fallback_title’被设置,将使用后者。

function sanitize_title( $title, $fallback_title = '', $context = 'save' ) {
	$raw_title = $title;

	if ( 'save' === $context ) {
		$title = remove_accents( $title );
	}

	/**
	 * Filters a sanitized title string.
	 *
	 * @since 1.2.0
	 *
	 * @param string $title     Sanitized title.
	 * @param string $raw_title The title prior to sanitization.
	 * @param string $context   The context for which the title is being sanitized.
	 */
	$title = apply_filters( 'sanitize_title', $title, $raw_title, $context );

	if ( '' === $title || false === $title ) {
		$title = $fallback_title;
	}

	return $title;
}

常见问题

FAQs
查看更多 >