1

I'm building a custom theme for in-house use only. I started by putting a lot of the core functions in plugins, but then that seemed silly since they don't require any user interaction.

Next, I put all of my classes in different php files in my theme folder and include them all in functions.php.

Is there a better way to do this?

  • Why don't you use an auto-loader? – fuxia Jan 26 '16 at 19:52
1

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...

  1. Add a folder in your theme called theme-plugins.
  2. 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).
  3. 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();
  • This is amazing. Thanks so much. I can't wait to give it a try. – ibhhvc Jan 27 '16 at 23:13
0

I store core functions in a parent theme, then load site-specific functions to a child theme.

Benefits:

  1. Decluttering the (child) theme with code that hardly ever changes per in-house preferences.
  2. Ability to push new core functionality to many installations by simply updating the parent theme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.