sanitize_term

Sanitize Term all fields.

Description

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

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.

Returns (array|object)

Term with all fields sanitized.

Parameters (3)

0. $term (array|object)
The term to check.
1. $taxonomy (string)
The taxonomy name to use.
2. $context — Optional. (string) => 'display'
Context in which to sanitize the term. Accepts edit,, db., display, attribute, or js. Default display.

Usage

  1. if ( !function_exists( 'sanitize_term' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // The term to check. 
  5. $term = null; 
  6.  
  7. // The taxonomy name to use. 
  8. $taxonomy = ''; 
  9.  
  10. // Optional. Context in which to sanitize the term. Accepts 'edit', 'db', 
  11. // 'display', 'attribute', or 'js'. Default 'display'. 
  12. $context = 'display'; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = sanitize_term($term, $taxonomy, $context); 
  16.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/taxonomy.php  
  1. function sanitize_term($term, $taxonomy, $context = 'display') { 
  2. $fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' ); 
  3.  
  4. $do_object = is_object( $term ); 
  5.  
  6. $term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0); 
  7.  
  8. foreach ( (array) $fields as $field ) { 
  9. if ( $do_object ) { 
  10. if ( isset($term->$field) ) 
  11. $term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context); 
  12. } else { 
  13. if ( isset($term[$field]) ) 
  14. $term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context); 
  15.  
  16. if ( $do_object ) 
  17. $term->filter = $context; 
  18. else 
  19. $term['filter'] = $context; 
  20.  
  21. return $term;