Description

Pour passer une valeur de champs à cette fonction, l'on doit s'assurer de l'avoir nettoyé, peu importe le contexte dans lequel la valeur sera utilisée.

S'il n'y a pas de contexte ou qu'un contexte donné est invalide, alors les filtres par défaut seront appliqués.

Il y a assez de filtres pour chaque contexte pour supporter un filtrage personnalisé sans avoir besoin de créer une fonction de filtres personnalisée.

Paramètres

$field

(string) (Requis) Champ de terme à nettoyer.

$value

(string) (Requis) Valeur à nettoyer en fonction du champ donné.

$term_id

(int) (Requis) Id du terme.

$taxonomy

(string) (Requis) Nom de la taxonomie à laquelle le terme appartient.

$context

(string) (Requis) Contexte dans lequel nettoyer le champ du terme. Accepte 'edit', 'db', 'display', 'attribute', ou 'js'.

Retourne

(mixed) Le champ nettoyé.

Structure de la fonction sanitize_term_field()

Définie dans le fichier wp-includes/taxonomy.php à la ligne 1575 :

function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) {
    $int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' );
    if ( in_array( $field, $int_fields, true ) ) {
        $value = (int) $value;
        if ( $value < 0 ) {
            $value = 0;
        }
    }

    $context = strtolower( $context );

    if ( 'raw' === $context ) {
        return $value;
    }

    if ( 'edit' === $context ) {

        /**
         * Filters a term field to edit before it is sanitized.
         *
         * The dynamic portion of the filter name, `$field`, refers to the term field.
         *
         * @since 2.3.0
         *
         * @param mixed $value     Value of the term field.
         * @param int   $term_id   Term ID.
         * @param string $taxonomy Taxonomy slug.
         */
        $value = apply_filters( "edit_term_{$field}", $value, $term_id, $taxonomy );

        /**
         * Filters the taxonomy field to edit before it is sanitized.
         *
         * The dynamic portions of the filter name, `$taxonomy` and `$field`, refer
         * to the taxonomy slug and taxonomy field, respectively.
         *
         * @since 2.3.0
         *
         * @param mixed $value   Value of the taxonomy field to edit.
         * @param int   $term_id Term ID.
         */
        $value = apply_filters( "edit_{$taxonomy}_{$field}", $value, $term_id );

        if ( 'description' === $field ) {
            $value = esc_html( $value ); // textarea_escaped
        } else {
            $value = esc_attr( $value );
        }
    } elseif ( 'db' === $context ) {

        /**
         * Filters a term field value before it is sanitized.
         *
         * The dynamic portion of the filter name, `$field`, refers to the term field.
         *
         * @since 2.3.0
         *
         * @param mixed  $value    Value of the term field.
         * @param string $taxonomy Taxonomy slug.
         */
        $value = apply_filters( "pre_term_{$field}", $value, $taxonomy );

        /**
         * Filters a taxonomy field before it is sanitized.
         *
         * The dynamic portions of the filter name, `$taxonomy` and `$field`, refer
         * to the taxonomy slug and field name, respectively.
         *
         * @since 2.3.0
         *
         * @param mixed $value Value of the taxonomy field.
         */
        $value = apply_filters( "pre_{$taxonomy}_{$field}", $value );

        // Back compat filters.
        if ( 'slug' === $field ) {
            /**
             * Filters the category nicename before it is sanitized.
             *
             * Use the {@see 'pre_$taxonomy_$field'} hook instead.
             *
             * @since 2.0.3
             *
             * @param string $value The category nicename.
             */
            $value = apply_filters( 'pre_category_nicename', $value );
        }
    } elseif ( 'rss' === $context ) {

        /**
         * Filters the term field for use in RSS.
         *
         * The dynamic portion of the filter name, `$field`, refers to the term field.
         *
         * @since 2.3.0
         *
         * @param mixed  $value    Value of the term field.
         * @param string $taxonomy Taxonomy slug.
         */
        $value = apply_filters( "term_{$field}_rss", $value, $taxonomy );

        /**
         * Filters the taxonomy field for use in RSS.
         *
         * The dynamic portions of the hook name, `$taxonomy`, and `$field`, refer
         * to the taxonomy slug and field name, respectively.
         *
         * @since 2.3.0
         *
         * @param mixed $value Value of the taxonomy field.
         */
        $value = apply_filters( "{$taxonomy}_{$field}_rss", $value );
    } else {
        // Use display filters by default.

        /**
         * Filters the term field sanitized for display.
         *
         * The dynamic portion of the filter name, `$field`, refers to the term field name.
         *
         * @since 2.3.0
         *
         * @param mixed  $value    Value of the term field.
         * @param int    $term_id  Term ID.
         * @param string $taxonomy Taxonomy slug.
         * @param string $context  Context to retrieve the term field value.
         */
        $value = apply_filters( "term_{$field}", $value, $term_id, $taxonomy, $context );

        /**
         * Filters the taxonomy field sanitized for display.
         *
         * The dynamic portions of the filter name, `$taxonomy`, and `$field`, refer
         * to the taxonomy slug and taxonomy field, respectively.
         *
         * @since 2.3.0
         *
         * @param mixed  $value   Value of the taxonomy field.
         * @param int    $term_id Term ID.
         * @param string $context Context to retrieve the taxonomy field value.
         */
        $value = apply_filters( "{$taxonomy}_{$field}", $value, $term_id, $context );
    }

    if ( 'attribute' === $context ) {
        $value = esc_attr( $value );
    } elseif ( 'js' === $context ) {
        $value = esc_js( $value );
    }
    return $value;
}

Fonctions et Hooks utilisés par sanitize_term_field()

esc_html()

Échappement pour les bloques HTML.

esc_attr()

Échappe les valeurs des attributs HTML.

esc_js()

Échappe les guillemets simples, le caractère spécial & et traite / enlève les fins de lignes.

edit_{$taxonomy}_{$field}

Filtre le champ de la taxonomie à éditer avant qu'il ne soit nettoyé.

pre_term_{$field}

Filtre la valeur du champ du terme avant qu'il ne soit nettoyé.

Où trouver la fonction sanitize_term_field() dans le CMS Wordpress

Sources

Codex Wordpress : sanitize_term_field()

Autres fonctions dans le même fichier : wp-includes/taxonomy.php

Retour