I’ve been reviewing a lot of information about WP theme and plugin security and understand the concept that you should escape attributes and HTML values in themes and plugins. I’ve seen bloginfo()
and echo get_bloginfo()
used both standard and inside an esc_html()
or esc_attr()
function.
Genesis and _s, Automattic’s base theme both escape these values but WP’s own codex theme standards guide does not say anything about escaping these values. I’ve looked into the WP code (wp-includes/option.php
) and it seems like that there’s a little sanitization of values passed from get_option()
but it also looks like there’s a filter that a plugin could overwrite for certain values.
It’s this fact that leads me toward thinking that it should be escaped. Can anyone enlighten me on this?
We have to look a bit deeper here to get an answer to your question.
So,
bloginfo
is a simple wrapper aroundget_bloginfo
.Notice the second argument
display
. Let’s see what that does.If the filter is set to
display
the output ofget_bloginfo
is run through a filter.Rather than hardcode something like a call to
esc_html
in a function, WP uses it’s own hook system to do things. The place to find that where that happens is inwp-includes/default-filters.php
. A quick search forbloginfo
in that file reveals…bloginfo
is hidden in theforeach
array. As you can see, the output ofbloginfo
gets escaped withesc_html
.In other words, this:
Is equivalent to this:
Or this:
So, no, the output of
bloginfo
does not need to be escaped. Neither does the output ofget_bloginfo
as long as the second argument is set todisplay
.The caveat, however, is that anyone can remove the
esc_html
filter frombloginfo
. So it’s likely safer just to escape the output. And, of course, if you’re using the output ofbloginfo
for anything other than HTML display (eg. in the alt attribute of an image), you should run it throughesc_attr
.