返回介绍

sanitize_term_field()

发布于 2017-09-11 10:19:10 字数 7702 浏览 1165 评论 0 收藏 0

sanitize_term_field( string $field,  string $value,  int $term_id,  string $taxonomy,  string $context )

Cleanse the field value in the term based on the context.


description

Passing a term field value through the function should be assumed to have cleansed the value for whatever context the term field is going to be used.

If no context or an unsupported context is given, then default filters will be applied.

There are enough filters for each context to support a custom filtering without creating your own filter function. Simply create a function that hooks into the filter you need.


参数

$field

(string) (Required) Term field to sanitize.

$value

(string) (Required) Search for this term value.

$term_id

(int) (Required) Term ID.

$taxonomy

(string) (Required) Taxonomy Name.

$context

(string) (Required) Context in which to sanitize the term field. Accepts 'edit', 'db', 'display', 'attribute', or 'js'.


返回值

(mixed) Sanitized field.


源代码

File: wp-includes/taxonomy.php

function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {
	$int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' );
	if ( in_array( $field, $int_fields ) ) {
		$value = (int) $value;
		if ( $value < 0 )
			$value = 0;
	}

	if ( 'raw' == $context )
		return $value;

	if ( 'edit' == $context ) {

		/**
		 * Filters a term field to edit before it is sanitized.
		 *
		 * The dynamic portion of the filter name, `$field`, refers to the term field.
		 *
		 * @since 2.3.0
		 *
		 * @param mixed $value     Value of the term field.
		 * @param int   $term_id   Term ID.
		 * @param string $taxonomy Taxonomy slug.
		 */
		$value = apply_filters( "edit_term_{$field}", $value, $term_id, $taxonomy );

		/**
		 * Filters the taxonomy field to edit before it is sanitized.
		 *
		 * The dynamic portions of the filter name, `$taxonomy` and `$field`, refer
		 * to the taxonomy slug and taxonomy field, respectively.
		 *
		 * @since 2.3.0
		 *
		 * @param mixed $value   Value of the taxonomy field to edit.
		 * @param int   $term_id Term ID.
		 */
		$value = apply_filters( "edit_{$taxonomy}_{$field}", $value, $term_id );

		if ( 'description' == $field )
			$value = esc_html($value); // textarea_escaped
		else
			$value = esc_attr($value);
	} elseif ( 'db' == $context ) {

		/**
		 * Filters a term field value before it is sanitized.
		 *
		 * The dynamic portion of the filter name, `$field`, refers to the term field.
		 *
		 * @since 2.3.0
		 *
		 * @param mixed  $value    Value of the term field.
		 * @param string $taxonomy Taxonomy slug.
		 */
		$value = apply_filters( "pre_term_{$field}", $value, $taxonomy );

		/**
		 * Filters a taxonomy field before it is sanitized.
		 *
		 * The dynamic portions of the filter name, `$taxonomy` and `$field`, refer
		 * to the taxonomy slug and field name, respectively.
		 *
		 * @since 2.3.0
		 *
		 * @param mixed $value Value of the taxonomy field.
		 */
		$value = apply_filters( "pre_{$taxonomy}_{$field}", $value );

		// Back compat filters
		if ( 'slug' == $field ) {
			/**
			 * Filters the category nicename before it is sanitized.
			 *
			 * Use the {@see 'pre_$taxonomy_$field'} hook instead.
			 *
			 * @since 2.0.3
			 *
			 * @param string $value The category nicename.
			 */
			$value = apply_filters( 'pre_category_nicename', $value );
		}

	} elseif ( 'rss' == $context ) {

		/**
		 * Filters the term field for use in RSS.
		 *
		 * The dynamic portion of the filter name, `$field`, refers to the term field.
		 *
		 * @since 2.3.0
		 *
		 * @param mixed  $value    Value of the term field.
		 * @param string $taxonomy Taxonomy slug.
		 */
		$value = apply_filters( "term_{$field}_rss", $value, $taxonomy );

		/**
		 * Filters the taxonomy field for use in RSS.
		 *
		 * The dynamic portions of the hook name, `$taxonomy`, and `$field`, refer
		 * to the taxonomy slug and field name, respectively.
		 *
		 * @since 2.3.0
		 *
		 * @param mixed $value Value of the taxonomy field.
		 */
		$value = apply_filters( "{$taxonomy}_{$field}_rss", $value );
	} else {
		// Use display filters by default.

		/**
		 * Filters the term field sanitized for display.
		 *
		 * The dynamic portion of the filter name, `$field`, refers to the term field name.
		 *
		 * @since 2.3.0
		 *
		 * @param mixed  $value    Value of the term field.
		 * @param int    $term_id  Term ID.
		 * @param string $taxonomy Taxonomy slug.
		 * @param string $context  Context to retrieve the term field value.
		 */
		$value = apply_filters( "term_{$field}", $value, $term_id, $taxonomy, $context );

		/**
		 * Filters the taxonomy field sanitized for display.
		 *
		 * The dynamic portions of the filter name, `$taxonomy`, and `$field`, refer
		 * to the taxonomy slug and taxonomy field, respectively.
		 *
		 * @since 2.3.0
		 *
		 * @param mixed  $value   Value of the taxonomy field.
		 * @param int    $term_id Term ID.
		 * @param string $context Context to retrieve the taxonomy field value.
		 */
		$value = apply_filters( "{$taxonomy}_{$field}", $value, $term_id, $context );
	}

	if ( 'attribute' == $context ) {
		$value = esc_attr($value);
	} elseif ( 'js' == $context ) {
		$value = esc_js($value);
	}
	return $value;
}

更新日志

Versiondescription
2.3.0Introduced.

相关函数

Uses

  • wp-includes/formatting.php: esc_html()
  • wp-includes/formatting.php: esc_attr()
  • wp-includes/formatting.php: esc_js()
  • wp-includes/taxonomy.php: edit_{$taxonomy}_{$field}
  • wp-includes/taxonomy.php: pre_term_{$field}
  • wp-includes/taxonomy.php: pre_{$taxonomy}_{$field}
  • wp-includes/taxonomy.php: pre_category_nicename
  • wp-includes/taxonomy.php: term_{$field}_rss
  • wp-includes/taxonomy.php: {$taxonomy}_{$field}_rss
  • wp-includes/taxonomy.php: term_{$field}
  • wp-includes/taxonomy.php: {$taxonomy}_{$field}
  • wp-includes/taxonomy.php: edit_term_{$field}
  • wp-includes/plugin.php: apply_filters()
  • Show 8 more uses Hide more uses

Used By

  • wp-includes/class-wp-term-query.php: WP_Term_Query::get_terms()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::column_default()
  • wp-admin/includes/class-wp-media-list-table.php: WP_Media_List_Table::column_default()
  • wp-includes/class-wp-query.php: WP_Query::parse_tax_query()
  • wp-includes/category.php: sanitize_category_field()
  • wp-includes/class-wp-tax-query.php: WP_Tax_Query::transform_query()
  • wp-includes/taxonomy.php: get_term_field()
  • wp-includes/taxonomy.php: sanitize_term()
  • wp-includes/feed.php: get_the_category_rss()
  • Show 4 more used by Hide more used by

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文