Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use request_filesystem_credentials filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
September 12, 2022
5 minutes read

request_filesystem_credentials filter

Returning anything other than an empty string will effectively short-circuit output of the filesystem credentials form, returning that value instead.

To use the request_filesystem_credentials filter, first register it using add_filter. You can place this code in the functions.php file of your active theme or in a custom WordPress Plugin.

We at WePlugins always prefer creating a custom WordPress Plugin for hooks to avoid breaking anything when you update your WordPress Theme in the future.

In the example below, we define a function weplugins_modify_request_filesystem_credentials_defaults which takes 7 parameters and registers it using add_filter. The first parameter request_filesystem_credentials is the name of the hook, the second parameter weplugins_modify_request_filesystem_credentials_defaults is the name of the function to call, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to pass to the registered function.

Sometimes, you need to remove a registered hook, so you can use remove_filter to remove the request_filesystem_credentials filter.

Parameters

Below are the 7 parameters required to use this hook.

  • $credentials: (mixed) Credentials to return instead. Default empty string.
  • $form_post: (string) The URL to post the form to.
  • $type: (string) Chosen type of filesystem.
  • $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.
  • $extra_fields: (array) Extra POST fields.
  • $allow_relaxed_file_ownership: (bool) Whether to allow Group/World writable.

Live Example

apply_filters( 'request_filesystem_credentials', mixed $credentials, string $form_post, string $type, bool|WP_Error $error, string $context, array $extra_fields, bool $allow_relaxed_file_ownership )

Below is an example of how you can use this hook.

    function weplugins_modify_request_filesystem_credentials_defaults($credentials, $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership) { 
        // Update the $credentials variable according to your website requirements and return this variable. 
        // You can modify the $credentials variable conditionally too if you want.
        return $credentials; 
    }
    // add the filter
    add_filter( "request_filesystem_credentials", "weplugins_modify_request_filesystem_credentials_defaults", 10, 7 );
    

To remove a hook callback, use the example below.

    remove_filter( "request_filesystem_credentials", "weplugins_modify_request_filesystem_credentials_defaults", 10, 7 );
    

Please ensure you provide the same callback function name, priority, and number of arguments while removing the hook callback.

Example 1: Customizing Filesystem Credentials

This example demonstrates how to customize the filesystem credentials based on specific conditions.

    function weplugins_customize_filesystem_credentials($credentials, $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership) {
        if ($context === '/specific-path') {
            $credentials = 'custom_credentials';
        }
        return $credentials;
    }
    add_filter('request_filesystem_credentials', 'weplugins_customize_filesystem_credentials', 10, 7);
    

Example 2: Bypassing Filesystem Form

This example shows how to bypass the filesystem form by returning preset credentials.

    function weplugins_bypass_filesystem_form($credentials, $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership) {
        return 'preset_credentials';
    }
    add_filter('request_filesystem_credentials', 'weplugins_bypass_filesystem_form', 10, 7);
    

Example 3: Error Handling for Filesystem Credentials

This example illustrates how to add error handling logic for filesystem credentials.

    function weplugins_handle_filesystem_errors($credentials, $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership) {
        if (is_wp_error($error)) {
            // Handle error
            error_log($error->get_error_message());
        }
        return $credentials;
    }
    add_filter('request_filesystem_credentials', 'weplugins_handle_filesystem_errors', 10, 7);
    

Access Premium WordPress Plugins

Contact Us

If you need any customization or help with this hook, feel free to contact us.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.