sanitize_term() WordPress Function

The sanitize_term() function is a WordPress function that is used to clean up a term before it is inserted into the database. It is used to remove unwanted characters from a term, such as ' and " .

sanitize_term( array|object $term, string $taxonomy, string $context = 'display' ) #

Sanitizes all term fields.


Description

Relies on 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().

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


Top ↑

Parameters

$term

(array|object)(Required)The term to check.

$taxonomy

(string)(Required)The taxonomy name to use.

$context

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

Default value: 'display'


Top ↑

Return

(array|object) Term with all fields sanitized.


Top ↑

Source

File: wp-includes/taxonomy.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;
}


Top ↑

Changelog

Changelog
VersionDescription
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More