Using WordPress ‘filesystem_method_file’ PHP filter

The filesystem_method_file WordPress PHP filter allows you to modify the path for a specific filesystem method class file.

Usage

add_filter('filesystem_method_file', 'your_custom_function', 10, 2);

function your_custom_function($path, $method) {
    // your custom code here
    return $path;
}

Parameters

  • $path (string) – Path to the specific filesystem method class file.
  • $method (string) – The filesystem method to use.

More information

See WordPress Developer Resources: filesystem_method_file

Examples

Changing the path for the FTP filesystem method

In this example, we will change the path for the FTP filesystem method.

add_filter('filesystem_method_file', 'change_ftp_file_path', 10, 2);

function change_ftp_file_path($path, $method) {
    if ('ftp' == $method) {
        $path = '/new/path/to/ftp/filesystem/method/class/file.php';
    }
    return $path;
}

Adding a custom filesystem method

In this example, we will add a custom filesystem method named ‘custom_method’.

add_filter('filesystem_method_file', 'add_custom_filesystem_method', 10, 2);

function add_custom_filesystem_method($path, $method) {
    if ('custom_method' == $method) {
        $path = '/path/to/custom/filesystem/method/class/file.php';
    }
    return $path;
}

Changing the path based on a condition

In this example, we will change the path for the ‘direct’ filesystem method if a certain condition is met.

add_filter('filesystem_method_file', 'conditionally_change_direct_path', 10, 2);

function conditionally_change_direct_path($path, $method) {
    if ('direct' == $method && your_custom_condition()) {
        $path = '/new/path/to/direct/filesystem/method/class/file.php';
    }
    return $path;
}

Changing paths for multiple filesystem methods

In this example, we will change the paths for the ‘direct’ and ‘ssh’ filesystem methods.

add_filter('filesystem_method_file', 'change_multiple_filesystem_paths', 10, 2);

function change_multiple_filesystem_paths($path, $method) {
    if ('direct' == $method) {
        $path = '/new/path/to/direct/filesystem/method/class/file.php';
    } elseif ('ssh' == $method) {
        $path = '/new/path/to/ssh/filesystem/method/class/file.php';
    }
    return $path;
}

Logging the filesystem method

In this example, we will log the filesystem method being used.

add_filter('filesystem_method_file', 'log_filesystem_method', 10, 2);

function log_filesystem_method($path, $method) {
    error_log("The filesystem method being used is: $method");
    return $path;
}