It seems that they do almost same type of job. So...
When should I use esc_html()
instead of sanitize_text_field()
?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityesc_html()
is more or less lossless — it just turns HTML markup into encoded visible text, so that it's not rendered as markup by browser.
Semantically it's escape, so it's meant to be used to make output to page safe.
sanitize_text_field()
however actually removes all HTML markup, as well as extra whitespace. It leaves nothing but plain text.
Semantically it's sanitize, so it's meant to be used to make input being saved safe.
sanitize_text_field()
and strip_tags