Using WordPress ‘request_filesystem_credentials()’ PHP function

The request_filesystem_credentials() WordPress PHP function displays a form to the user to request their FTP/SSH details in order to connect to the filesystem.

Usage

$creds = request_filesystem_credentials($form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership);

Example:

$form_post = 'http://example.com/post-url/';
$creds = request_filesystem_credentials($form_post);
if (false === $creds) {
    return;
}

Parameters

  • $form_post (string) (Required): The URL to post the form to.
  • $type (string) (Optional): Chosen type of filesystem. Default: ”.
  • $error (bool|WP_Error) (Optional): Whether the current request has failed to connect, or an error object. Default: false.
  • $context (string) (Optional): Full path to the directory that is tested for being writable. Default: ”.
  • $extra_fields (array) (Optional): Extra POST fields to be checked for inclusion in the post. Default: null.
  • $allow_relaxed_file_ownership (bool) (Optional): Whether to allow Group/World writable. Default: false.

More information

See WordPress Developer Resources: request_filesystem_credentials()

Examples

Basic usage

This example shows the basic usage of request_filesystem_credentials() function to display a form for FTP/SSH details.

$form_post = 'http://example.com/post-url/';
$creds = request_filesystem_credentials($form_post);
if (false === $creds) {
    return;
}

Specifying the type of filesystem

In this example, the function is used to request filesystem credentials specifically for ‘ftpext’ type.

$form_post = 'http://example.com/post-url/';
$type = 'ftpext';
$creds = request_filesystem_credentials($form_post, $type);
if (false === $creds) {
    return;
}

Handling error

This example demonstrates how to handle an error when requesting filesystem credentials.

$form_post = 'http://example.com/post-url/';
$type = 'ftpext';
$error = new WP_Error('connection_failed', 'Connection failed');
$creds = request_filesystem_credentials($form_post, $type, $error);
if (false === $creds) {
    return;
}

Specifying context and extra fields

This example shows how to specify a context (directory) and extra POST fields when requesting filesystem credentials.

$form_post = 'http://example.com/post-url/';
$context = '/path/to/directory/';
$extra_fields = array('field1' => 'value1', 'field2' => 'value2');
$creds = request_filesystem_credentials($form_post, '', false, $context, $extra_fields);
if (false === $creds) {
    return;
}

Allowing relaxed file ownership

This example demonstrates how to allow Group/World writable when requesting filesystem credentials.

$form_post = 'http://example.com/post-url/';
$allow_relaxed_file_ownership = true;
$creds = request_filesystem_credentials($form_post, '', false, '', null, $allow_relaxed_file_ownership);
if (false === $creds) {
    return;
}