As @toscho mentioned in his comment, your best bet is probably an
autoloader
of some sort…
foreach ( glob( plugin_dir_path( __FILE__ ) . "subfolder/*.php" ) as $file ) {
include_once $file;
}
There are plugins that do this kind of thing or possilby something like code snippets, but I’ll add some examples below…
Example for basic functions in separate files in a plugin:
This example lets you easily add and remove features using theme-support. so basically in your functions.php
you can this….
add_theme_support( feature-one );
add_theme_support( feature-two );
add_theme_support( feature-three );
Create a basic plugin with the following structure:
plugin-name/
├── plugin-name.php
├── features/feature-one.php
├── features/feature-two.php
├── features/feature-three.php
└── features/feature-four.php
Inside plugin-name.php include this code:
<?php
/*
Plugin Name: My Themename Addons
Plugin URI: https://example.com/
Description: Add Themename support
Version: 1.0.0
Author: bryanwillis
Author URI: https://github.com/bryanwillis/
License: MIT License
License URI: http://opensource.org/licenses/MIT
*/
function mytheme_autoload_files() {
global $_wp_theme_features;
foreach (glob(__DIR__ . '/theme-support/*.php') as $file) {
$feature="mytheme-" . basename($file, '.php');
if (isset($_wp_theme_features[$feature])) {
require_once $file;
}
}
}
add_action('after_setup_theme', 'mytheme_autoload_files', 100);
Second Example with functions in separate folders (like entire plugins):
This example doesn’t have the option to remove features on the fly, but it automatically loads everything…
- Add a folder in your theme called
theme-plugins
. - Create a file called
autoloader.php
inside theme-plugins folder with the below code and use include/require/etc. in your functions.php to include it ( you could also make this it’s own plugin). - Drop your plugins inside the
theme-plugins
folder to autoload them ( plugins must have the same folder name as file name.
Folder structure:
themename/
└── theme-plugins/
├── autoloader.php
├── plugin-one/plugin-one.php
├── plugin-two/plugin-two.php
├── plugin-three/plugin-three.php
└── plugin-foo/plugin-foo.php
Code for autoloader.php
:
<?php
/**
* Autoloader - theme-plugins/autoloader.php
*/
function themename_plugins_autoloader() {
$plugins_dirs = = array_filter( scandir() );
$non_dirs = array(
'.',
'..',
'.DS_Store',
);
for( $i = 0; $i < count( $non_dirs ); $i++ ) {
$not_dir_key = array_search( $non_dirs[ $i ], $$autoload_plugin_dirs );
if( $not_dir_key !== false ) {
unset( $$autoload_plugin_dirs[ $not_dir_key ] );
}
unset( $not_dir_key );
}
unset( $non_dirs );
if( empty( $$autoload_plugin_dirs ) ) {
return;
}
sort( $$autoload_plugin_dirs );
foreach( $$autoload_plugin_dirs as $dir ) {
$plugin_dir = plugin_dir_path( __FILE__ ) . $dir;
$plugin_file = trailingslashit( $plugin_dir ) . $dir . '.php';
if( is_dir( $plugin_dir ) && file_exists( $plugin_file ) ) {
require_once( $plugin_file );
}
unset( $plugin_file, $plugin_dir );
}
}
themename_plugins_autoloader();