The filesystem_method WordPress PHP filter allows you to modify the method used by WordPress to interact with the file system.
Usage
add_filter('filesystem_method', 'your_custom_function', 10, 4); function your_custom_function($method, $args, $context, $allow_relaxed_file_ownership) { // your custom code here return $method; }
Parameters
$method (string)
– The filesystem method to return.$args (array)
– An array of connection details for the method.$context (string)
– Full path to the directory that is tested for being writable.$allow_relaxed_file_ownership (bool)
– Whether to allow Group/World writable.
More information
See WordPress Developer Resources: filesystem_method
Examples
Change filesystem method to FTP
Force WordPress to use FTP as the filesystem method.
add_filter('filesystem_method', 'force_ftp_filesystem_method'); function force_ftp_filesystem_method($method) { return 'ftp'; }
Change filesystem method based on the current user
Set the filesystem method based on the current user’s capabilities.
add_filter('filesystem_method', 'set_filesystem_method_based_on_user', 10, 4); function set_filesystem_method_based_on_user($method, $args, $context, $allow_relaxed_file_ownership) { if (current_user_can('manage_options')) { return 'direct'; } else { return 'ftp'; } }
Disable direct filesystem method
Prevent WordPress from using the direct filesystem method.
add_filter('filesystem_method', 'disable_direct_filesystem_method'); function disable_direct_filesystem_method($method) { if ($method == 'direct') { return 'ftp'; } return $method; }
Allow relaxed file ownership for specific users
Enable relaxed file ownership for users with specific capabilities.
add_filter('filesystem_method', 'allow_relaxed_file_ownership_for_specific_users', 10, 4); function allow_relaxed_file_ownership_for_specific_users($method, $args, $context, $allow_relaxed_file_ownership) { if (current_user_can('manage_options')) { return true; } return $allow_relaxed_file_ownership; }
Change FTP credentials
Modify the FTP credentials when using the FTP filesystem method.
add_filter('filesystem_method', 'change_ftp_credentials', 10, 4); function change_ftp_credentials($method, $args, $context, $allow_relaxed_file_ownership) { if ($method == 'ftp') { $args['hostname'] = 'your_hostname'; $args['username'] = 'your_username'; $args['password'] = 'your_password'; } return $method; }