The WP_Filesystem API: write files the way WordPress does
When WordPress updates a plugin, it does not always write files with PHP directly. On some servers PHP runs as a user that may not change the WordPress files, and WordPress asks for FTP or SSH credentials instead. The WP_Filesystem API is the layer that hides this: the same methods work over direct file access, FTP or SSH, whichever the server needs.
Problem: A plugin writes files with file_put_contents(), and on some hosts the writes fail or the new files belong to the wrong user.
Solution: Use the global $wp_filesystem object after initialising it with WP_Filesystem(). For files the plugin writes on its own (cache, logs, exports in uploads), check the method with get_filesystem_method() and use it only when it is direct; credentials can be asked for only on an admin screen.
Initialising
<?php
function ha_filesystem() {
global $wp_filesystem;
if ( ! function_exists( 'WP_Filesystem' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
// Without credentials: works when the method is "direct" or credentials are in wp-config.php
if ( ! WP_Filesystem() ) {
return false;
}
return $wp_filesystem;
}
WP_Filesystem() chooses the method itself: direct when PHP can write files that will belong to the same user as the WordPress files, otherwise FTP or SSH with credentials from the FTP_HOST, FTP_USER, FTP_PASS constants in wp-config.php. The method can be forced with define( 'FS_METHOD', 'direct' );.
Reading and writing
<?php
$fs = ha_filesystem();
if ( ! $fs ) {
return new WP_Error( 'ha_fs', 'The file system is not available.' );
}
$upload = wp_upload_dir();
$dir = trailingslashit( $upload['basedir'] ) . 'ha-exports';
if ( ! $fs->is_dir( $dir ) ) {
$fs->mkdir( $dir, FS_CHMOD_DIR );
}
$file = $dir . '/export.csv';
if ( ! $fs->put_contents( $file, $csv, FS_CHMOD_FILE ) ) {
return new WP_Error( 'ha_write', 'Could not write ' . $file );
}
$content = $fs->get_contents( $file );
The other methods mirror PHP: exists(), is_file(), delete(), copy(), move(), dirlist(). FS_CHMOD_FILE and FS_CHMOD_DIR are the permissions WordPress uses for its own files (usually 644 and 755).
Over FTP the paths of the server and of the FTP account can differ. For paths inside WordPress, convert them with $fs->wp_content_dir() or $fs->find_folder() when the method is not direct.
Asking for credentials in the admin
When a plugin must change files that only the FTP user may change (like core updates do), request_filesystem_credentials() prints the familiar form on an admin page and returns the entered data:
<?php
$url = wp_nonce_url( admin_url( 'tools.php?page=ha-tools' ), 'ha-tools' );
$creds = request_filesystem_credentials( $url );
if ( false === $creds ) {
return; // the form is shown, wait for the user
}
if ( ! WP_Filesystem( $creds ) ) {
request_filesystem_credentials( $url, '', true ); // wrong data: show the form again with an error
return;
}
Never call it on the front end, in cron or in AJAX: it prints HTML.
NOTE: For files a plugin creates in uploads during normal work (caches, logs, generated CSS), the uploads folder is writable by PHP on practically every host, and the method is direct. Check 'direct' === get_filesystem_method( array(), $dir ) and fall back to doing nothing (or keeping the data in the database) otherwise, instead of trying to ask a visitor for FTP credentials.