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

How to use pre_recurse_dirsize filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
March 24, 2023
5 minutes read

So, you’re venturing into the world of WordPress hooks, eh? As an Indian developer, let me tell you, hooks are like the secret masala that can spice up your WordPress site without altering the core code. Today, let’s dive into the pre_recurse_dirsize filter—a nifty hook that helps you manage file size calculations more efficiently.

pre_recurse_dirsize filter

This filter lets you return the actual used space, effectively bypassing the recursive PHP file size calculation. You can leverage a CDN API or native OS tools for improved performance. To use this filter, first register it using add_filter. You can write this code into the functions.php of your activated theme or in a custom WordPress Plugin. At WePlugins, we always prefer creating a custom WordPress Plugin to ensure nothing breaks when you update your WordPress Theme in the future.

In the example below, we define a function modify_pre_recurse_dirsize_defaults which takes five parameters and is registered using add_filter. The first parameter, pre_recurse_dirsize, is the hook name. The second parameter, modify_pre_recurse_dirsize_defaults, is the function name to be called. The third parameter is the priority for calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments to be passed in the registered function.

Sometimes, you might need to remove a registered hook, and that’s where remove_filter comes into play.

Parameters

  • $space_used: (int|false) The amount of used space, in bytes. Default false.
  • $directory: (string) Full path of a directory.
  • $exclude: (string|string[]|null) Full path of a subdirectory to exclude from the total, or array of paths.
  • $max_execution_time: (int) Maximum time to run before giving up. In seconds.
  • $directory_cache: (array) Array of cached directory paths.

Example 1: Basic Hook Usage

Here’s a basic example of using this hook.

    function weplugins_modify_pre_recurse_dirsize_defaults($space_used, $directory, $exclude, $max_execution_time, $directory_cache) { 
        // Update the $space_used variable according to your website requirements and return this variable.
        return $space_used; 
    }
    // add the filter
    add_filter( "pre_recurse_dirsize", "weplugins_modify_pre_recurse_dirsize_defaults", 10, 5 );
    

Example 2: Removing a Hook Callback

If you need to remove a hook callback, use the example below.

    remove_filter( "pre_recurse_dirsize", "weplugins_modify_pre_recurse_dirsize_defaults", 10, 5 );
    

Ensure you provide the same callback function name, priority, and number of arguments when removing the hook callback.

Example 3: Conditional Modification of $space_used

You can conditionally modify the $space_used variable based on your website’s requirements.

    function weplugins_conditionally_modify_pre_recurse_dirsize($space_used, $directory, $exclude, $max_execution_time, $directory_cache) { 
        if ($directory === '/specific/path') {
            $space_used = 1024; // Example modification
        }
        return $space_used; 
    }
    // add the filter
    add_filter( "pre_recurse_dirsize", "weplugins_conditionally_modify_pre_recurse_dirsize", 10, 5 );
    

Access Premium WordPress Plugins

Contact Us

If you’re having any trouble using this hook or need customization, please contact us. We’re here to help!

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.