WordPress at a glance

sanitize_term() WP 2.3.0

Sanitize all fields of the taxonomy element (term) using the sanitize_term_field() function.

Relies on sanitize_term_field() to sanitize the term. The difference is that this function will sanitize all fields. Takes an array or data object of a term, processes each value with sanitize_term_field(), and returns the passed array.

Also, 'filter' = $context key is added into the returned array/object, where $context is a context in which data was sanitized. It is needed for optimization, to not sanitize fields again in multiple calls. So, if 'filter' is specified and it matches the given context, the function will simply return the passed object.

Also, the function will remove invalid fields from the passed object. Only these fields are returned:

term_id
name
description
slug
count
parent
term_group
term_taxonomy_id
object_id
Works based on: sanitize_term_field()
✈ 1 time = 0.000771s = slow | 50000 times = 6s = fast

No Hooks.

Return

Array/Object. Term with all fields sanitized.

Usage

sanitize_term( $term, $taxonomy, $context );
$term(array/object) (required)
The term to check.
$taxonomy(string) (required)
The taxonomy name to use.
$context(string)
Context in which to sanitize the term. Accepts 'edit', 'db', 'display', 'attribute', or 'js'.
Default: 'display'

Examples

#1 Demonstration of fields clearing for taxonomy element (term)

// Original array
$term = array(
	'term_id'          => '3',
	'name'             => 'Word \' press <tag>foo</tag>',
	'slug'             => 'word press <tag>',
	'term_group'       => 0,
	'term_taxonomy_id' => 3,
	'taxonomy'         => 'category',
	'description'      => 'Description > " \' press <tag>foo</tag>',
	'parent'           => 0,
	'count'            => 0,
);

$term = sanitize_term( $term, 'category' );
/*
Array
(
	[term_id] => 3
	[name] => Word ‘ press <tag>foo</tag>
	[slug] => word press <tag>
	[term_group] => 0
	[term_taxonomy_id] => 3
	[taxonomy] => category
	[description] => <p>Description > » ‘ press <tag>foo</tag></p>
	[parent] => 0
	[count] => 0
	[filter] => display
)
*/

$term = sanitize_term( $term, 'category', 'db' );
/*
Array
(
	[term_id] => 3
	[name] => Word \' press foo
	[slug] => word-press-tag
	[term_group] => 0
	[term_taxonomy_id] => 3
	[taxonomy] => category
	[description] => Description > \" \' press foo
	[parent] => 0
	[count] => 0
	[filter] => db
)
*/

$term = sanitize_term( $term, 'category', 'raw' );
/*
Array
(
	[term_id] => 3
	[name] => Word ' press <tag>foo</tag>
	[slug] => word press <tag>
	[term_group] => 0
	[term_taxonomy_id] => 3
	[taxonomy] => category
	[description] => Description > " ' press <tag>foo</tag>
	[parent] => 0
	[count] => 0
	[filter] => raw
)
*/

$term = sanitize_term( $term, 'category', 'js' );
/*
Array
(
	[term_id] => 3
	[name] => Word ‘ press <tag>foo</tag>
	[slug] => word press <tag>
	[term_group] => 0
	[term_taxonomy_id] => 3
	[taxonomy] => category
	[description] => <p>Description > » ‘ press <tag>foo</tag></p>
	[parent] => 0
	[count] => 0
	[filter] => js
)
*/

Changelog

Since 2.3.0 Introduced.

Code of sanitize term: wp-includes/taxonomy.php WP 5.2.4

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

Related Functions

From tag: sanitize_

More from category: Any taxonomy

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register