sanitize_term

The timeline below displays how wordpress function sanitize_term has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Sanitizes all term fields.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param array|object $term     The term to check.
 * @param string       $taxonomy The taxonomy name to use.
 * @param string       $context  Optional. Context in which to sanitize the term.
 *                               Accepts 'raw', 'edit', 'db', 'display', 'rss',
 *                               'attribute', or 'js'. Default 'display'.
 * @return array|object Term with all fields sanitized.
 */
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;
}

WordPress Version: 5.7

/**
 * Sanitize all term fields.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param array|object $term     The term to check.
 * @param string       $taxonomy The taxonomy name to use.
 * @param string       $context  Optional. Context in which to sanitize the term.
 *                               Accepts 'raw', 'edit', 'db', 'display', 'rss',
 *                               'attribute', or 'js'. Default 'display'.
 * @return array|object Term with all fields sanitized.
 */
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;
}

WordPress Version: 4.3

/**
 * Sanitize Term all fields.
 *
 * Relies on sanitize_term_field() to sanitize the term. The difference is that
 * this function will sanitize <strong>all</strong> fields. The context is based
 * on sanitize_term_field().
 *
 * The $term is expected to be either an array or an object.
 *
 * @since 2.3.0
 *
 * @param array|object $term     The term to check.
 * @param string       $taxonomy The taxonomy name to use.
 * @param string       $context  Optional. Context in which to sanitize the term. Accepts 'edit', 'db',
 *                               'display', 'attribute', or 'js'. Default 'display'.
 * @return array|object Term with all fields sanitized.
 */
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;
}

WordPress Version: 4.1

/**
 * Sanitize Term all fields.
 *
 * Relies on sanitize_term_field() to sanitize the term. The difference is that
 * this function will sanitize <strong>all</strong> fields. The context is based
 * on sanitize_term_field().
 *
 * The $term is expected to be either an array or an object.
 *
 * @since 2.3.0
 *
 * @param array|object $term The term to check
 * @param string $taxonomy The taxonomy name to use
 * @param string $context Default is 'display'.
 * @return array|object Term with all fields sanitized
 */
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;
}

WordPress Version: 3.9

/**
 * Sanitize Term all fields.
 *
 * Relies on sanitize_term_field() to sanitize the term. The difference is that
 * this function will sanitize <strong>all</strong> fields. The context is based
 * on sanitize_term_field().
 *
 * The $term is expected to be either an array or an object.
 *
 * @since 2.3.0
 *
 * @uses sanitize_term_field Used to sanitize all fields in a term
 *
 * @param array|object $term The term to check
 * @param string $taxonomy The taxonomy name to use
 * @param string $context Default is 'display'.
 * @return array|object Term with all fields sanitized
 */
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;
}

WordPress Version: 3.8

/**
 * Sanitize Term all fields.
 *
 * Relies on sanitize_term_field() to sanitize the term. The difference is that
 * this function will sanitize <strong>all</strong> fields. The context is based
 * on sanitize_term_field().
 *
 * The $term is expected to be either an array or an object.
 *
 * @package WordPress
 * @subpackage Taxonomy
 * @since 2.3.0
 *
 * @uses sanitize_term_field Used to sanitize all fields in a term
 *
 * @param array|object $term The term to check
 * @param string $taxonomy The taxonomy name to use
 * @param string $context Default is 'display'.
 * @return array|object Term with all fields sanitized
 */
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;
}

WordPress Version: 3.7

/**
 * Sanitize Term all fields.
 *
 * Relies on sanitize_term_field() to sanitize the term. The difference is that
 * this function will sanitize <strong>all</strong> fields. The context is based
 * on sanitize_term_field().
 *
 * The $term is expected to be either an array or an object.
 *
 * @package WordPress
 * @subpackage Taxonomy
 * @since 2.3.0
 *
 * @uses sanitize_term_field Used to sanitize all fields in a term
 *
 * @param array|object $term The term to check
 * @param string $taxonomy The taxonomy name to use
 * @param string $context Default is 'display'.
 * @return array|object Term with all fields sanitized
 */
function sanitize_term($term, $taxonomy, $context = 'display')
{
    if ('raw' == $context) {
        return $term;
    }
    $fields = array('term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group');
    $do_object = false;
    if (is_object($term)) {
        $do_object = true;
    }
    $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;
}