Can’t access wp_filesystem in cron function

I’m writing a plugin for integrating WP with Visma. It will basically spit out an XML file every third hour using wp cron (going to setup server cron for wp-cron.php on the live site) and save it to the uploads folder.

I got everything working when doing this manually (submit in wp-admin). However when running the cron function it seems $wp_filesystem isn’t accessible nor is it possible to set it up with request_filesystem_credentials(). If I try to run request_filesystem_credentials() in the cron function I get an error saying the function does not exist. If I leave it to my other function (hooked into admin_init) $wp_filesystem is just empty (non existent).

I’m trying to save stuff to uploads-folder in a cron function.

I’ve tried setting the FTP constants in wp-config.php. I’ve tried setting the priority of the wp_filesystem setup function to 1 (to make sure it runs as soon as possible).

I’d prefer using the wp_filesystem since it has so many benefits in terms of stability and security.

1 Answer
1

So of course as soon as I post this I find the solution. After looking for it for a day. I had to include the file handling the request_filesystem_credentials manually in the function. For reference this was
require ABSPATH . 'wp-admin/includes/file.php';

Leave a Comment