Articles of wp filesystem

$wp_filesystem returns NULL. What are the dependencies?

I need to obtain a reference to the $wp_filesystem object. In the test below, the var_dump($wp_filesystem) returns NULL. What additional files are required in order to properly set up $wp_filesystem? I was expecting that since its called in file.php, loading that file would be sufficient to load the object. <?php require(‘../../../wp-blog-header.php’); require(‘../../../wp-admin/includes/file.php’); $mytest = somefunction(); […]

What to use , set_transient, set_option or file system?

I have a shortcode that can parse page contents. For example you give it a link to a GitHub md file , it reads it and saves the page content for you. $parse_atts = array( ‘id’ => $id, ‘url’ => $url, ‘days’ => $days, ‘hours’ => $hours, ‘markdown’ => $markdown, ‘escaped’ => $escaped, ‘mdengine’ => […]

Converting fopen/fwrite operations to WP_filesystem

In the function below, I’m trying to convert the fopen/fwrite/fclose methods to the preferred WP_filesystem methods. Everything works as expected using the commented out script block, however, the WP_filesystem methods always return the error block. Any ideas what I’m doing wrong? function so_doHtaccess(){ if ( !current_user_can( ‘manage_options’ ) ) return; if ( file_exists( get_home_path() . […]

Moving wp-content outside of web root?

Is it possible to move the entire wp-content folder outside of the Web root? I need to prevent anyone to view any asset file (images, pdfs, movies etc) with a direct url to the file… Any ideas? Or could I do this safely with htaccess?

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 […]

What is the best way to move a plugin´s subdirectory+files to wp-content/uploads-directory?

I am developer of the plugin Leaflet Maps Marker ( – “Pin, organize & show your favorite places through OpenStreetMap, OGD Vienna or any custom WMTS-map”) and have one question: the plugin contains about 100 marker icons from Maps Icons Collection ( which can be used to mark your favorite places. These icons are stored […]

Where is Featured Image code stored in WP?

Well, I’m looking for source code of these two small containers (PHP backend, HTML frontend & JS used to attach the image): I want to build a theme option based on Featured Image code, but I’m not able to find it. I’m pretty sure it is somewhere in wp-admin directory, but I’ve spent loong time […]

Convert PHP readfile to WP_Filesystem

In the script below, I need to convert the readfile to the WP_filesytem equivalent. However, I can’t find same in Codex. Any help much appreciated. This script saves the settings.ini file to the user’s desktop/pc. WP_Filesystem(); global $wp_filesystem; $mySettingsFileLocation = WP_PLUGIN_DIR.’/my-settings/settings.ini’; if ( ! $wp_filesystem->put_contents( $mySettingsFileLocation, $mySettings, 0644) ) { return true; } // Define […]

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”; } } […]

Using wp_filesystem in Plugins to store customizer settings

I try to fix my plugin with the answers i got here: Using wp_filesystem in Plugins Background i wrote a theme ( and a plugin ( now i want to integrate the plugin in the theme. The plugin wrote a CSS file to a folder. Write files with native PHP function is not secure as […]