# sanitize_term_field()

URL: https://chugunov.pro/api-wordpress/functions/sanitize_term_field/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 2.3.0.

## Сигнатура

```php
sanitize_term_field( string $field, string $value, int $term_id, string $taxonomy, string $context ): mixed
```

## Описание

Пропустив значение поля термина через эту функцию, следует считать, что значение очищено для того контекста, в котором поле термина будет использоваться.
Если контекст не указан или указан неподдерживаемый, будут применены фильтры по умолчанию.
Для каждого контекста существует достаточно фильтров, чтобы поддержать пользовательскую фильтрацию без создания собственной функции-фильтра. Просто создайте функцию, подключаемую к нужному вам фильтру.

## Параметры

- `$field` `string` — обязательный. Поле термина для очистки.
- `$value` `string` — обязательный. Искать по этому значению термина.
- `$term_id` `int` — обязательный. ID термина.
- `$taxonomy` `string` — обязательный. Имя таксономии.
- `$context` `string` — обязательный. Контекст, в котором очищается поле термина.
  
  Принимает 'raw', 'edit', 'db', 'display', 'rss', 'attribute' или 'js'.

## Возвращаемое значение

`mixed`

## Исходный код

Файл: `wp-includes/taxonomy.php:1773`

```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, true ) ) {
		$value = (int) $value;
		if ( $value < 0 ) {
			$value = 0;
		}
	}

	$context = strtolower( $context );

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

	if ( 'edit' === $context ) {

		/**
		 * Filters a term field to edit before it is sanitized.
		 *
		 * The dynamic portion of the hook 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 hook 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 '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 hook 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 hook 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 );
	}

	// Restore the type for integer fields after esc_attr().
	if ( in_array( $field, $int_fields, true ) ) {
		$value = (int) $value;
	}

	return $value;
}
```

## История изменений

- 2.3.0 — Introduced.

## Связанные

Использует: [`esc_js`](https://chugunov.pro/api-wordpress/functions/esc_js/), [`esc_html`](https://chugunov.pro/api-wordpress/functions/esc_html/), [`esc_attr`](https://chugunov.pro/api-wordpress/functions/esc_attr/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/).
Используется в: `WP_Term_Query::get_terms`, `WP_Posts_List_Table::column_default`, `WP_Media_List_Table::column_default`, `WP_Query::parse_tax_query`, [`sanitize_category_field`](https://chugunov.pro/api-wordpress/functions/sanitize_category_field/), [`get_term_field`](https://chugunov.pro/api-wordpress/functions/get_term_field/), [`sanitize_term`](https://chugunov.pro/api-wordpress/functions/sanitize_term/), [`get_the_category_rss`](https://chugunov.pro/api-wordpress/functions/get_the_category_rss/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/sanitize_term_field/
