Using WordPress ‘plugin_files_exclusions’ PHP filter

The plugin_files_exclusions WordPress PHP filter allows you to modify the array of excluded directories and files while scanning the folder.

Usage

add_filter('plugin_files_exclusions', 'your_custom_function');
function your_custom_function($exclusions) {
    // your custom code here
    return $exclusions;
}

Parameters

  • $exclusions (string[]): Array of excluded directories and files.

More information

See WordPress Developer Resources: plugin_files_exclusions

Examples

Exclude a specific directory

To exclude a specific directory from the scanning process, add its name to the exclusions array.

add_filter('plugin_files_exclusions', 'exclude_specific_directory');
function exclude_specific_directory($exclusions) {
    $exclusions[] = 'my_directory_to_exclude';
    return $exclusions;
}

Exclude multiple directories

To exclude multiple directories, add their names to the exclusions array.

add_filter('plugin_files_exclusions', 'exclude_multiple_directories');
function exclude_multiple_directories($exclusions) {
    $dirs_to_exclude = ['directory_one', 'directory_two', 'directory_three'];
    $exclusions = array_merge($exclusions, $dirs_to_exclude);
    return $exclusions;
}

Exclude a specific file

To exclude a specific file, add its name to the exclusions array.

add_filter('plugin_files_exclusions', 'exclude_specific_file');
function exclude_specific_file($exclusions) {
    $exclusions[] = 'my_file_to_exclude.php';
    return $exclusions;
}

Exclude files with a specific extension

To exclude all files with a specific extension, iterate through the exclusions array and add the file names that match the extension.

add_filter('plugin_files_exclusions', 'exclude_files_with_extension');
function exclude_files_with_extension($exclusions) {
    $extension_to_exclude = '.txt';

    foreach (glob('*' . $extension_to_exclude) as $file) {
        $exclusions[] = $file;
    }

    return $exclusions;
}

Remove an exclusion

To remove an exclusion from the list, use array_search and unset functions.

add_filter('plugin_files_exclusions', 'remove_exclusion_from_list');
function remove_exclusion_from_list($exclusions) {
    $exclusion_to_remove = 'directory_to_include';

    if (($key = array_search($exclusion_to_remove, $exclusions)) !== false) {
        unset($exclusions[$key]);
    }

    return $exclusions;
}