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 … Read more

What is the correct way to check if WP_Filesystem can write to a directory without aking for username / password?

I’m trying to implement caching using the filesystem so i need to check if i can write in a directory. I’ve read Otto’s article, but i’ve failed to understand how should i implement a check where the user doesn’t input any kind of password. So basically i need to use the direct method, what’s the … Read more

How do you use unzip_file()?

I have been trying to use the unzip_file() function. It says undefined so I looked into it and the WP_Filesystem() must be called and set up. So easy, right? require_once(ABSPATH .’/wp-admin/includes/file.php’); WP_Filesystem(); unzip_file( $zip, $dest ); Even this shows as undefined, and I don’t see any documentation on it. I am trying to use the … Read more

What exactly does $wp_filesystem->abspath() return?

The real problem I have when using the $wp_filesystem is defining the correct paths. In the documentation of the filesystem the path (for example for the plugins directory) is usually set like this: $plugin_path = str_replace(ABSPATH, $wp_filesystem->abspath(), MY_PLUGIN_DIR); I tried to understand this but I failed because I couldn’t find out what $wp_filesystem->abspath() is actually … Read more

How to fix the error “file_get_contents was found in the file functions.php”?

I am building a wordpress theme, I reach the step of finalization. Right now, I am struggling to fix an error raised by Theme Check, : file_get_contents was found in the file functions.php File operations should use the WP_Filesystem methods instead of direct PHP filesystem calls. Here the code: function theme_critical() { $critical_css = file_get_contents( … Read more

Copy a file from a plugin into my theme directory

I coded a wordpress plugin using php’s ‘copy()’ to copy a file from my plugin directory to my theme directory, but it’s not working: <? function file_replace() { $plugin_dir = plugin_dir_path( __FILE__ ) . ‘/library/front-page.php’; $theme_dir = get_stylesheet_directory() . ‘front-page.php’; copy($plugin_dir, $theme_dir); if (!copy($plugin_dir, $theme_dir)) { echo “failed to copy $plugin_dir to $theme_dir…\n”; } } … Read more