Few weeks ago, I wrote a WordPress plugin that displays recently published articles queried from WordPress feed using SimplePie (Yet to be released).
Wondering what Simple is? Well it is a free, fast and easy-to-use feed parser, written in PHP that handles all of the dirty work when it comes to fetching, caching, parsing, and sanitizing the resulting data.
After I had finished writing the plugin, I tested to confirm if it was working perfectly and indeed it worked but there was a minor bug I.e. SimplePie was unable to resolve the path to the file-based cache folder which by default is set to cache.
Below was the exact error message that was returned by SimplePie:
Warning: cache is not writeable. Make sure you’ve set the correct relative or absolute path, and that the location is server-writable. in path/to/SimplePie.php on line 1369
To fix the bug, I had to use the set_cache_location() method which accept a path to the cache folder as it argument where the cache files should be written or/and stored.
$feed->set_cache_location('cache_files');
In WordPress, that didn’t fix it although the folder cache_files
was located in same directory as the plugin file.
I also tried using WordPress plugins_url function like so:
$feed->set_cache_location(plugins_url( 'cache_files' , __FILE__ ));
but still same old shit.
At this point, I got frustrated. The path to the cache folder was correct, what was I missing?
Going through the simple set_cache_location() documentation, I discovered the path to the cache folder was supposed to be a file system location.
At this juncture, I knew this is where WP plugin_dir_path() function will come in handy.
Below is the code that finally fixed the bug.
$feed->set_cache_location(plugin_dir_path( __FILE__ ) . 'cache_files');
I hope someone will find this useful someday.