Convenient way to use wp_filesystem

I’d like to use $wp_filesystem which seems to be the recommended way to manipulate file system objects in WordPress but compare this: plain PHP code: mkdir(‘abc’); WP Filesystem API code: $url = wp_nonce_url(‘plugins.php’); if (false === ($creds = request_filesystem_credentials($url, ”, false, false, null) ) ) { echo “Could not create filesystem credentials”; return; } if … Read more

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

I have a method that creates a downloadable CSV on the fly when a button is clicked: header( ‘Content-type: text/csv’ ); header( ‘Cache-Control: no-store, no-cache’ ); header( ‘Content-Disposition: attachment; filename=”email_list.csv”‘ ); $outstream = fopen( ‘php://output’, ‘w’ ); fputcsv( $outstream, $csv->header ); foreach ( $csv->rows as $row ) { fputcsv( $outstream, $row ); } fclose( $outstream … Read more

Call to a member function put_contents() on a non-object

I have a plugin and I am creating a css file in wp_content. I used this: $this->content_dir = WP_CONTENT_DIR . “/some_folder”; $path = $this->content_dir . ‘options.css’; $css=”some string”; global $wp_filesystem; if(!$wp_filesystem->put_contents( $path, $css, 0644) ) { return __(‘Failed to create css file’); } However I get this error: Fatal error: Call to a member function … Read more

Which filters or actions to use after a media upload and delete?

When a media (image) is uploaded and the subsequent images are created (all the different sizes), I would like to perform a few actions. It is simple and I basically need only the new attachment ID, that would be enough. Same during deletion: before or after deletion, I need to know which attachment ID is … Read more

How to use WordPress HTTP API to download file from remote location

So this PHP code works for me: $ch = curl_init( TCS_CPDF_REMOTE_ZIP ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); $data = curl_exec( $ch ); curl_close( $ch ); file_put_contents( TCS_CPDF_LOCAL_ZIP, $data ); but when trying to use the WordPress HTTP API: $the_body = wp_remote_retrieve_body( wp_remote_get( TCS_CPDF_REMOTE_ZIP ) ); file_put_contents( TCS_CPDF_LOCAL_ZIP, $the_body ); I end up getting a 0KB … Read more

Standard location for plugin to save/cache files?

Are there official guidelines on where a plugin should cache files? If not, is there a best-practice out there that I can follow? For example, a plugin grabs content from a back-end system benefits from caching the file so it doesn’t repeatedly fetch the same content from the back-end. Searching here and elsewhere is tough … Read more

Creating directory in uploads – wp_mkdir_p() or WP_Filesystem?

I don’t think I ever had practical need to create folders/files in WP before, but for a plugin I need cache (for resized images) folder in wp-content/uploads. Which raises the question – do I really need to go through all the process with Filesystem API (including messily asking for FTP credentials when needed), or wp_mkdir_p() … Read more