@guido07111975 The general guidance is to escape as late as possible. So ideally, if you are echoing data, you’d wrap that data in an escape function. If PHPCS sees an echo $var;
without being escaped, it’s going to assume it’s not escaped and raise and issue.
The deciding factor of whether your function should return escaped data is how your specific function is going to be used. For example, if you are creating a utility function similar to Core’s get_site_url(), which returns the site URL, it likely doesn’t make sense to escape the data, as it may be used for output but it may not be as well. Somebody using that function would be wrapping the results of that function in esc_url()
to follow best practices and ensure that it’s escaped. However, if your function is doing something along the lines of creating HTML for a page such as a function named get_generated_section_html()
it would be best to ensure all the data you are outputting is sanitized, especially if the data is using $_GET or $_POST. It’ll still be up to wherever that function is being output to properly run through a function like wp_kses_post()
, but that extra bit of escaping won’t hurt.
Side-note on your usage of esc_attr()
in your example: esc_attr is useful for escaping a variable that is used in HTML attributes such as <img alt="<?php echo esc_attr( $alt_text ); ?> />
, however in your example it looks like esc_html()
would be better to use since it would be escaping a block of HTML.
Hopefully that all makes sense and helps put you on the right track.
-
This reply was modified 2 years, 7 months ago by
Ben Greeley.
-
This reply was modified 2 years, 7 months ago by
Ben Greeley.
-
This reply was modified 2 years, 7 months ago by
Ben Greeley.