Title: sanitize_term_field
Published: April 25, 2014
Last modified: February 24, 2026

---

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

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#wp--skip-link--target)

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

## 󠀁[Description](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#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.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#parameters)󠁿

 `$field`stringrequired

Term field to sanitize.

`$value`stringrequired

Search for this term value.

`$term_id`intrequired

Term ID.

`$taxonomy`stringrequired

Taxonomy name.

`$context`stringrequired

Context in which to sanitize the term field.
 Accepts `'raw'`, `'edit'`, `'db'`,`'
display'`, `'rss'`, `'attribute'`, or `'js'`. Default `'display'`.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#return)󠁿

 mixed Sanitized field.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#source)󠁿

    ```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;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/taxonomy.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/taxonomy.php#L1773)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/taxonomy.php#L1773-L1929)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#hooks)󠁿

 [apply_filters( “edit_term_{$field}”, mixed $value, int $term_id, string $taxonomy )](https://developer.wordpress.org/reference/hooks/edit_term_field/)

Filters a term field to edit before it is sanitized.

 [apply_filters( “edit_{$taxonomy}_{$field}”, mixed $value, int $term_id )](https://developer.wordpress.org/reference/hooks/edit_taxonomy_field/)

Filters the taxonomy field to edit before it is sanitized.

 [apply_filters( ‘pre_category_nicename’, string $value )](https://developer.wordpress.org/reference/hooks/pre_category_nicename/)

Filters the category nicename before it is sanitized.

 [apply_filters( “pre_term_{$field}”, mixed $value, string $taxonomy )](https://developer.wordpress.org/reference/hooks/pre_term_field/)

Filters a term field value before it is sanitized.

 [apply_filters( “pre_{$taxonomy}_{$field}”, mixed $value )](https://developer.wordpress.org/reference/hooks/pre_taxonomy_field/)

Filters a taxonomy field before it is sanitized.

 [apply_filters( “term_{$field}”, mixed $value, int $term_id, string $taxonomy, string $context )](https://developer.wordpress.org/reference/hooks/term_field/)

Filters the term field sanitized for display.

 [apply_filters( “term_{$field}_rss”, mixed $value, string $taxonomy )](https://developer.wordpress.org/reference/hooks/term_field_rss/)

Filters the term field for use in RSS.

 [apply_filters( “{$taxonomy}_{$field}”, mixed $value, int $term_id, string $context )](https://developer.wordpress.org/reference/hooks/taxonomy_field/)

Filters the taxonomy field sanitized for display.

 [apply_filters( “{$taxonomy}_{$field}_rss”, mixed $value )](https://developer.wordpress.org/reference/hooks/taxonomy_field_rss/)

Filters the taxonomy field for use in RSS.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#related)󠁿

| Uses | Description | 
| [esc_js()](https://developer.wordpress.org/reference/functions/esc_js/)`wp-includes/formatting.php` |

Escapes single quotes, `"`, , `&amp;`, and fixes line endings.

  | 
| [esc_html()](https://developer.wordpress.org/reference/functions/esc_html/)`wp-includes/formatting.php` |

Escaping for HTML blocks.

  | 
| [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)`wp-includes/formatting.php` |

Escaping for HTML attributes.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 2 more](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#)

| Used by | Description | 
| [WP_Term_Query::get_terms()](https://developer.wordpress.org/reference/classes/wp_term_query/get_terms/)`wp-includes/class-wp-term-query.php` |

Retrieves the query results.

  | 
| [WP_Posts_List_Table::column_default()](https://developer.wordpress.org/reference/classes/wp_posts_list_table/column_default/)`wp-admin/includes/class-wp-posts-list-table.php` |

Handles the default column output.

  | 
| [WP_Media_List_Table::column_default()](https://developer.wordpress.org/reference/classes/wp_media_list_table/column_default/)`wp-admin/includes/class-wp-media-list-table.php` |

Handles output for the default column.

  | 
| [WP_Query::parse_tax_query()](https://developer.wordpress.org/reference/classes/wp_query/parse_tax_query/)`wp-includes/class-wp-query.php` |

Parses various taxonomy related query vars.

  | 
| [sanitize_category_field()](https://developer.wordpress.org/reference/functions/sanitize_category_field/)`wp-includes/category.php` |

Sanitizes data in single category key field.

  | 
| [get_term_field()](https://developer.wordpress.org/reference/functions/get_term_field/)`wp-includes/taxonomy.php` |

Gets sanitized term field.

  | 
| [sanitize_term()](https://developer.wordpress.org/reference/functions/sanitize_term/)`wp-includes/taxonomy.php` |

Sanitizes all term fields.

  | 
| [get_the_category_rss()](https://developer.wordpress.org/reference/functions/get_the_category_rss/)`wp-includes/feed.php` |

Retrieves all of the post categories, formatted for use in feeds.

  |

[Show 3 more](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/sanitize_term_field/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.3.0](https://developer.wordpress.org/reference/since/2.3.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fsanitize_term_field%2F)
before being able to contribute a note or feedback.