What’s a simple but secure method to get file contents into WordPress?

The scenario

I’m developing a plugin, and part of it requires lots of text content (help descriptions) that I would prefer to keep as separate text files (in a subdirectory within the plugin) for organisational and version control purposes. The obvious candidate would be file_get_contents(), but its usage with WordPress is generally frowned upon. I’ve looked at some alternatives, such as wp_remote_get() (not the right one as the files are in the plugin) and the WP_Filesystem API (which seems overkill for a simple reading of text files). Is there a simple, but secure alternative? I have the nagging feeling I’m missing something very obvious.

The spec

I’m looking for a solution to:

  1. Read the contents of a HTML/text file into a variable
  2. That won’t flag as insecure when used with Theme Check or similar plugins
  3. Doesn’t require any writing to the files whatsoever

2 Answers
2

I had to dig pretty deep for this too, and eventually figured out some way to do this.

in my solution I used the following:

WP_Filesystem();
global $wp_filesystem;
$wp_filesystem->exists( '/path/to/file' ); //use any other functions available in the Filesystem API: @see https://developer.wordpress.org/reference/classes/wp_filesystem_direct/

Leave a Comment