Using WordPress ‘get_plugin_files()’ PHP function

The get_plugin_files() WordPress PHP function retrieves a list of a plugin’s files.

Usage

To use the function, simply pass the relative path of the plugin file to the function.

get_plugin_files( 'your-plugin/your-plugin.php' )

Parameters

  • $plugin (string) – Required. Path to the plugin file relative to the plugins directory.

More information

See WordPress Developer Resources: get_plugin_files()

Examples

Get a list of all files in a plugin

This code will retrieve and display a list of all files in the specified plugin.

$plugin_files = get_plugin_files( 'your-plugin/your-plugin.php' );
print_r( $plugin_files );

Display the number of files in a plugin

This example will display the number of files in the specified plugin.

$plugin_files = get_plugin_files( 'your-plugin/your-plugin.php' );
echo 'Number of files in the plugin: ' . count( $plugin_files );

Check if a specific file exists in a plugin

This code checks if a specific file is part of the specified plugin.

$plugin_files = get_plugin_files( 'your-plugin/your-plugin.php' );
$file_to_search = 'your-plugin/includes/some-file.php';

if ( in_array( $file_to_search, $plugin_files ) ) {
    echo 'File exists in the plugin.';
} else {
    echo 'File not found in the plugin.';
}

List all PHP files in a plugin

This example will display a list of all PHP files in the specified plugin.

$plugin_files = get_plugin_files( 'your-plugin/your-plugin.php' );
$php_files = array_filter( $plugin_files, function ( $file ) {
    return pathinfo( $file, PATHINFO_EXTENSION ) === 'php';
} );

print_r( $php_files );

Get a list of files with a specific directory

This code will retrieve and display a list of all files in a specific directory of the specified plugin.

$plugin_files = get_plugin_files( 'your-plugin/your-plugin.php' );
$directory_to_search = 'your-plugin/includes/';

$filtered_files = array_filter( $plugin_files, function ( $file ) use ( $directory_to_search ) {
    return strpos( $file, $directory_to_search ) === 0;
} );

print_r( $filtered_files );