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

---

# sanitize_term( array|object $term, string $taxonomy, string $context ): array|object

## In this article

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

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

Sanitizes all term fields.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/sanitize_term/?output_format=md#description)󠁿

Relies on [sanitize_term_field()](https://developer.wordpress.org/reference/functions/sanitize_term_field/)
to sanitize the term. The difference is that this function will sanitize **all**
fields. The context is based on [sanitize_term_field()](https://developer.wordpress.org/reference/functions/sanitize_term_field/).

The `$term` is expected to be either an array or an object.

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

 `$term`array|objectrequired

The term to check.

`$taxonomy`stringrequired

The taxonomy name to use.

`$context`stringoptional

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

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

 array|object Term with all fields sanitized.

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

    ```php
    function sanitize_term( $term, $taxonomy, $context = 'display' ) {
    	$fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' );

    	$do_object = is_object( $term );

    	$term_id = $do_object ? $term->term_id : ( isset( $term['term_id'] ) ? $term['term_id'] : 0 );

    	foreach ( (array) $fields as $field ) {
    		if ( $do_object ) {
    			if ( isset( $term->$field ) ) {
    				$term->$field = sanitize_term_field( $field, $term->$field, $term_id, $taxonomy, $context );
    			}
    		} else {
    			if ( isset( $term[ $field ] ) ) {
    				$term[ $field ] = sanitize_term_field( $field, $term[ $field ], $term_id, $taxonomy, $context );
    			}
    		}
    	}

    	if ( $do_object ) {
    		$term->filter = $context;
    	} else {
    		$term['filter'] = $context;
    	}

    	return $term;
    }
    ```

[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#L1721)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/taxonomy.php#L1721-L1747)

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

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

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

  |

| Used by | Description | 
| [WP_Term::filter()](https://developer.wordpress.org/reference/classes/wp_term/filter/)`wp-includes/class-wp-term.php` |

Sanitizes term fields, according to the filter type provided.

  | 
| [WP_Term::__get()](https://developer.wordpress.org/reference/classes/wp_term/__get/)`wp-includes/class-wp-term.php` |

Getter.

  | 
| [WP_Term::get_instance()](https://developer.wordpress.org/reference/classes/wp_term/get_instance/)`wp-includes/class-wp-term.php` |

Retrieve [WP_Term](https://developer.wordpress.org/reference/classes/wp_term/) instance.

  | 
| [WP_Terms_List_Table::single_row()](https://developer.wordpress.org/reference/classes/wp_terms_list_table/single_row/)`wp-admin/includes/class-wp-terms-list-table.php` |  | 
| [sanitize_category()](https://developer.wordpress.org/reference/functions/sanitize_category/)`wp-includes/category.php` |

Sanitizes category data based on context.

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

Updates term based on arguments provided.

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

Adds a new term to the database.

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

Sanitizes term for editing.

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

Gets all term data from database by term ID.

  |

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

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/sanitize_term/?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%2F)
before being able to contribute a note or feedback.