0

I'm a little confused about using ternary operators and keep the up to date with WP Coding standards. I'm usng Atom along with PHPCS and Wordpress Coding Standards. In theory every variable should be escaped before outputting. But the problem comes when I try to check, if variable isn't empty in ternary operator, PHPCS throws the error at me: WordPress.XSS.EscapeOutput.OutputNotEscaped. That check allows me to display or not additional HTML as you can see in the example below:

$var = 'some-other-class';
echo '<span class="whatever">' . ( '' !== $var ? '<span class="class ' . esc_attr( $var ) . '"></span>' : '' ) . '</span>';

naturally it wants me to escape '' !== $var but does escaping the check bit make any sense if I escape $var later on when it actually passes the check?

  • 1
    WordPress Coding Standards for PHPCS isn't part of WordPress, or an official part of the WordPress project, as far as I can tell. If you have issues with it you should take them up with its maintainers: github.com/WordPress-Coding-Standards/… – Jacob Peattie Jun 24 '18 at 10:25
  • Well ,not exactly an issue, I just thought it may be the case to write the code some other way, the way, I'm not aware of as being beginner. Thank you for your time anyway! – Daniel Jun 24 '18 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.