Wp filesystem api

From Lonsdale201/wp-agent-skills · View interactive version

Wp filesystem api is an AI agent skill from the GitHub repository Lonsdale201/wp-agent-skills. Read, write, copy, delete, chmod files from a WordPress plugin via the `WP_Filesystem` abstraction instead of bare PHP. Covers the bootstrap sequence from loading `wp-admin/includes/file.php` through `request_filesystem_credentials()` and `WP_Filesystem()` to filesystem method calls, the four transp

How to use this skill

Skill file

WordPress Filesystem API

WP abstracts filesystem access because on shared hosts the web user often can't write plugin/theme/core paths directly — FTP/SSH credentials are required. WP_Filesystem picks the right transport (direct when PHP can write as the file owner, otherwise ssh2 / ftpext / ftpsockets) and exposes a uniform method set. Plugins that call file_put_contents() directly outside writable uploads fail on those hosts; plugins that use WP_Filesystem work across more hosting setups.

When to use this skill

Trigger when ANY of the following is true:

The bootstrap — five lines, in this exact order

// 1. Load the API. NOT autoloaded.
require_once ABSPATH . 'wp-admin/includes/file.php';

// 2. Request creds (returns true for direct, an array for FTP/SSH, false if form was shown).
$creds = request_filesystem_credentials( $form_url, '', false, $context );
if ( false === $creds ) {
    return; // Form was rendered; wait for the next request.
}

// 3. Initialize (returns true|false|null).
if ( ! WP_Filesystem( $creds, $context ) ) {
    // Bad creds — re-render the form with an error.
    request_filesystem_credentials( $form_url, '', true, $context );
    return;
}

// 4. Use the global instance.
global $wp_filesystem;
$remote_context = $wp_filesystem->find_folder( $context );
if ( false === $remote_context ) {
    return;
}

if ( ! $wp_filesystem->put_contents(
    trailingslashit( $remote_context ) . 'generated.css',
    $css,
    FS_CHMOD_FILE
) ) {
    return;
}

The $context arg is a directory you are about to write to. It controls writability detection (get_filesystem_method writes a temp file there to confirm permissions) and determines whether direct is safe.

$context is a local WordPress path used for method detection. Filesystem

methods need paths in the selected transport's namespace. With direct they

look identical; with FTP/SSH they may not. Convert with find_folder() or use

base mappings such as $wp_filesystem->wp_content_dir(),

wp_plugins_dir(), or abspath() before appending a relative path.

What `request_filesystem_credentials()` returns

Verified in request_filesystem_credentials() in wp-admin/includes/file.php:

| Return | Meaning | What you do |

|---|---|---|

| true | No credentials needed — direct available on this host | Proceed to WP_Filesystem() |

| array | User entered FTP/SSH credentials | Pass to WP_Filesystem( $creds ) |

| false | Form was rendered AND no submission yet | return — wait for the form post |

The function ALSO ECHOES the form when no creds are present and $_POST is empty. Don't render your own output before calling it on an admin POST handler — the form must reach the screen.

The four transports

get_filesystem_method() picks in this order:

| Priority | Method | Condition |

|---|---|---|

| 1 | direct | PHP can write as the same owner as WP files (or $allow_relaxed_file_ownership set + dir already writable) |

| 2 | ssh2 | PHP ssh2 extension loaded AND user picked SSH in the form |

| 3 | ftpext | PHP ftp extension loaded |

| 4 | ftpsockets | PHP sockets extension OR fsockopen() available |

Only the host operator should force a specific method in wp-config.php,

after checking ownership and write permissions:

define( 'FS_METHOD', 'direct' ); // Operator-controlled; never set by a plugin.

Defaults established by WP_Filesystem() initialization:

Always pass FS_CHMOD_FILE / FS_CHMOD_DIR to write/mkdir operations so permissions match WP's own.

The methods you'll actually use

From WP_Filesystem_Base (wp-admin/includes/class-wp-filesystem-base.php):

global $wp_filesystem;

// Read.
$wp_filesystem->exists( $path );                  // bool
$wp_filesystem->is_file( $path );                 // bool
$wp_filesystem->is_dir( $path );                  // bool
$wp_filesystem->is_readable( $path );             // bool
$wp_filesystem->is_writable( $path );             // bool
$wp_filesystem->size( $path );                    // int bytes
$wp_filesystem->mtime( $path );                   // int unix ts
$wp_filesystem->get_contents( $path );            // string|false
$wp_filesystem->get_contents_array( $path );      // array|false (one line per element)
$wp_filesystem->dirlist( $path, $hidden = true, $recursive = false );

// Write.
$wp_filesystem->put_contents( $path, $content, FS_CHMOD_FILE );  // bool
$wp_filesystem->touch( $path );                                  // bool
$wp_filesystem->mkdir( $path, FS_CHMOD_DIR );                    // bool
$wp_filesystem->chmod( $path, $mode, $recursive = false );       // bool
$wp_filesystem->copy( $source, $dest, $overwrite = false, FS_CHMOD_FILE );
$wp_filesystem->move( $source, $dest, $overwrite = false );

// Delete.
$wp_filesystem->delete( $path, $recursive = false, $type = false );  // $type: 'f' | 'd' | false
$wp_filesystem->rmdir( $path, $recursive = false );

put_contents returns true on success — always check.

WordPress 7.1 transport hardening

WordPress 7.1 tightened the concrete filesystem transports. This mostly turns

warnings, invalid coercions, and ambiguous results into explicit failure

values, so callers that already check every result continue to work:

wp_plugins_dir(), and wp_themes_dir() are explicitly string|false;

FTP transports;

stop on constructor configuration errors;

more consistently;

actual listing names, and default chmod() preserves the detected mode

instead of forcibly OR-ing file bits.

Do not branch on transport-specific warning behavior. Check false from path,

read, listing, stat, and mutation methods on every supported WordPress version.

Do not instantiate WP_Filesystem_FTPext, WP_Filesystem_ftpsockets, or

WP_Filesystem_SSH2 directly; let WP_Filesystem() validate and connect them.

When NOT to use `WP_Filesystem`

| Need | Right tool |

|---|---|

| Writing a user-uploaded file to wp-content/uploads/ | wp_handle_upload() or media_handle_upload() |

| Reading any existing file (where you don't need write capability) | Plain PHP file_get_contents() is fine — read access doesn't need FS abstraction |

| Touching a local file that you just got back from wp_handle_upload | Plain PHP, after checking every return value; storage-offload plugins may change the lifecycle |

| Anything in PHP tempnam() / sys_get_temp_dir() | Plain PHP |

WP_Filesystem is overkill for wp-content/uploads. It's the right tool for wp-content/, wp-content/plugins/<self>/cache/, wp-content/mu-plugins/, or anywhere ELSE that needs the credentials dance on restricted hosts.

Usage patterns

Read references/patterns-and-mistakes.md for checked examples covering local

uploads, bundled-file reads, admin credential flows, and REST-safe failure.

Critical rules

Common mistakes

Read references/patterns-and-mistakes.md before moving a filesystem mutation

into REST, AJAX, cron, or CLI; the credentials form is admin-interactive.

Cross-references

What this skill does NOT cover

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.