Can I simply use htmlspecialchars instead of esc_attr?
1 Answer
esc_attr() is written specifically for escaping a string that is to be used as an html attribute, which means also escaping single and double-quote characters etc.
In general, it's better to use the data validation API that WP provides rather than the generic PHP functions.
-
And what is the difference between the WordPress function
esc_html
and the PHP functionhtmlspecialchars
(withENT_QUOTES
flag)? It is also good to note that using an official PHP function instead of a WordPress function has the advantage to have a more portable code, that you can use without CMS or with another CMS.– baptxCommented Jul 19, 2019 at 15:44 -
The WordPress function can be updated with other sanitization methods as we discover them. Commented Feb 2, 2022 at 1:19
esc_html