Using WordPress ‘fs_ftp_connection_types’ PHP filter

The fs_ftp_connection_types WordPress PHP filter allows you to modify the connection types available for the filesystem credentials form.

Usage

add_filter('fs_ftp_connection_types', 'your_custom_function', 10, 5);
function your_custom_function($types, $credentials, $type, $error, $context) {
    // your custom code here
    return $types;
}

Parameters

  • $types (string[]): Types of connections available.
  • $credentials (array): Credentials to connect with.
  • $type (string): Chosen filesystem method.
  • $error (bool|WP_Error): Whether the current request has failed to connect, or an error object.
  • $context (string): Full path to the directory that is tested for being writable.

More information

See WordPress Developer Resources: fs_ftp_connection_types

Examples

Add a new connection type

Add a custom connection type to the list of available connection types.

add_filter('fs_ftp_connection_types', 'add_custom_connection_type', 10, 5);
function add_custom_connection_type($types, $credentials, $type, $error, $context) {
    $types['custom'] = __('Custom Connection Type');
    return $types;
}

Remove a connection type

Remove the FTP connection type from the list of available connection types.

add_filter('fs_ftp_connection_types', 'remove_ftp_connection_type', 10, 5);
function remove_ftp_connection_type($types, $credentials, $type, $error, $context) {
    unset($types['ftp']);
    return $types;
}

Change the order of connection types

Change the order of connection types in the list.

add_filter('fs_ftp_connection_types', 'change_connection_type_order', 10, 5);
function change_connection_type_order($types, $credentials, $type, $error, $context) {
    $new_order = array('ssh2' => $types['ssh2'], 'ftpext' => $types['ftpext'], 'ftpsockets' => $types['ftpsockets']);
    return $new_order;
}

Customize connection types based on user role

Show different connection types for different user roles.

add_filter('fs_ftp_connection_types', 'customize_connection_types_based_on_role', 10, 5);
function customize_connection_types_based_on_role($types, $credentials, $type, $error, $context) {
    if (current_user_can('administrator')) {
        return $types;
    } else {
        unset($types['ssh2']);
        return $types;
    }
}

Modify connection types based on the error

Modify the connection types based on whether the current request has failed to connect.

add_filter('fs_ftp_connection_types', 'modify_connection_types_based_on_error', 10, 5);
function modify_connection_types_based_on_error($types, $credentials, $type, $error, $context) {
    if ($error) {
        unset($types['ftp']);
    }
    return $types;
}