Paramètre

$text(string)requis

Texte à échapper.

Retourne

(string) Texte échappé.

Déclaration et structure de la fonction esc_html()

function esc_html( $text ) {
    $safe_text = wp_check_invalid_utf8( $text );
    $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
    /**
     * Filters a string cleaned and escaped for output in HTML.
     *
     * Text passed to esc_html() is stripped of invalid or special characters
     * before output.
     *
     * @since 2.8.0
     *
     * @param string $safe_text The text after it has been escaped.
     * @param string $text      The text prior to being escaped.
     */
    return apply_filters( 'esc_html', $safe_text, $text );
}
wp_check_invalid_utf8()

Vérifie si les caractères d'une chaîne sont bien au format UTF-8.

_wp_specialchars()

Convertie certains caractères spéciaux en entités HTML.

apply_filters()

Appel les fonctions qui ont été attaché à un filtre (hook).

esc_html

Filtre une chaîne nettoyée et échappée pour une sortie en HTML.

Où est utilisée la fonction esc_html() dans le CMS WordPress

Exemples

$lien = '<a href="http://www.example.com/">A link</a>';
$esc_html = esc_html($lien);

// $esc_html affiche :
// <a href="http://www.example.com/">A link</a>
Noter que esc_html() tentera de supprimer les doubles guillemets :
$html = 'A && B';
esc_html($html);

// Affichera :
// 'A & B';

Sources

Codex WordPress : esc_html()

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

Retour