Do I need to use WP_Filesystem when creating a downloadable file on the fly?

No.

Keep in mind that the code sniffer is a limited utility operating on very simplistic parsing of the PHP code and has very little contextual knowledge. Therefor, you should use it as an advisory, a tool that should direct you to give a second thought to a part of your code, and not as some “source of truth” regarding code quality.

Once you have decided that your code is good, “mask” the error generation with one of the ways the sniffer provides, so it will not bugger you all the time and hide other problems.

Leave a Comment