sanitize_term()
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
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 'raw', 'edit', 'db', 'display', 'rss', '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. |